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): |
| 43 | return s in COLORS |
| 44 | |
| 45 | def is_attr(s): |
| 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): |
| 49 | fg = COLORS[fg] |
| 50 | bg = COLORS[bg] |
| 51 | attr = ATTRS[attr] |
| 52 | |
| 53 | if attr >= 0 or fg >= 0 or bg >= 0: |
| 54 | need_sep = False |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame^] | 55 | code = "\033[" #pylint: disable=W1401 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 56 | |
| 57 | if attr >= 0: |
| 58 | code += chr(ord('0') + attr) |
| 59 | need_sep = True |
| 60 | |
| 61 | if fg >= 0: |
| 62 | if need_sep: |
| 63 | code += ';' |
| 64 | need_sep = True |
| 65 | |
| 66 | if fg < 8: |
| 67 | code += '3%c' % (ord('0') + fg) |
| 68 | else: |
| 69 | code += '38;5;%d' % fg |
| 70 | |
| 71 | if bg >= 0: |
| 72 | if need_sep: |
| 73 | code += ';' |
| 74 | need_sep = True |
| 75 | |
| 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 |
| 84 | |
| 85 | |
| 86 | class Coloring(object): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 87 | def __init__(self, config, section_type): |
| 88 | self._section = 'color.%s' % section_type |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 89 | self._config = config |
| 90 | self._out = sys.stdout |
| 91 | |
| 92 | on = self._config.GetString(self._section) |
| 93 | if on is None: |
| 94 | on = self._config.GetString('color.ui') |
| 95 | |
| 96 | if on == 'auto': |
| 97 | if pager.active or os.isatty(1): |
| 98 | self._on = True |
| 99 | else: |
| 100 | self._on = False |
| 101 | elif on in ('true', 'always'): |
| 102 | self._on = True |
| 103 | else: |
| 104 | self._on = False |
| 105 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 106 | def redirect(self, out): |
| 107 | self._out = out |
| 108 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 109 | @property |
| 110 | def is_on(self): |
| 111 | return self._on |
| 112 | |
| 113 | def write(self, fmt, *args): |
| 114 | self._out.write(fmt % args) |
| 115 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 116 | def flush(self): |
| 117 | self._out.flush() |
| 118 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 119 | def nl(self): |
| 120 | self._out.write('\n') |
| 121 | |
| 122 | def printer(self, opt=None, fg=None, bg=None, attr=None): |
| 123 | s = self |
| 124 | c = self.colorer(opt, fg, bg, attr) |
| 125 | def f(fmt, *args): |
| 126 | s._out.write(c(fmt, *args)) |
| 127 | return f |
| 128 | |
| 129 | def colorer(self, opt=None, fg=None, bg=None, attr=None): |
| 130 | if self._on: |
| 131 | c = self._parse(opt, fg, bg, attr) |
| 132 | def f(fmt, *args): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 133 | output = fmt % args |
| 134 | return ''.join([c, output, RESET]) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 135 | return f |
| 136 | else: |
| 137 | def f(fmt, *args): |
| 138 | return fmt % args |
| 139 | return f |
| 140 | |
| 141 | def _parse(self, opt, fg, bg, attr): |
| 142 | if not opt: |
| 143 | return _Color(fg, bg, attr) |
| 144 | |
| 145 | v = self._config.GetString('%s.%s' % (self._section, opt)) |
| 146 | if v is None: |
| 147 | return _Color(fg, bg, attr) |
| 148 | |
Shawn O. Pearce | a8e98a6 | 2009-02-02 16:17:02 -0800 | [diff] [blame] | 149 | v = v.strip().lower() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 150 | if v == "reset": |
| 151 | return RESET |
| 152 | elif v == '': |
| 153 | return _Color(fg, bg, attr) |
| 154 | |
| 155 | have_fg = False |
| 156 | for a in v.split(' '): |
| 157 | if is_color(a): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 158 | if have_fg: |
| 159 | bg = a |
| 160 | else: |
| 161 | fg = a |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 162 | elif is_attr(a): |
| 163 | attr = a |
| 164 | |
| 165 | return _Color(fg, bg, attr) |