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 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 20 | COLORS = { |
| 21 | 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 | } |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 32 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 33 | ATTRS = {None: -1, "bold": 1, "dim": 2, "ul": 4, "blink": 5, "reverse": 7} |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 34 | |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 35 | RESET = "\033[m" |
| 36 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 37 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 38 | def is_color(s): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 39 | return s in COLORS |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 40 | |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 41 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 42 | def is_attr(s): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 43 | return s in ATTRS |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 44 | |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 45 | |
| 46 | def _Color(fg=None, bg=None, attr=None): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 47 | fg = COLORS[fg] |
| 48 | bg = COLORS[bg] |
| 49 | attr = ATTRS[attr] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 50 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 51 | if attr >= 0 or fg >= 0 or bg >= 0: |
| 52 | need_sep = False |
| 53 | code = "\033[" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 55 | if attr >= 0: |
| 56 | code += chr(ord("0") + attr) |
| 57 | need_sep = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 58 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 59 | if fg >= 0: |
| 60 | if need_sep: |
| 61 | code += ";" |
| 62 | need_sep = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 63 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 64 | if fg < 8: |
| 65 | code += "3%c" % (ord("0") + fg) |
| 66 | else: |
| 67 | code += "38;5;%d" % fg |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 68 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 69 | if bg >= 0: |
| 70 | if need_sep: |
| 71 | code += ";" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 72 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 73 | if bg < 8: |
| 74 | code += "4%c" % (ord("0") + bg) |
| 75 | else: |
| 76 | code += "48;5;%d" % bg |
| 77 | code += "m" |
| 78 | else: |
| 79 | code = "" |
| 80 | return code |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 81 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 82 | |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 83 | DEFAULT = None |
| 84 | |
Anthony King | bdf7ed2 | 2015-03-28 21:10:17 +0000 | [diff] [blame] | 85 | |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 86 | def SetDefaultColoring(state): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 87 | """Set coloring behavior to |state|. |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 88 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 89 | This is useful for overriding config options via the command line. |
| 90 | """ |
| 91 | if state is None: |
| 92 | # Leave it alone -- return quick! |
| 93 | return |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 94 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 95 | global DEFAULT |
| 96 | state = state.lower() |
| 97 | if state in ("auto",): |
| 98 | DEFAULT = state |
| 99 | elif state in ("always", "yes", "true", True): |
| 100 | DEFAULT = "always" |
| 101 | elif state in ("never", "no", "false", False): |
| 102 | DEFAULT = "never" |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 103 | |
| 104 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 105 | class Coloring(object): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 106 | def __init__(self, config, section_type): |
| 107 | self._section = "color.%s" % section_type |
| 108 | self._config = config |
| 109 | self._out = sys.stdout |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 110 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 111 | on = DEFAULT |
| 112 | if on is None: |
| 113 | on = self._config.GetString(self._section) |
| 114 | if on is None: |
| 115 | on = self._config.GetString("color.ui") |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 116 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 117 | if on == "auto": |
| 118 | if pager.active or os.isatty(1): |
| 119 | self._on = True |
| 120 | else: |
| 121 | self._on = False |
| 122 | elif on in ("true", "always"): |
| 123 | self._on = True |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 124 | else: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 125 | self._on = False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 126 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 127 | def redirect(self, out): |
| 128 | self._out = out |
| 129 | |
| 130 | @property |
| 131 | def is_on(self): |
| 132 | return self._on |
| 133 | |
| 134 | def write(self, fmt, *args): |
| 135 | self._out.write(fmt % args) |
| 136 | |
| 137 | def flush(self): |
| 138 | self._out.flush() |
| 139 | |
| 140 | def nl(self): |
| 141 | self._out.write("\n") |
| 142 | |
| 143 | def printer(self, opt=None, fg=None, bg=None, attr=None): |
| 144 | s = self |
| 145 | c = self.colorer(opt, fg, bg, attr) |
| 146 | |
| 147 | def f(fmt, *args): |
| 148 | s._out.write(c(fmt, *args)) |
| 149 | |
| 150 | return f |
| 151 | |
| 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 | |
| 156 | def f(fmt): |
| 157 | s._out.write(c(fmt)) |
| 158 | |
| 159 | return f |
| 160 | |
| 161 | def colorer(self, opt=None, fg=None, bg=None, attr=None): |
| 162 | if self._on: |
| 163 | c = self._parse(opt, fg, bg, attr) |
| 164 | |
| 165 | def f(fmt, *args): |
| 166 | output = fmt % args |
| 167 | return "".join([c, output, RESET]) |
| 168 | |
| 169 | return f |
| 170 | else: |
| 171 | |
| 172 | def f(fmt, *args): |
| 173 | return fmt % args |
| 174 | |
| 175 | return f |
| 176 | |
| 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) |
| 180 | |
| 181 | def f(fmt): |
| 182 | return "".join([c, fmt, RESET]) |
| 183 | |
| 184 | return f |
| 185 | else: |
| 186 | |
| 187 | def f(fmt): |
| 188 | return fmt |
| 189 | |
| 190 | return f |
| 191 | |
| 192 | def _parse(self, opt, fg, bg, attr): |
| 193 | if not opt: |
| 194 | return _Color(fg, bg, attr) |
| 195 | |
| 196 | v = self._config.GetString("%s.%s" % (self._section, opt)) |
| 197 | if v is None: |
| 198 | return _Color(fg, bg, attr) |
| 199 | |
| 200 | v = v.strip().lower() |
| 201 | if v == "reset": |
| 202 | return RESET |
| 203 | elif v == "": |
| 204 | return _Color(fg, bg, attr) |
| 205 | |
| 206 | have_fg = False |
| 207 | for a in v.split(" "): |
| 208 | if is_color(a): |
| 209 | if have_fg: |
| 210 | bg = a |
| 211 | else: |
| 212 | fg = a |
| 213 | elif is_attr(a): |
| 214 | attr = a |
| 215 | |
| 216 | return _Color(fg, bg, attr) |