|
5 | 5 | import subprocess |
6 | 6 | import sys |
7 | 7 |
|
| 8 | +from pre_commit import color |
8 | 9 | from pre_commit import commands |
9 | 10 | from pre_commit import git |
10 | 11 | from pre_commit.runner import Runner |
11 | 12 | from pre_commit.util import entry |
12 | 13 |
|
13 | 14 |
|
14 | | -RED = '\033[41m' |
15 | | -GREEN = '\033[42m' |
16 | | -NORMAL = '\033[0m' |
17 | 15 | COLS = int(subprocess.Popen(['tput', 'cols'], stdout=subprocess.PIPE).communicate()[0]) |
18 | 16 |
|
19 | 17 | PASS_FAIL_LENGTH = 6 |
@@ -46,15 +44,15 @@ def _run_single_hook(runner, repository, hook_id, args): |
46 | 44 | output = '\n'.join([stdout, stderr]).strip() |
47 | 45 | if retcode != repository.hooks[hook_id]['expected_return_value']: |
48 | 46 | retcode = 1 |
49 | | - color = RED |
| 47 | + print_color = color.RED |
50 | 48 | pass_fail = 'Failed' |
51 | 49 | else: |
52 | 50 | retcode = 0 |
53 | | - color = GREEN |
| 51 | + print_color = color.GREEN |
54 | 52 | pass_fail = 'Passed' |
55 | 53 |
|
56 | 54 |
|
57 | | - print('{0}{1}{2}'.format(color, pass_fail, NORMAL)) |
| 55 | + print(color.format_color(pass_fail, print_color, args.color)) |
58 | 56 |
|
59 | 57 | if output and (retcode or args.verbose): |
60 | 58 | print('\n' + output) |
@@ -110,6 +108,10 @@ def run(argv): |
110 | 108 | help='Run on all the files in the repo.', |
111 | 109 | ) |
112 | 110 | 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 | + ) |
113 | 115 |
|
114 | 116 | help = subparsers.add_parser('help', help='Show help for a specific command.') |
115 | 117 | help.add_argument('help_cmd', nargs='?', help='Command to show help for.') |
|
0 commit comments