The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # Copyright (C) 2008 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import os |
| 16 | import sys |
| 17 | |
| 18 | import pager |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 20 | COLORS = {None: -1, |
| 21 | 'normal': -1, |
| 22 | 'black': 0, |
| 23 | 'red': 1, |
| 24 | 'green': 2, |
| 25 | 'yellow': 3, |
| 26 | 'blue': 4, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 27 | 'magenta': 5, |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 28 | 'cyan': 6, |
| 29 | 'white': 7} |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 30 | |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 31 | ATTRS = {None: -1, |
| 32 | 'bold': 1, |
| 33 | 'dim': 2, |
| 34 | 'ul': 4, |
| 35 | 'blink': 5, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 36 | 'reverse': 7} |
| 37 | |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 38 | RESET = "\033[m" |
| 39 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 40 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 41 | def is_color(s): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 42 | return s in COLORS |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 43 | |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 44 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 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 | |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 48 | |
| 49 | def _Color(fg=None, bg=None, attr=None): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 50 | fg = COLORS[fg] |
| 51 | bg = COLORS[bg] |
| 52 | attr = ATTRS[attr] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 53 | |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 54 | if attr >= 0 or fg >= 0 or bg >= 0: |
| 55 | need_sep = False |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 56 | code = "\033[" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 57 | |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 58 | if attr >= 0: |
| 59 | code += chr(ord('0') + attr) |
| 60 | need_sep = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 61 | |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 62 | if fg >= 0: |
| 63 | if need_sep: |
| 64 | code += ';' |
| 65 | need_sep = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 66 | |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 67 | if fg < 8: |
| 68 | code += '3%c' % (ord('0') + fg) |
| 69 | else: |
| 70 | code += '38;5;%d' % fg |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 71 | |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 72 | if bg >= 0: |
| 73 | if need_sep: |
| 74 | code += ';' |
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 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [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) |