Wire in color for pre-commit. Closes #63. · precommit/pre-commit@73e0111 · GitHub
Skip to content

Commit 73e0111

Browse files
committed
Wire in color for pre-commit. Closes pre-commit#63.
1 parent 8aa88c7 commit 73e0111

3 files changed

Lines changed: 20 additions & 6 deletions

File tree

pre_commit/color.py

Lines changed: 6 additions & 0 deletions

pre_commit/run.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
import subprocess
66
import sys
77

8+
from pre_commit import color
89
from pre_commit import commands
910
from pre_commit import git
1011
from pre_commit.runner import Runner
1112
from pre_commit.util import entry
1213

1314

14-
RED = '\033[41m'
15-
GREEN = '\033[42m'
16-
NORMAL = '\033[0m'
1715
COLS = int(subprocess.Popen(['tput', 'cols'], stdout=subprocess.PIPE).communicate()[0])
1816

1917
PASS_FAIL_LENGTH = 6
@@ -46,15 +44,15 @@ def _run_single_hook(runner, repository, hook_id, args):
4644
output = '\n'.join([stdout, stderr]).strip()
4745
if retcode != repository.hooks[hook_id]['expected_return_value']:
4846
retcode = 1
49-
color = RED
47+
print_color = color.RED
5048
pass_fail = 'Failed'
5149
else:
5250
retcode = 0
53-
color = GREEN
51+
print_color = color.GREEN
5452
pass_fail = 'Passed'
5553

5654

57-
print('{0}{1}{2}'.format(color, pass_fail, NORMAL))
55+
print(color.format_color(pass_fail, print_color, args.color))
5856

5957
if output and (retcode or args.verbose):
6058
print('\n' + output)
@@ -110,6 +108,10 @@ def run(argv):
110108
help='Run on all the files in the repo.',
111109
)
112110
run.add_argument('--verbose', '-v', action='store_true', default=False)
111+
run.add_argument(
112+
'--color', default='auto', type=color.use_color,
113+
help='Whether to use color in output. Defaults to `auto`',
114+
)
113115

114116
help = subparsers.add_parser('help', help='Show help for a specific command.')
115117
help.add_argument('help_cmd', nargs='?', help='Command to show help for.')

tests/color_test.py

Lines changed: 6 additions & 0 deletions

0 commit comments

Comments
 (0)