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 |
| 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 | |
| 22 | class PrintLine: |
| 23 | """A line of text output |
| 24 | |
| 25 | Members: |
| 26 | text: Text line that was printed |
| 27 | newline: True to output a newline after the text |
| 28 | colour: Text colour to use |
| 29 | """ |
| 30 | def __init__(self, text, newline, colour): |
| 31 | self.text = text |
| 32 | self.newline = newline |
| 33 | self.colour = colour |
| 34 | |
| 35 | def __str__(self): |
| 36 | return 'newline=%s, colour=%s, text=%s' % (self.newline, self.colour, |
| 37 | self.text) |
| 38 | |
| 39 | def Print(text='', newline=True, colour=None): |
| 40 | """Handle a line of output to the terminal. |
| 41 | |
| 42 | In test mode this is recorded in a list. Otherwise it is output to the |
| 43 | terminal. |
| 44 | |
| 45 | Args: |
| 46 | text: Text to print |
| 47 | newline: True to add a new line at the end of the text |
| 48 | colour: Colour to use for the text |
| 49 | """ |
| 50 | if print_test_mode: |
| 51 | print_test_list.append(PrintLine(text, newline, colour)) |
| 52 | else: |
| 53 | if colour: |
| 54 | col = Color() |
| 55 | text = col.Color(colour, text) |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 56 | if newline: |
Simon Glass | 82e4c64 | 2020-04-09 15:08:39 -0600 | [diff] [blame^] | 57 | print(text) |
Simon Glass | 9c45a4e | 2016-09-18 16:48:30 -0600 | [diff] [blame] | 58 | else: |
Simon Glass | 82e4c64 | 2020-04-09 15:08:39 -0600 | [diff] [blame^] | 59 | print(text, end='', flush=True) |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 60 | |
| 61 | def SetPrintTestMode(): |
| 62 | """Go into test mode, where all printing is recorded""" |
| 63 | global print_test_mode |
| 64 | |
| 65 | print_test_mode = True |
| 66 | |
| 67 | def GetPrintTestLines(): |
| 68 | """Get a list of all lines output through Print() |
| 69 | |
| 70 | Returns: |
| 71 | A list of PrintLine objects |
| 72 | """ |
| 73 | global print_test_list |
| 74 | |
| 75 | ret = print_test_list |
| 76 | print_test_list = [] |
| 77 | return ret |
| 78 | |
| 79 | def EchoPrintTestLines(): |
| 80 | """Print out the text lines collected""" |
| 81 | for line in print_test_list: |
| 82 | if line.colour: |
| 83 | col = Color() |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 84 | print(col.Color(line.colour, line.text), end='') |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 85 | else: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 86 | print(line.text, end='') |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 87 | if line.newline: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 88 | print() |
Simon Glass | fb35f9f | 2014-09-05 19:00:06 -0600 | [diff] [blame] | 89 | |
| 90 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 91 | class Color(object): |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 92 | """Conditionally wraps text in ANSI color escape sequences.""" |
| 93 | BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) |
| 94 | BOLD = -1 |
| 95 | BRIGHT_START = '\033[1;%dm' |
| 96 | NORMAL_START = '\033[22;%dm' |
| 97 | BOLD_START = '\033[1m' |
| 98 | RESET = '\033[0m' |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 99 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 100 | def __init__(self, colored=COLOR_IF_TERMINAL): |
| 101 | """Create a new Color object, optionally disabling color output. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 102 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 103 | Args: |
| 104 | enabled: True if color output should be enabled. If False then this |
| 105 | class will not add color codes at all. |
| 106 | """ |
Simon Glass | b0cd341 | 2014-08-28 09:43:35 -0600 | [diff] [blame] | 107 | try: |
| 108 | self._enabled = (colored == COLOR_ALWAYS or |
| 109 | (colored == COLOR_IF_TERMINAL and |
| 110 | os.isatty(sys.stdout.fileno()))) |
| 111 | except: |
| 112 | self._enabled = False |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 113 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 114 | def Start(self, color, bright=True): |
| 115 | """Returns a start color code. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 116 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 117 | Args: |
| 118 | color: Color to use, .e.g BLACK, RED, etc. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 119 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 120 | Returns: |
| 121 | If color is enabled, returns an ANSI sequence to start the given |
| 122 | color, otherwise returns empty string |
| 123 | """ |
| 124 | if self._enabled: |
| 125 | base = self.BRIGHT_START if bright else self.NORMAL_START |
| 126 | return base % (color + 30) |
| 127 | return '' |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 128 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 129 | def Stop(self): |
Anatolij Gustschin | f2bcb32 | 2019-10-27 17:55:04 +0100 | [diff] [blame] | 130 | """Returns a stop color code. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 131 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 132 | Returns: |
| 133 | If color is enabled, returns an ANSI color reset sequence, |
| 134 | otherwise returns empty string |
| 135 | """ |
| 136 | if self._enabled: |
| 137 | return self.RESET |
| 138 | return '' |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 139 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 140 | def Color(self, color, text, bright=True): |
| 141 | """Returns text with conditionally added color escape sequences. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 142 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 143 | Keyword arguments: |
| 144 | color: Text color -- one of the color constants defined in this |
| 145 | class. |
| 146 | text: The text to color. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 147 | |
Simon Glass | 381fad8 | 2014-08-28 09:43:34 -0600 | [diff] [blame] | 148 | Returns: |
| 149 | If self._enabled is False, returns the original text. If it's True, |
| 150 | returns text with color escape sequences based on the value of |
| 151 | color. |
| 152 | """ |
| 153 | if not self._enabled: |
| 154 | return text |
| 155 | if color == self.BOLD: |
| 156 | start = self.BOLD_START |
| 157 | else: |
| 158 | base = self.BRIGHT_START if bright else self.NORMAL_START |
| 159 | start = base % (color + 30) |
| 160 | return start + text + self.RESET |