Skip to content

Commit 73e0111

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

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

pre_commit/color.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
NORMAL = '\033[0m'
77

88

9+
class InvalidColorSetting(ValueError): pass
10+
11+
912
def format_color(text, color, use_color):
1013
"""Format text with color.
1114
@@ -26,6 +29,9 @@ def use_color(setting):
2629
Args:
2730
setting - Either `auto`, `always`, or `never`
2831
"""
32+
if setting not in ('auto', 'always', 'never'):
33+
raise InvalidColorSetting(setting)
34+
2935
return (
3036
setting == 'always' or
3137
(setting == 'auto' and sys.stdout.isatty())

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
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from pre_commit.color import format_color
77
from pre_commit.color import GREEN
8+
from pre_commit.color import InvalidColorSetting
89
from pre_commit.color import use_color
910

1011

@@ -33,3 +34,8 @@ def test_use_color_no_tty():
3334
def test_use_color_tty():
3435
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
3536
assert use_color('auto') is True
37+
38+
39+
def test_use_color_raises_if_given_shenanigans():
40+
with pytest.raises(InvalidColorSetting):
41+
use_color('herpaderp')

0 commit comments

Comments
 (0)