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