Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 2 | # Copyright (c) 2011 The Chromium OS Authors. |
| 3 | # |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 4 | |
| 5 | """Terminal utilities |
| 6 | |
| 7 | This module handles terminal interaction including ANSI color codes. |
| 8 | """ |
| 9 | |
Simon Glass | a9f7edb | 2012-12-15 10:42:01 +0000 | [diff] [blame] | 10 | import os |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame] | 11 | import re |
Simon Glass | bbde053 | 2020-04-09 15:08:41 -0600 | [diff] [blame] | 12 | import shutil |
Simon Glass | a9f7edb | 2012-12-15 10:42:01 +0000 | [diff] [blame] | 13 | import sys |
| 14 | |
| 15 | # Selection of when we want our output to be colored |
| 16 | COLOR_IF_TERMINAL, COLOR_ALWAYS, COLOR_NEVER = range(3) |
| 17 | |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 18 | # Initially, we are set up to print to the terminal |
| 19 | print_test_mode = False |
| 20 | print_test_list = [] |
| 21 | |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame] | 22 | # The length of the last line printed without a newline |
| 23 | last_print_len = None |
| 24 | |
| 25 | # credit: |
| 26 | # stackoverflow.com/questions/14693701/how-can-i-remove-the-ansi-escape-sequences-from-a-string-in-python |
| 27 | ansi_escape = re.compile(r'\x1b(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') |
| 28 | |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 29 | class PrintLine: |
| 30 | """A line of text output |
| 31 | |
| 32 | Members: |
| 33 | text: Text line that was printed |
| 34 | newline: True to output a newline after the text |
| 35 | colour: Text colour to use |
| 36 | """ |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 37 | def __init__(self, text, colour, newline=True, bright=True): |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 38 | self.text = text |
| 39 | self.newline = newline |
| 40 | self.colour = colour |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 41 | self.bright = bright |
| 42 | |
| 43 | def __eq__(self, other): |
| 44 | return (self.text == other.text and |
| 45 | self.newline == other.newline and |
| 46 | self.colour == other.colour and |
| 47 | self.bright == other.bright) |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 48 | |
| 49 | def __str__(self): |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 50 | return ("newline=%s, colour=%s, bright=%d, text='%s'" % |
| 51 | (self.newline, self.colour, self.bright, self.text)) |
| 52 | |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 53 | |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 54 | def calc_ascii_len(text): |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame] | 55 | """Calculate the length of a string, ignoring any ANSI sequences |
| 56 | |
Simon Glass | bbde053 | 2020-04-09 15:08:41 -0600 | [diff] [blame] | 57 | When displayed on a terminal, ANSI sequences don't take any space, so we |
| 58 | need to ignore them when calculating the length of a string. |
| 59 | |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame] | 60 | Args: |
| 61 | text: Text to check |
| 62 | |
| 63 | Returns: |
| 64 | Length of text, after skipping ANSI sequences |
| 65 | |
| 66 | >>> col = Color(COLOR_ALWAYS) |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 67 | >>> text = col.build(Color.RED, 'abc') |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame] | 68 | >>> len(text) |
| 69 | 14 |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 70 | >>> calc_ascii_len(text) |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame] | 71 | 3 |
| 72 | >>> |
| 73 | >>> text += 'def' |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 74 | >>> calc_ascii_len(text) |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame] | 75 | 6 |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 76 | >>> text += col.build(Color.RED, 'abc') |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 77 | >>> calc_ascii_len(text) |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame] | 78 | 9 |
| 79 | """ |
| 80 | result = ansi_escape.sub('', text) |
| 81 | return len(result) |
| 82 | |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 83 | def trim_ascii_len(text, size): |
Simon Glass | bbde053 | 2020-04-09 15:08:41 -0600 | [diff] [blame] | 84 | """Trim a string containing ANSI sequences to the given ASCII length |
| 85 | |
| 86 | The string is trimmed with ANSI sequences being ignored for the length |
| 87 | calculation. |
| 88 | |
| 89 | >>> col = Color(COLOR_ALWAYS) |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 90 | >>> text = col.build(Color.RED, 'abc') |
Simon Glass | bbde053 | 2020-04-09 15:08:41 -0600 | [diff] [blame] | 91 | >>> len(text) |
| 92 | 14 |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 93 | >>> calc_ascii_len(trim_ascii_len(text, 4)) |
Simon Glass | bbde053 | 2020-04-09 15:08:41 -0600 | [diff] [blame] | 94 | 3 |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 95 | >>> calc_ascii_len(trim_ascii_len(text, 2)) |
Simon Glass | bbde053 | 2020-04-09 15:08:41 -0600 | [diff] [blame] | 96 | 2 |
| 97 | >>> text += 'def' |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 98 | >>> calc_ascii_len(trim_ascii_len(text, 4)) |
Simon Glass | bbde053 | 2020-04-09 15:08:41 -0600 | [diff] [blame] | 99 | 4 |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 100 | >>> text += col.build(Color.RED, 'ghi') |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 101 | >>> calc_ascii_len(trim_ascii_len(text, 7)) |
Simon Glass | bbde053 | 2020-04-09 15:08:41 -0600 | [diff] [blame] | 102 | 7 |
| 103 | """ |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 104 | if calc_ascii_len(text) < size: |
Simon Glass | bbde053 | 2020-04-09 15:08:41 -0600 | [diff] [blame] | 105 | return text |
| 106 | pos = 0 |
| 107 | out = '' |
| 108 | left = size |
| 109 | |
| 110 | # Work through each ANSI sequence in turn |
| 111 | for m in ansi_escape.finditer(text): |
| 112 | # Find the text before the sequence and add it to our string, making |
| 113 | # sure it doesn't overflow |
| 114 | before = text[pos:m.start()] |
| 115 | toadd = before[:left] |
| 116 | out += toadd |
| 117 | |
| 118 | # Figure out how much non-ANSI space we have left |
| 119 | left -= len(toadd) |
| 120 | |
| 121 | # Add the ANSI sequence and move to the position immediately after it |
| 122 | out += m.group() |
| 123 | pos = m.start() + len(m.group()) |
| 124 | |
| 125 | # Deal with text after the last ANSI sequence |
| 126 | after = text[pos:] |
| 127 | toadd = after[:left] |
| 128 | out += toadd |
| 129 | |
| 130 | return out |
| 131 | |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame] | 132 | |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 133 | def tprint(text='', newline=True, colour=None, limit_to_line=False, bright=True): |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 134 | """Handle a line of output to the terminal. |
| 135 | |
| 136 | In test mode this is recorded in a list. Otherwise it is output to the |
| 137 | terminal. |
| 138 | |
| 139 | Args: |
| 140 | text: Text to print |
| 141 | newline: True to add a new line at the end of the text |
| 142 | colour: Colour to use for the text |
| 143 | """ |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame] | 144 | global last_print_len |
| 145 | |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 146 | if print_test_mode: |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 147 | print_test_list.append(PrintLine(text, colour, newline, bright)) |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 148 | else: |
| 149 | if colour: |
| 150 | col = Color() |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 151 | text = col.build(colour, text, bright=bright) |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 152 | if newline: |
Simon Glass | 82e4c64 | 2020-04-09 15:08:39 -0600 | [diff] [blame] | 153 | print(text) |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame] | 154 | last_print_len = None |
Simon Glass | 9c45a4e | 2016-09-18 16:48:30 -0600 | [diff] [blame] | 155 | else: |
Simon Glass | bbde053 | 2020-04-09 15:08:41 -0600 | [diff] [blame] | 156 | if limit_to_line: |
| 157 | cols = shutil.get_terminal_size().columns |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 158 | text = trim_ascii_len(text, cols) |
Simon Glass | 82e4c64 | 2020-04-09 15:08:39 -0600 | [diff] [blame] | 159 | print(text, end='', flush=True) |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 160 | last_print_len = calc_ascii_len(text) |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame] | 161 | |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 162 | def print_clear(): |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame] | 163 | """Clear a previously line that was printed with no newline""" |
| 164 | global last_print_len |
| 165 | |
| 166 | if last_print_len: |
| 167 | print('\r%s\r' % (' '* last_print_len), end='', flush=True) |
| 168 | last_print_len = None |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 169 | |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 170 | def set_print_test_mode(enable=True): |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 171 | """Go into test mode, where all printing is recorded""" |
| 172 | global print_test_mode |
| 173 | |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 174 | print_test_mode = enable |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 175 | get_print_test_lines() |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 176 | |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 177 | def get_print_test_lines(): |
| 178 | """Get a list of all lines output through tprint() |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 179 | |
| 180 | Returns: |
| 181 | A list of PrintLine objects |
| 182 | """ |
| 183 | global print_test_list |
| 184 | |
| 185 | ret = print_test_list |
| 186 | print_test_list = [] |
| 187 | return ret |
| 188 | |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 189 | def echo_print_test_lines(): |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 190 | """Print out the text lines collected""" |
| 191 | for line in print_test_list: |
| 192 | if line.colour: |
| 193 | col = Color() |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 194 | print(col.build(line.colour, line.text), end='') |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 195 | else: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 196 | print(line.text, end='') |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 197 | if line.newline: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 198 | print() |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 199 | |
| 200 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 201 | class Color(object): |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 202 | """Conditionally wraps text in ANSI color escape sequences.""" |
| 203 | BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) |
| 204 | BOLD = -1 |
| 205 | BRIGHT_START = '\033[1;%dm' |
| 206 | NORMAL_START = '\033[22;%dm' |
| 207 | BOLD_START = '\033[1m' |
| 208 | RESET = '\033[0m' |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 209 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 210 | def __init__(self, colored=COLOR_IF_TERMINAL): |
| 211 | """Create a new Color object, optionally disabling color output. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 212 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 213 | Args: |
| 214 | enabled: True if color output should be enabled. If False then this |
| 215 | class will not add color codes at all. |
| 216 | """ |
Simon Glass | b0cd341 | 2014-08-28 09:43:35 -0600 | [diff] [blame] | 217 | try: |
| 218 | self._enabled = (colored == COLOR_ALWAYS or |
| 219 | (colored == COLOR_IF_TERMINAL and |
| 220 | os.isatty(sys.stdout.fileno()))) |
| 221 | except: |
| 222 | self._enabled = False |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 223 | |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 224 | def start(self, color, bright=True): |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 225 | """Returns a start color code. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 226 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 227 | Args: |
| 228 | color: Color to use, .e.g BLACK, RED, etc. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 229 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 230 | Returns: |
| 231 | If color is enabled, returns an ANSI sequence to start the given |
| 232 | color, otherwise returns empty string |
| 233 | """ |
| 234 | if self._enabled: |
| 235 | base = self.BRIGHT_START if bright else self.NORMAL_START |
| 236 | return base % (color + 30) |
| 237 | return '' |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 238 | |
Simon Glass | 0281158 | 2022-01-29 14:14:18 -0700 | [diff] [blame] | 239 | def stop(self): |
Anatolij Gustschin | f2bcb32 | 2019-10-27 17:55:04 +0100 | [diff] [blame] | 240 | """Returns a stop color code. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 241 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 242 | Returns: |
| 243 | If color is enabled, returns an ANSI color reset sequence, |
| 244 | otherwise returns empty string |
| 245 | """ |
| 246 | if self._enabled: |
| 247 | return self.RESET |
| 248 | return '' |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 249 | |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 250 | def build(self, color, text, bright=True): |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 251 | """Returns text with conditionally added color escape sequences. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 252 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 253 | Keyword arguments: |
| 254 | color: Text color -- one of the color constants defined in this |
| 255 | class. |
| 256 | text: The text to color. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 257 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 258 | Returns: |
| 259 | If self._enabled is False, returns the original text. If it's True, |
| 260 | returns text with color escape sequences based on the value of |
| 261 | color. |
| 262 | """ |
| 263 | if not self._enabled: |
| 264 | return text |
| 265 | if color == self.BOLD: |
| 266 | start = self.BOLD_START |
| 267 | else: |
| 268 | base = self.BRIGHT_START if bright else self.NORMAL_START |
| 269 | start = base % (color + 30) |
| 270 | return start + text + self.RESET |