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 | |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 21 | COLORS = {None: -1, |
| 22 | 'normal': -1, |
| 23 | 'black': 0, |
| 24 | 'red': 1, |
| 25 | 'green': 2, |
| 26 | 'yellow': 3, |
| 27 | 'blue': 4, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 28 | 'magenta': 5, |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 29 | 'cyan': 6, |
| 30 | 'white': 7} |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 31 | |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 32 | ATTRS = {None: -1, |
| 33 | 'bold': 1, |
| 34 | 'dim': 2, |
| 35 | 'ul': 4, |
| 36 | 'blink': 5, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 37 | 'reverse': 7} |
| 38 | |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 39 | RESET = "\033[m" |
| 40 | |
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 | |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 45 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 46 | def is_attr(s): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 47 | return s in ATTRS |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 48 | |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 49 | |
| 50 | def _Color(fg=None, bg=None, attr=None): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 51 | fg = COLORS[fg] |
| 52 | bg = COLORS[bg] |
| 53 | attr = ATTRS[attr] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 55 | if attr >= 0 or fg >= 0 or bg >= 0: |
| 56 | need_sep = False |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 57 | code = "\033[" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 58 | |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 59 | if attr >= 0: |
| 60 | code += chr(ord('0') + attr) |
| 61 | need_sep = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 62 | |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 63 | if fg >= 0: |
| 64 | if need_sep: |
| 65 | code += ';' |
| 66 | need_sep = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 67 | |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 68 | if fg < 8: |
| 69 | code += '3%c' % (ord('0') + fg) |
| 70 | else: |
| 71 | code += '38;5;%d' % fg |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 72 | |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 73 | if bg >= 0: |
| 74 | if need_sep: |
| 75 | code += ';' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 76 | |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 77 | if bg < 8: |
| 78 | code += '4%c' % (ord('0') + bg) |
| 79 | else: |
| 80 | code += '48;5;%d' % bg |
| 81 | code += 'm' |
| 82 | else: |
| 83 | code = '' |
| 84 | return code |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 85 | |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 86 | DEFAULT = None |
| 87 | |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 88 | |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 89 | def 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 | |
| 107 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 108 | class Coloring(object): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 109 | def __init__(self, config, section_type): |
| 110 | self._section = 'color.%s' % section_type |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 111 | self._config = config |
| 112 | self._out = sys.stdout |
| 113 | |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 114 | on = DEFAULT |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 115 | if on is None: |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 116 | on = self._config.GetString(self._section) |
| 117 | if on is None: |
| 118 | on = self._config.GetString('color.ui') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 119 | |
| 120 | if on == 'auto': |
| 121 | if pager.active or os.isatty(1): |
| 122 | self._on = True |
| 123 | else: |
| 124 | self._on = False |
| 125 | elif on in ('true', 'always'): |
| 126 | self._on = True |
| 127 | else: |
| 128 | self._on = False |
| 129 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 130 | def redirect(self, out): |
| 131 | self._out = out |
| 132 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | @property |
| 134 | def is_on(self): |
| 135 | return self._on |
| 136 | |
| 137 | def write(self, fmt, *args): |
| 138 | self._out.write(fmt % args) |
| 139 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 140 | def flush(self): |
| 141 | self._out.flush() |
| 142 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 143 | def nl(self): |
| 144 | self._out.write('\n') |
| 145 | |
| 146 | def printer(self, opt=None, fg=None, bg=None, attr=None): |
| 147 | s = self |
| 148 | c = self.colorer(opt, fg, bg, attr) |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 149 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 150 | def f(fmt, *args): |
| 151 | s._out.write(c(fmt, *args)) |
| 152 | return f |
| 153 | |
Olof Johansson | b754150 | 2013-02-26 07:36:03 +0100 | [diff] [blame] | 154 | def nofmt_printer(self, opt=None, fg=None, bg=None, attr=None): |
| 155 | s = self |
| 156 | c = self.nofmt_colorer(opt, fg, bg, attr) |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 157 | |
Olof Johansson | b754150 | 2013-02-26 07:36:03 +0100 | [diff] [blame] | 158 | def f(fmt): |
| 159 | s._out.write(c(fmt)) |
| 160 | return f |
| 161 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 162 | def colorer(self, opt=None, fg=None, bg=None, attr=None): |
| 163 | if self._on: |
| 164 | c = self._parse(opt, fg, bg, attr) |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 165 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 166 | def f(fmt, *args): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 167 | output = fmt % args |
| 168 | return ''.join([c, output, RESET]) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 169 | return f |
| 170 | else: |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 171 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 172 | def f(fmt, *args): |
| 173 | return fmt % args |
| 174 | return f |
| 175 | |
Olof Johansson | b754150 | 2013-02-26 07:36:03 +0100 | [diff] [blame] | 176 | def nofmt_colorer(self, opt=None, fg=None, bg=None, attr=None): |
| 177 | if self._on: |
| 178 | c = self._parse(opt, fg, bg, attr) |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 179 | |
Olof Johansson | b754150 | 2013-02-26 07:36:03 +0100 | [diff] [blame] | 180 | def f(fmt): |
| 181 | return ''.join([c, fmt, RESET]) |
| 182 | return f |
| 183 | else: |
| 184 | def f(fmt): |
| 185 | return fmt |
| 186 | return f |
| 187 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 188 | def _parse(self, opt, fg, bg, attr): |
| 189 | if not opt: |
| 190 | return _Color(fg, bg, attr) |
| 191 | |
| 192 | v = self._config.GetString('%s.%s' % (self._section, opt)) |
| 193 | if v is None: |
| 194 | return _Color(fg, bg, attr) |
| 195 | |
Shawn O. Pearce | a8e98a6 | 2009-02-02 16:17:02 -0800 | [diff] [blame] | 196 | v = v.strip().lower() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 197 | if v == "reset": |
| 198 | return RESET |
| 199 | elif v == '': |
| 200 | return _Color(fg, bg, attr) |
| 201 | |
| 202 | have_fg = False |
| 203 | for a in v.split(' '): |
| 204 | if is_color(a): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 205 | if have_fg: |
| 206 | bg = a |
| 207 | else: |
| 208 | fg = a |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 209 | elif is_attr(a): |
| 210 | attr = a |
| 211 | |
| 212 | return _Color(fg, bg, attr) |