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.
11 from __future__ import print_function
16 # Selection of when we want our output to be colored
17 COLOR_IF_TERMINAL, COLOR_ALWAYS, COLOR_NEVER = range(3)
19 # Initially, we are set up to print to the terminal
20 print_test_mode = False
24 """A line of text output
27 text: Text line that was printed
28 newline: True to output a newline after the text
29 colour: Text colour to use
31 def __init__(self, text, newline, colour):
33 self.newline = newline
37 return 'newline=%s, colour=%s, text=%s' % (self.newline, self.colour,
40 def Print(text='', newline=True, colour=None):
41 """Handle a line of output to the terminal.
43 In test mode this is recorded in a list. Otherwise it is output to the
48 newline: True to add a new line at the end of the text
49 colour: Colour to use for the text
52 print_test_list.append(PrintLine(text, newline, colour))
56 text = col.Color(colour, text)
63 def SetPrintTestMode():
64 """Go into test mode, where all printing is recorded"""
65 global print_test_mode
67 print_test_mode = True
69 def GetPrintTestLines():
70 """Get a list of all lines output through Print()
73 A list of PrintLine objects
75 global print_test_list
81 def EchoPrintTestLines():
82 """Print out the text lines collected"""
83 for line in print_test_list:
86 print(col.Color(line.colour, line.text), end='')
88 print(line.text, end='')
94 """Conditionally wraps text in ANSI color escape sequences."""
95 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
97 BRIGHT_START = '\033[1;%dm'
98 NORMAL_START = '\033[22;%dm'
99 BOLD_START = '\033[1m'
102 def __init__(self, colored=COLOR_IF_TERMINAL):
103 """Create a new Color object, optionally disabling color output.
106 enabled: True if color output should be enabled. If False then this
107 class will not add color codes at all.
110 self._enabled = (colored == COLOR_ALWAYS or
111 (colored == COLOR_IF_TERMINAL and
112 os.isatty(sys.stdout.fileno())))
114 self._enabled = False
116 def Start(self, color, bright=True):
117 """Returns a start color code.
120 color: Color to use, .e.g BLACK, RED, etc.
123 If color is enabled, returns an ANSI sequence to start the given
124 color, otherwise returns empty string
127 base = self.BRIGHT_START if bright else self.NORMAL_START
128 return base % (color + 30)
132 """Retruns a stop color code.
135 If color is enabled, returns an ANSI color reset sequence,
136 otherwise returns empty string
142 def Color(self, color, text, bright=True):
143 """Returns text with conditionally added color escape sequences.
146 color: Text color -- one of the color constants defined in this
148 text: The text to color.
151 If self._enabled is False, returns the original text. If it's True,
152 returns text with color escape sequences based on the value of
155 if not self._enabled:
157 if color == self.BOLD:
158 start = self.BOLD_START
160 base = self.BRIGHT_START if bright else self.NORMAL_START
161 start = base % (color + 30)
162 return start + text + self.RESET