The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2008 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | import os |
| 17 | import sys |
| 18 | |
| 19 | import pager |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | |
| 21 | COLORS = {None :-1, |
| 22 | 'normal' :-1, |
| 23 | 'black' : 0, |
| 24 | 'red' : 1, |
| 25 | 'green' : 2, |
| 26 | 'yellow' : 3, |
| 27 | 'blue' : 4, |
| 28 | 'magenta': 5, |
| 29 | 'cyan' : 6, |
| 30 | 'white' : 7} |
| 31 | |
| 32 | ATTRS = {None :-1, |
| 33 | 'bold' : 1, |
| 34 | 'dim' : 2, |
| 35 | 'ul' : 4, |
| 36 | 'blink' : 5, |
| 37 | 'reverse': 7} |
| 38 | |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 39 | RESET = "\033[m" # pylint: disable=W1401 |
| 40 | # backslash is not anomalous |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 41 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 42 | def is_color(s): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 43 | return s in COLORS |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 44 | |
| 45 | def is_attr(s): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 46 | return s in ATTRS |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 47 | |
| 48 | def _Color(fg = None, bg = None, attr = None): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 49 | fg = COLORS[fg] |
| 50 | bg = COLORS[bg] |
| 51 | attr = ATTRS[attr] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 52 | |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 53 | if attr >= 0 or fg >= 0 or bg >= 0: |
| 54 | need_sep = False |
| 55 | code = "\033[" #pylint: disable=W1401 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 56 | |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 57 | if attr >= 0: |
| 58 | code += chr(ord('0') + attr) |
| 59 | need_sep = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 60 | |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 61 | if fg >= 0: |
| 62 | if need_sep: |
| 63 | code += ';' |
| 64 | need_sep = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 65 | |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 66 | if fg < 8: |
| 67 | code += '3%c' % (ord('0') + fg) |
| 68 | else: |
| 69 | code += '38;5;%d' % fg |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 70 | |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 71 | if bg >= 0: |
| 72 | if need_sep: |
| 73 | code += ';' |
| 74 | need_sep = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 75 | |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 76 | if bg < 8: |
| 77 | code += '4%c' % (ord('0') + bg) |
| 78 | else: |
| 79 | code += '48;5;%d' % bg |
| 80 | code += 'm' |
| 81 | else: |
| 82 | code = '' |
| 83 | return code |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 84 | |
| 85 | |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 86 | DEFAULT = None |
| 87 | |
| 88 | def SetDefaultColoring(state): |
| 89 | """Set coloring behavior to |state|. |
| 90 | |
| 91 | This is useful for overriding config options via the command line. |
| 92 | """ |
| 93 | if state is None: |
| 94 | # Leave it alone -- return quick! |
| 95 | return |
| 96 | |
| 97 | global DEFAULT |
| 98 | state = state.lower() |
| 99 | if state in ('auto',): |
| 100 | DEFAULT = state |
| 101 | elif state in ('always', 'yes', 'true', True): |
| 102 | DEFAULT = 'always' |
| 103 | elif state in ('never', 'no', 'false', False): |
| 104 | DEFAULT = 'never' |
| 105 | |
| 106 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 107 | class Coloring(object): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 108 | def __init__(self, config, section_type): |
| 109 | self._section = 'color.%s' % section_type |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 110 | self._config = config |
| 111 | self._out = sys.stdout |
| 112 | |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 113 | on = DEFAULT |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 114 | if on is None: |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 115 | on = self._config.GetString(self._section) |
| 116 | if on is None: |
| 117 | on = self._config.GetString('color.ui') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 118 | |
| 119 | if on == 'auto': |
| 120 | if pager.active or os.isatty(1): |
| 121 | self._on = True |
| 122 | else: |
| 123 | self._on = False |
| 124 | elif on in ('true', 'always'): |
| 125 | self._on = True |
| 126 | else: |
| 127 | self._on = False |
| 128 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 129 | def redirect(self, out): |
| 130 | self._out = out |
| 131 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 132 | @property |
| 133 | def is_on(self): |
| 134 | return self._on |
| 135 | |
| 136 | def write(self, fmt, *args): |
| 137 | self._out.write(fmt % args) |
| 138 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 139 | def flush(self): |
| 140 | self._out.flush() |
| 141 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 142 | def nl(self): |
| 143 | self._out.write('\n') |
| 144 | |
| 145 | def printer(self, opt=None, fg=None, bg=None, attr=None): |
| 146 | s = self |
| 147 | c = self.colorer(opt, fg, bg, attr) |
| 148 | def f(fmt, *args): |
| 149 | s._out.write(c(fmt, *args)) |
| 150 | return f |
| 151 | |
Olof Johansson | b754150 | 2013-02-26 07:36:03 +0100 | [diff] [blame] | 152 | def nofmt_printer(self, opt=None, fg=None, bg=None, attr=None): |
| 153 | s = self |
| 154 | c = self.nofmt_colorer(opt, fg, bg, attr) |
| 155 | def f(fmt): |
| 156 | s._out.write(c(fmt)) |
| 157 | return f |
| 158 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 159 | def colorer(self, opt=None, fg=None, bg=None, attr=None): |
| 160 | if self._on: |
| 161 | c = self._parse(opt, fg, bg, attr) |
| 162 | def f(fmt, *args): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 163 | output = fmt % args |
| 164 | return ''.join([c, output, RESET]) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 165 | return f |
| 166 | else: |
| 167 | def f(fmt, *args): |
| 168 | return fmt % args |
| 169 | return f |
| 170 | |
Olof Johansson | b754150 | 2013-02-26 07:36:03 +0100 | [diff] [blame] | 171 | def nofmt_colorer(self, opt=None, fg=None, bg=None, attr=None): |
| 172 | if self._on: |
| 173 | c = self._parse(opt, fg, bg, attr) |
| 174 | def f(fmt): |
| 175 | return ''.join([c, fmt, RESET]) |
| 176 | return f |
| 177 | else: |
| 178 | def f(fmt): |
| 179 | return fmt |
| 180 | return f |
| 181 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 182 | def _parse(self, opt, fg, bg, attr): |
| 183 | if not opt: |
| 184 | return _Color(fg, bg, attr) |
| 185 | |
| 186 | v = self._config.GetString('%s.%s' % (self._section, opt)) |
| 187 | if v is None: |
| 188 | return _Color(fg, bg, attr) |
| 189 | |
Shawn O. Pearce | a8e98a6 | 2009-02-02 16:17:02 -0800 | [diff] [blame] | 190 | v = v.strip().lower() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 191 | if v == "reset": |
| 192 | return RESET |
| 193 | elif v == '': |
| 194 | return _Color(fg, bg, attr) |
| 195 | |
| 196 | have_fg = False |
| 197 | for a in v.split(' '): |
| 198 | if is_color(a): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 199 | if have_fg: |
| 200 | bg = a |
| 201 | else: |
| 202 | fg = a |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 203 | elif is_attr(a): |
| 204 | attr = a |
| 205 | |
| 206 | return _Color(fg, bg, attr) |