1 # Copyright (c) 2011 The Chromium OS Authors.
3 # SPDX-License-Identifier: GPL-2.0+
8 This module handles terminal interaction including ANSI color codes.
14 # Selection of when we want our output to be colored
15 COLOR_IF_TERMINAL, COLOR_ALWAYS, COLOR_NEVER = range(3)
17 # Initially, we are set up to print to the terminal
18 print_test_mode = False
22 """A line of text output
25 text: Text line that was printed
26 newline: True to output a newline after the text
27 colour: Text colour to use
29 def __init__(self, text, newline, colour):
31 self.newline = newline
35 return 'newline=%s, colour=%s, text=%s' % (self.newline, self.colour,
38 def Print(text='', newline=True, colour=None):
39 """Handle a line of output to the terminal.
41 In test mode this is recorded in a list. Otherwise it is output to the
46 newline: True to add a new line at the end of the text
47 colour: Colour to use for the text
50 print_test_list.append(PrintLine(text, newline, colour))
54 text = col.Color(colour, text)
59 def SetPrintTestMode():
60 """Go into test mode, where all printing is recorded"""
61 global print_test_mode
63 print_test_mode = True
65 def GetPrintTestLines():
66 """Get a list of all lines output through Print()
69 A list of PrintLine objects
71 global print_test_list
77 def EchoPrintTestLines():
78 """Print out the text lines collected"""
79 for line in print_test_list:
82 print col.Color(line.colour, line.text),
90 """Conditionally wraps text in ANSI color escape sequences."""
91 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
93 BRIGHT_START = '\033[1;%dm'
94 NORMAL_START = '\033[22;%dm'
95 BOLD_START = '\033[1m'
98 def __init__(self, colored=COLOR_IF_TERMINAL):
99 """Create a new Color object, optionally disabling color output.
102 enabled: True if color output should be enabled. If False then this
103 class will not add color codes at all.
106 self._enabled = (colored == COLOR_ALWAYS or
107 (colored == COLOR_IF_TERMINAL and
108 os.isatty(sys.stdout.fileno())))
110 self._enabled = False
112 def Start(self, color, bright=True):
113 """Returns a start color code.
116 color: Color to use, .e.g BLACK, RED, etc.
119 If color is enabled, returns an ANSI sequence to start the given
120 color, otherwise returns empty string
123 base = self.BRIGHT_START if bright else self.NORMAL_START
124 return base % (color + 30)
128 """Retruns a stop color code.
131 If color is enabled, returns an ANSI color reset sequence,
132 otherwise returns empty string
138 def Color(self, color, text, bright=True):
139 """Returns text with conditionally added color escape sequences.
142 color: Text color -- one of the color constants defined in this
144 text: The text to color.
147 If self._enabled is False, returns the original text. If it's True,
148 returns text with color escape sequences based on the value of
151 if not self._enabled:
153 if color == self.BOLD:
154 start = self.BOLD_START
156 base = self.BRIGHT_START if bright else self.NORMAL_START
157 start = base % (color + 30)
158 return start + text + self.RESET