summaryrefslogtreecommitdiffstats
path: root/color.py
diff options
context:
space:
mode:
Diffstat (limited to 'color.py')
-rw-r--r--color.py73
1 files changed, 51 insertions, 22 deletions
diff --git a/color.py b/color.py
index 7970198a..0218aab8 100644
--- a/color.py
+++ b/color.py
@@ -18,41 +18,43 @@ import sys
18 18
19import pager 19import pager
20 20
21COLORS = {None :-1, 21COLORS = {None: -1,
22 'normal' :-1, 22 'normal': -1,
23 'black' : 0, 23 'black': 0,
24 'red' : 1, 24 'red': 1,
25 'green' : 2, 25 'green': 2,
26 'yellow' : 3, 26 'yellow': 3,
27 'blue' : 4, 27 'blue': 4,
28 'magenta': 5, 28 'magenta': 5,
29 'cyan' : 6, 29 'cyan': 6,
30 'white' : 7} 30 'white': 7}
31 31
32ATTRS = {None :-1, 32ATTRS = {None: -1,
33 'bold' : 1, 33 'bold': 1,
34 'dim' : 2, 34 'dim': 2,
35 'ul' : 4, 35 'ul': 4,
36 'blink' : 5, 36 'blink': 5,
37 'reverse': 7} 37 'reverse': 7}
38 38
39RESET = "\033[m" # pylint: disable=W1401 39RESET = "\033[m"
40 # backslash is not anomalous 40
41 41
42def is_color(s): 42def is_color(s):
43 return s in COLORS 43 return s in COLORS
44 44
45
45def is_attr(s): 46def is_attr(s):
46 return s in ATTRS 47 return s in ATTRS
47 48
48def _Color(fg = None, bg = None, attr = None): 49
50def _Color(fg=None, bg=None, attr=None):
49 fg = COLORS[fg] 51 fg = COLORS[fg]
50 bg = COLORS[bg] 52 bg = COLORS[bg]
51 attr = ATTRS[attr] 53 attr = ATTRS[attr]
52 54
53 if attr >= 0 or fg >= 0 or bg >= 0: 55 if attr >= 0 or fg >= 0 or bg >= 0:
54 need_sep = False 56 need_sep = False
55 code = "\033[" #pylint: disable=W1401 57 code = "\033["
56 58
57 if attr >= 0: 59 if attr >= 0:
58 code += chr(ord('0') + attr) 60 code += chr(ord('0') + attr)
@@ -71,7 +73,6 @@ def _Color(fg = None, bg = None, attr = None):
71 if bg >= 0: 73 if bg >= 0:
72 if need_sep: 74 if need_sep:
73 code += ';' 75 code += ';'
74 need_sep = True
75 76
76 if bg < 8: 77 if bg < 8:
77 code += '4%c' % (ord('0') + bg) 78 code += '4%c' % (ord('0') + bg)
@@ -82,6 +83,27 @@ def _Color(fg = None, bg = None, attr = None):
82 code = '' 83 code = ''
83 return code 84 return code
84 85
86DEFAULT = None
87
88
89def SetDefaultColoring(state):
90 """Set coloring behavior to |state|.
91
92 This is useful for overriding config options via the command line.
93 """
94 if state is None:
95 # Leave it alone -- return quick!
96 return
97
98 global DEFAULT
99 state = state.lower()
100 if state in ('auto',):
101 DEFAULT = state
102 elif state in ('always', 'yes', 'true', True):
103 DEFAULT = 'always'
104 elif state in ('never', 'no', 'false', False):
105 DEFAULT = 'never'
106
85 107
86class Coloring(object): 108class Coloring(object):
87 def __init__(self, config, section_type): 109 def __init__(self, config, section_type):
@@ -89,9 +111,11 @@ class Coloring(object):
89 self._config = config 111 self._config = config
90 self._out = sys.stdout 112 self._out = sys.stdout
91 113
92 on = self._config.GetString(self._section) 114 on = DEFAULT
93 if on is None: 115 if on is None:
94 on = self._config.GetString('color.ui') 116 on = self._config.GetString(self._section)
117 if on is None:
118 on = self._config.GetString('color.ui')
95 119
96 if on == 'auto': 120 if on == 'auto':
97 if pager.active or os.isatty(1): 121 if pager.active or os.isatty(1):
@@ -122,6 +146,7 @@ class Coloring(object):
122 def printer(self, opt=None, fg=None, bg=None, attr=None): 146 def printer(self, opt=None, fg=None, bg=None, attr=None):
123 s = self 147 s = self
124 c = self.colorer(opt, fg, bg, attr) 148 c = self.colorer(opt, fg, bg, attr)
149
125 def f(fmt, *args): 150 def f(fmt, *args):
126 s._out.write(c(fmt, *args)) 151 s._out.write(c(fmt, *args))
127 return f 152 return f
@@ -129,6 +154,7 @@ class Coloring(object):
129 def nofmt_printer(self, opt=None, fg=None, bg=None, attr=None): 154 def nofmt_printer(self, opt=None, fg=None, bg=None, attr=None):
130 s = self 155 s = self
131 c = self.nofmt_colorer(opt, fg, bg, attr) 156 c = self.nofmt_colorer(opt, fg, bg, attr)
157
132 def f(fmt): 158 def f(fmt):
133 s._out.write(c(fmt)) 159 s._out.write(c(fmt))
134 return f 160 return f
@@ -136,11 +162,13 @@ class Coloring(object):
136 def colorer(self, opt=None, fg=None, bg=None, attr=None): 162 def colorer(self, opt=None, fg=None, bg=None, attr=None):
137 if self._on: 163 if self._on:
138 c = self._parse(opt, fg, bg, attr) 164 c = self._parse(opt, fg, bg, attr)
165
139 def f(fmt, *args): 166 def f(fmt, *args):
140 output = fmt % args 167 output = fmt % args
141 return ''.join([c, output, RESET]) 168 return ''.join([c, output, RESET])
142 return f 169 return f
143 else: 170 else:
171
144 def f(fmt, *args): 172 def f(fmt, *args):
145 return fmt % args 173 return fmt % args
146 return f 174 return f
@@ -148,6 +176,7 @@ class Coloring(object):
148 def nofmt_colorer(self, opt=None, fg=None, bg=None, attr=None): 176 def nofmt_colorer(self, opt=None, fg=None, bg=None, attr=None):
149 if self._on: 177 if self._on:
150 c = self._parse(opt, fg, bg, attr) 178 c = self._parse(opt, fg, bg, attr)
179
151 def f(fmt): 180 def f(fmt):
152 return ''.join([c, fmt, RESET]) 181 return ''.join([c, fmt, RESET])
153 return f 182 return f