diff options
| -rw-r--r-- | color.py | 48 |
1 files changed, 27 insertions, 21 deletions
| @@ -18,41 +18,43 @@ import sys | |||
| 18 | 18 | ||
| 19 | import pager | 19 | import pager |
| 20 | 20 | ||
| 21 | COLORS = {None :-1, | 21 | COLORS = {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 | ||
| 32 | ATTRS = {None :-1, | 32 | ATTRS = {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 | ||
| 39 | RESET = "\033[m" # pylint: disable=W1401 | 39 | RESET = "\033[m" |
| 40 | # backslash is not anomalous | 40 | |
| 41 | 41 | ||
| 42 | def is_color(s): | 42 | def is_color(s): |
| 43 | return s in COLORS | 43 | return s in COLORS |
| 44 | 44 | ||
| 45 | |||
| 45 | def is_attr(s): | 46 | def is_attr(s): |
| 46 | return s in ATTRS | 47 | return s in ATTRS |
| 47 | 48 | ||
| 48 | def _Color(fg = None, bg = None, attr = None): | 49 | |
| 50 | def _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,9 +83,9 @@ def _Color(fg = None, bg = None, attr = None): | |||
| 82 | code = '' | 83 | code = '' |
| 83 | return code | 84 | return code |
| 84 | 85 | ||
| 85 | |||
| 86 | DEFAULT = None | 86 | DEFAULT = None |
| 87 | 87 | ||
| 88 | |||
| 88 | def SetDefaultColoring(state): | 89 | def SetDefaultColoring(state): |
| 89 | """Set coloring behavior to |state|. | 90 | """Set coloring behavior to |state|. |
| 90 | 91 | ||
| @@ -145,6 +146,7 @@ class Coloring(object): | |||
| 145 | def printer(self, opt=None, fg=None, bg=None, attr=None): | 146 | def printer(self, opt=None, fg=None, bg=None, attr=None): |
| 146 | s = self | 147 | s = self |
| 147 | c = self.colorer(opt, fg, bg, attr) | 148 | c = self.colorer(opt, fg, bg, attr) |
| 149 | |||
| 148 | def f(fmt, *args): | 150 | def f(fmt, *args): |
| 149 | s._out.write(c(fmt, *args)) | 151 | s._out.write(c(fmt, *args)) |
| 150 | return f | 152 | return f |
| @@ -152,6 +154,7 @@ class Coloring(object): | |||
| 152 | 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): |
| 153 | s = self | 155 | s = self |
| 154 | c = self.nofmt_colorer(opt, fg, bg, attr) | 156 | c = self.nofmt_colorer(opt, fg, bg, attr) |
| 157 | |||
| 155 | def f(fmt): | 158 | def f(fmt): |
| 156 | s._out.write(c(fmt)) | 159 | s._out.write(c(fmt)) |
| 157 | return f | 160 | return f |
| @@ -159,11 +162,13 @@ class Coloring(object): | |||
| 159 | def colorer(self, opt=None, fg=None, bg=None, attr=None): | 162 | def colorer(self, opt=None, fg=None, bg=None, attr=None): |
| 160 | if self._on: | 163 | if self._on: |
| 161 | c = self._parse(opt, fg, bg, attr) | 164 | c = self._parse(opt, fg, bg, attr) |
| 165 | |||
| 162 | def f(fmt, *args): | 166 | def f(fmt, *args): |
| 163 | output = fmt % args | 167 | output = fmt % args |
| 164 | return ''.join([c, output, RESET]) | 168 | return ''.join([c, output, RESET]) |
| 165 | return f | 169 | return f |
| 166 | else: | 170 | else: |
| 171 | |||
| 167 | def f(fmt, *args): | 172 | def f(fmt, *args): |
| 168 | return fmt % args | 173 | return fmt % args |
| 169 | return f | 174 | return f |
| @@ -171,6 +176,7 @@ class Coloring(object): | |||
| 171 | 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): |
| 172 | if self._on: | 177 | if self._on: |
| 173 | c = self._parse(opt, fg, bg, attr) | 178 | c = self._parse(opt, fg, bg, attr) |
| 179 | |||
| 174 | def f(fmt): | 180 | def f(fmt): |
| 175 | return ''.join([c, fmt, RESET]) | 181 | return ''.join([c, fmt, RESET]) |
| 176 | return f | 182 | return f |
