Skip to content

Commit a677c42

Browse files
committed
Use 80 or min width instead of terminal size
1 parent 0a81024 commit a677c42

File tree

4 files changed

+63
-31
lines changed

4 files changed

+63
-31
lines changed

pre_commit/commands/run.py

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,6 @@ def _hook_msg_start(hook, verbose):
3030
)
3131

3232

33-
def _print_no_files_skipped(hook, write, args):
34-
write(get_hook_message(
35-
_hook_msg_start(hook, args.verbose),
36-
postfix='(no files to check) ',
37-
end_msg='Skipped',
38-
end_color=color.TURQUOISE,
39-
use_color=args.color,
40-
))
41-
42-
43-
def _print_user_skipped(hook, write, args):
44-
write(get_hook_message(
45-
_hook_msg_start(hook, args.verbose),
46-
end_msg='Skipped',
47-
end_color=color.YELLOW,
48-
use_color=args.color,
49-
))
50-
51-
5233
def get_changed_files(new, old):
5334
return cmd_output(
5435
'git', 'diff', '--name-only', '{0}...{1}'.format(old, new),
@@ -71,18 +52,37 @@ def get_filenames(args, include_expr, exclude_expr):
7152
return getter(include_expr, exclude_expr)
7253

7354

74-
def _run_single_hook(hook, repo, args, write, skips=frozenset()):
55+
SKIPPED = 'Skipped'
56+
NO_FILES = '(no files to check)'
57+
58+
59+
def _run_single_hook(hook, repo, args, write, skips, cols):
7560
filenames = get_filenames(args, hook['files'], hook['exclude'])
7661
if hook['id'] in skips:
77-
_print_user_skipped(hook, write, args)
62+
write(get_hook_message(
63+
_hook_msg_start(hook, args.verbose),
64+
end_msg=SKIPPED,
65+
end_color=color.YELLOW,
66+
use_color=args.color,
67+
cols=cols,
68+
))
7869
return 0
7970
elif not filenames and not hook['always_run']:
80-
_print_no_files_skipped(hook, write, args)
71+
write(get_hook_message(
72+
_hook_msg_start(hook, args.verbose),
73+
postfix=NO_FILES,
74+
end_msg=SKIPPED,
75+
end_color=color.TURQUOISE,
76+
use_color=args.color,
77+
cols=cols,
78+
))
8179
return 0
8280

8381
# Print the hook and the dots first in case the hook takes hella long to
8482
# run.
85-
write(get_hook_message(_hook_msg_start(hook, args.verbose), end_len=6))
83+
write(get_hook_message(
84+
_hook_msg_start(hook, args.verbose), end_len=6, cols=cols,
85+
))
8686
sys.stdout.flush()
8787

8888
diff_before = cmd_output('git', 'diff', retcode=None, encoding=None)
@@ -128,12 +128,32 @@ def _run_single_hook(hook, repo, args, write, skips=frozenset()):
128128
return retcode
129129

130130

131+
def _compute_cols(hooks, verbose):
132+
"""Compute the number of columns to display hook messages. The widest
133+
that will be displayed is in the no files skipped case:
134+
135+
Hook name...(no files to check) Skipped
136+
137+
or in the verbose case
138+
139+
Hook name [hookid]...(no files to check) Skipped
140+
"""
141+
if hooks:
142+
name_len = max(len(_hook_msg_start(hook, verbose)) for hook in hooks)
143+
else:
144+
name_len = 0
145+
146+
cols = name_len + 3 + len(NO_FILES) + 1 + len(SKIPPED)
147+
return max(cols, 80)
148+
149+
131150
def _run_hooks(repo_hooks, args, write, environ):
132151
"""Actually run the hooks."""
133152
skips = _get_skips(environ)
153+
cols = _compute_cols([hook for _, hook in repo_hooks], args.verbose)
134154
retval = 0
135155
for repo, hook in repo_hooks:
136-
retval |= _run_single_hook(hook, repo, args, write, skips)
156+
retval |= _run_single_hook(hook, repo, args, write, skips, cols)
137157
return retval
138158

139159

pre_commit/output.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,18 @@
22

33
import sys
44

5-
from pyterminalsize import get_terminal_size
6-
75
from pre_commit import color
86
from pre_commit import five
97

108

11-
COLS = get_terminal_size((80, 0)).columns
12-
13-
149
def get_hook_message(
1510
start,
1611
postfix='',
1712
end_msg=None,
1813
end_len=0,
1914
end_color=None,
2015
use_color=None,
21-
cols=COLS,
16+
cols=80,
2217
):
2318
"""Prints a message for running a hook.
2419

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
'cached-property',
4242
'jsonschema',
4343
'nodeenv>=0.11.1',
44-
'pyterminalsize',
4544
'pyyaml',
4645
'virtualenv',
4746
],

tests/commands/run_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import pre_commit.constants as C
1515
from pre_commit.commands.install_uninstall import install
16+
from pre_commit.commands.run import _compute_cols
1617
from pre_commit.commands.run import _get_skips
1718
from pre_commit.commands.run import _has_unmerged_paths
1819
from pre_commit.commands.run import get_changed_files
@@ -279,6 +280,23 @@ def test_merge_conflict_resolved(in_merge_conflict, mock_out_store_directory):
279280
assert msg in printed
280281

281282

283+
@pytest.mark.parametrize(
284+
('hooks', 'verbose', 'expected'),
285+
(
286+
([], True, 80),
287+
([{'id': 'a', 'name': 'a' * 51}], False, 81),
288+
([{'id': 'a', 'name': 'a' * 51}], True, 85),
289+
(
290+
[{'id': 'a', 'name': 'a' * 51}, {'id': 'b', 'name': 'b' * 52}],
291+
False,
292+
82,
293+
),
294+
),
295+
)
296+
def test_compute_cols(hooks, verbose, expected):
297+
assert _compute_cols(hooks, verbose) == expected
298+
299+
282300
@pytest.mark.parametrize(
283301
('environ', 'expected_output'),
284302
(

0 commit comments

Comments
 (0)