mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Wire in color for pre-commit. Closes #63.
This commit is contained in:
parent
8aa88c7363
commit
73e0111e38
3 changed files with 20 additions and 6 deletions
|
|
@ -6,6 +6,9 @@ GREEN = '\033[42m'
|
||||||
NORMAL = '\033[0m'
|
NORMAL = '\033[0m'
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidColorSetting(ValueError): pass
|
||||||
|
|
||||||
|
|
||||||
def format_color(text, color, use_color):
|
def format_color(text, color, use_color):
|
||||||
"""Format text with color.
|
"""Format text with color.
|
||||||
|
|
||||||
|
|
@ -26,6 +29,9 @@ def use_color(setting):
|
||||||
Args:
|
Args:
|
||||||
setting - Either `auto`, `always`, or `never`
|
setting - Either `auto`, `always`, or `never`
|
||||||
"""
|
"""
|
||||||
|
if setting not in ('auto', 'always', 'never'):
|
||||||
|
raise InvalidColorSetting(setting)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
setting == 'always' or
|
setting == 'always' or
|
||||||
(setting == 'auto' and sys.stdout.isatty())
|
(setting == 'auto' and sys.stdout.isatty())
|
||||||
|
|
|
||||||
|
|
@ -5,15 +5,13 @@ import argparse
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from pre_commit import color
|
||||||
from pre_commit import commands
|
from pre_commit import commands
|
||||||
from pre_commit import git
|
from pre_commit import git
|
||||||
from pre_commit.runner import Runner
|
from pre_commit.runner import Runner
|
||||||
from pre_commit.util import entry
|
from pre_commit.util import entry
|
||||||
|
|
||||||
|
|
||||||
RED = '\033[41m'
|
|
||||||
GREEN = '\033[42m'
|
|
||||||
NORMAL = '\033[0m'
|
|
||||||
COLS = int(subprocess.Popen(['tput', 'cols'], stdout=subprocess.PIPE).communicate()[0])
|
COLS = int(subprocess.Popen(['tput', 'cols'], stdout=subprocess.PIPE).communicate()[0])
|
||||||
|
|
||||||
PASS_FAIL_LENGTH = 6
|
PASS_FAIL_LENGTH = 6
|
||||||
|
|
@ -46,15 +44,15 @@ def _run_single_hook(runner, repository, hook_id, args):
|
||||||
output = '\n'.join([stdout, stderr]).strip()
|
output = '\n'.join([stdout, stderr]).strip()
|
||||||
if retcode != repository.hooks[hook_id]['expected_return_value']:
|
if retcode != repository.hooks[hook_id]['expected_return_value']:
|
||||||
retcode = 1
|
retcode = 1
|
||||||
color = RED
|
print_color = color.RED
|
||||||
pass_fail = 'Failed'
|
pass_fail = 'Failed'
|
||||||
else:
|
else:
|
||||||
retcode = 0
|
retcode = 0
|
||||||
color = GREEN
|
print_color = color.GREEN
|
||||||
pass_fail = 'Passed'
|
pass_fail = 'Passed'
|
||||||
|
|
||||||
|
|
||||||
print('{0}{1}{2}'.format(color, pass_fail, NORMAL))
|
print(color.format_color(pass_fail, print_color, args.color))
|
||||||
|
|
||||||
if output and (retcode or args.verbose):
|
if output and (retcode or args.verbose):
|
||||||
print('\n' + output)
|
print('\n' + output)
|
||||||
|
|
@ -110,6 +108,10 @@ def run(argv):
|
||||||
help='Run on all the files in the repo.',
|
help='Run on all the files in the repo.',
|
||||||
)
|
)
|
||||||
run.add_argument('--verbose', '-v', action='store_true', default=False)
|
run.add_argument('--verbose', '-v', action='store_true', default=False)
|
||||||
|
run.add_argument(
|
||||||
|
'--color', default='auto', type=color.use_color,
|
||||||
|
help='Whether to use color in output. Defaults to `auto`',
|
||||||
|
)
|
||||||
|
|
||||||
help = subparsers.add_parser('help', help='Show help for a specific command.')
|
help = subparsers.add_parser('help', help='Show help for a specific command.')
|
||||||
help.add_argument('help_cmd', nargs='?', help='Command to show help for.')
|
help.add_argument('help_cmd', nargs='?', help='Command to show help for.')
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import sys
|
||||||
|
|
||||||
from pre_commit.color import format_color
|
from pre_commit.color import format_color
|
||||||
from pre_commit.color import GREEN
|
from pre_commit.color import GREEN
|
||||||
|
from pre_commit.color import InvalidColorSetting
|
||||||
from pre_commit.color import use_color
|
from pre_commit.color import use_color
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -33,3 +34,8 @@ def test_use_color_no_tty():
|
||||||
def test_use_color_tty():
|
def test_use_color_tty():
|
||||||
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
|
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
|
||||||
assert use_color('auto') is True
|
assert use_color('auto') is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_use_color_raises_if_given_shenanigans():
|
||||||
|
with pytest.raises(InvalidColorSetting):
|
||||||
|
use_color('herpaderp')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue