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