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 | |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 10 | from __future__ import print_function |
| 11 | |
Simon Glass | a9f7edb | 2012-12-15 10:42:01 +0000 | [diff] [blame] | 12 | import os |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame^] | 13 | import re |
Simon Glass | a9f7edb | 2012-12-15 10:42:01 +0000 | [diff] [blame] | 14 | import sys |
| 15 | |
| 16 | # Selection of when we want our output to be colored |
| 17 | COLOR_IF_TERMINAL, COLOR_ALWAYS, COLOR_NEVER = range(3) |
| 18 | |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 19 | # Initially, we are set up to print to the terminal |
| 20 | print_test_mode = False |
| 21 | print_test_list = [] |
| 22 | |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame^] | 23 | # The length of the last line printed without a newline |
| 24 | last_print_len = None |
| 25 | |
| 26 | # credit: |
| 27 | # stackoverflow.com/questions/14693701/how-can-i-remove-the-ansi-escape-sequences-from-a-string-in-python |
| 28 | ansi_escape = re.compile(r'\x1b(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') |
| 29 | |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 30 | class PrintLine: |
| 31 | """A line of text output |
| 32 | |
| 33 | Members: |
| 34 | text: Text line that was printed |
| 35 | newline: True to output a newline after the text |
| 36 | colour: Text colour to use |
| 37 | """ |
| 38 | def __init__(self, text, newline, colour): |
| 39 | self.text = text |
| 40 | self.newline = newline |
| 41 | self.colour = colour |
| 42 | |
| 43 | def __str__(self): |
| 44 | return 'newline=%s, colour=%s, text=%s' % (self.newline, self.colour, |
| 45 | self.text) |
| 46 | |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame^] | 47 | def CalcAsciiLen(text): |
| 48 | """Calculate the length of a string, ignoring any ANSI sequences |
| 49 | |
| 50 | Args: |
| 51 | text: Text to check |
| 52 | |
| 53 | Returns: |
| 54 | Length of text, after skipping ANSI sequences |
| 55 | |
| 56 | >>> col = Color(COLOR_ALWAYS) |
| 57 | >>> text = col.Color(Color.RED, 'abc') |
| 58 | >>> len(text) |
| 59 | 14 |
| 60 | >>> CalcAsciiLen(text) |
| 61 | 3 |
| 62 | >>> |
| 63 | >>> text += 'def' |
| 64 | >>> CalcAsciiLen(text) |
| 65 | 6 |
| 66 | >>> text += col.Color(Color.RED, 'abc') |
| 67 | >>> CalcAsciiLen(text) |
| 68 | 9 |
| 69 | """ |
| 70 | result = ansi_escape.sub('', text) |
| 71 | return len(result) |
| 72 | |
| 73 | |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 74 | def Print(text='', newline=True, colour=None): |
| 75 | """Handle a line of output to the terminal. |
| 76 | |
| 77 | In test mode this is recorded in a list. Otherwise it is output to the |
| 78 | terminal. |
| 79 | |
| 80 | Args: |
| 81 | text: Text to print |
| 82 | newline: True to add a new line at the end of the text |
| 83 | colour: Colour to use for the text |
| 84 | """ |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame^] | 85 | global last_print_len |
| 86 | |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 87 | if print_test_mode: |
| 88 | print_test_list.append(PrintLine(text, newline, colour)) |
| 89 | else: |
| 90 | if colour: |
| 91 | col = Color() |
| 92 | text = col.Color(colour, text) |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 93 | if newline: |
Simon Glass | 82e4c64 | 2020-04-09 15:08:39 -0600 | [diff] [blame] | 94 | print(text) |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame^] | 95 | last_print_len = None |
Simon Glass | 9c45a4e | 2016-09-18 16:48:30 -0600 | [diff] [blame] | 96 | else: |
Simon Glass | 82e4c64 | 2020-04-09 15:08:39 -0600 | [diff] [blame] | 97 | print(text, end='', flush=True) |
Simon Glass | 5f9325d | 2020-04-09 15:08:40 -0600 | [diff] [blame^] | 98 | last_print_len = CalcAsciiLen(text) |
| 99 | |
| 100 | def PrintClear(): |
| 101 | """Clear a previously line that was printed with no newline""" |
| 102 | global last_print_len |
| 103 | |
| 104 | if last_print_len: |
| 105 | print('\r%s\r' % (' '* last_print_len), end='', flush=True) |
| 106 | last_print_len = None |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 107 | |
| 108 | def SetPrintTestMode(): |
| 109 | """Go into test mode, where all printing is recorded""" |
| 110 | global print_test_mode |
| 111 | |
| 112 | print_test_mode = True |
| 113 | |
| 114 | def GetPrintTestLines(): |
| 115 | """Get a list of all lines output through Print() |
| 116 | |
| 117 | Returns: |
| 118 | A list of PrintLine objects |
| 119 | """ |
| 120 | global print_test_list |
| 121 | |
| 122 | ret = print_test_list |
| 123 | print_test_list = [] |
| 124 | return ret |
| 125 | |
| 126 | def EchoPrintTestLines(): |
| 127 | """Print out the text lines collected""" |
| 128 | for line in print_test_list: |
| 129 | if line.colour: |
| 130 | col = Color() |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 131 | print(col.Color(line.colour, line.text), end='') |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 132 | else: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 133 | print(line.text, end='') |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 134 | if line.newline: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 135 | print() |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 136 | |
| 137 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 138 | class Color(object): |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 139 | """Conditionally wraps text in ANSI color escape sequences.""" |
| 140 | BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) |
| 141 | BOLD = -1 |
| 142 | BRIGHT_START = '\033[1;%dm' |
| 143 | NORMAL_START = '\033[22;%dm' |
| 144 | BOLD_START = '\033[1m' |
| 145 | RESET = '\033[0m' |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 146 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 147 | def __init__(self, colored=COLOR_IF_TERMINAL): |
| 148 | """Create a new Color object, optionally disabling color output. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 149 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 150 | Args: |
| 151 | enabled: True if color output should be enabled. If False then this |
| 152 | class will not add color codes at all. |
| 153 | """ |
Simon Glass | b0cd341 | 2014-08-28 09:43:35 -0600 | [diff] [blame] | 154 | try: |
| 155 | self._enabled = (colored == COLOR_ALWAYS or |
| 156 | (colored == COLOR_IF_TERMINAL and |
| 157 | os.isatty(sys.stdout.fileno()))) |
| 158 | except: |
| 159 | self._enabled = False |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 160 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 161 | def Start(self, color, bright=True): |
| 162 | """Returns a start color code. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 163 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 164 | Args: |
| 165 | color: Color to use, .e.g BLACK, RED, etc. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 166 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 167 | Returns: |
| 168 | If color is enabled, returns an ANSI sequence to start the given |
| 169 | color, otherwise returns empty string |
| 170 | """ |
| 171 | if self._enabled: |
| 172 | base = self.BRIGHT_START if bright else self.NORMAL_START |
| 173 | return base % (color + 30) |
| 174 | return '' |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 175 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 176 | def Stop(self): |
Anatolij Gustschin | f2bcb32 | 2019-10-27 17:55:04 +0100 | [diff] [blame] | 177 | """Returns a stop color code. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 178 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 179 | Returns: |
| 180 | If color is enabled, returns an ANSI color reset sequence, |
| 181 | otherwise returns empty string |
| 182 | """ |
| 183 | if self._enabled: |
| 184 | return self.RESET |
| 185 | return '' |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 186 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 187 | def Color(self, color, text, bright=True): |
| 188 | """Returns text with conditionally added color escape sequences. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 189 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 190 | Keyword arguments: |
| 191 | color: Text color -- one of the color constants defined in this |
| 192 | class. |
| 193 | text: The text to color. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 194 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 195 | Returns: |
| 196 | If self._enabled is False, returns the original text. If it's True, |
| 197 | returns text with color escape sequences based on the value of |
| 198 | color. |
| 199 | """ |
| 200 | if not self._enabled: |
| 201 | return text |
| 202 | if color == self.BOLD: |
| 203 | start = self.BOLD_START |
| 204 | else: |
| 205 | base = self.BRIGHT_START if bright else self.NORMAL_START |
| 206 | start = base % (color + 30) |
| 207 | return start + text + self.RESET |