forked from pre-commit/pre-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.py
42 lines (31 loc) · 940 Bytes
/
color.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from __future__ import unicode_literals
import sys
RED = '\033[41m'
GREEN = '\033[42m'
YELLOW = '\033[43;30m'
TURQUOISE = '\033[46;30m'
NORMAL = '\033[0m'
class InvalidColorSetting(ValueError):
pass
def format_color(text, color, use_color_setting):
"""Format text with color.
Args:
text - Text to be formatted with color if `use_color`
color - The color start string
use_color_setting - Whether or not to color
"""
if not use_color_setting:
return text
else:
return u'{0}{1}{2}'.format(color, text, NORMAL)
def use_color(setting):
"""Choose whether to use color based on the command argument.
Args:
setting - Either `auto`, `always`, or `never`
"""
if setting not in ('auto', 'always', 'never'):
raise InvalidColorSetting(setting)
return (
setting == 'always' or
(setting == 'auto' and sys.stdout.isatty())
)