1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2011 The Chromium OS Authors.
7 This module handles terminal interaction including ANSI color codes.
10 from __future__ import print_function
15 # Selection of when we want our output to be colored
16 COLOR_IF_TERMINAL, COLOR_ALWAYS, COLOR_NEVER = range(3)
18 # Initially, we are set up to print to the terminal
19 print_test_mode = False
23 """A line of text output
26 text: Text line that was printed
27 newline: True to output a newline after the text
28 colour: Text colour to use
30 def __init__(self, text, newline, colour):
32 self.newline = newline
36 return 'newline=%s, colour=%s, text=%s' % (self.newline, self.colour,
39 def Print(text='', newline=True, colour=None):
40 """Handle a line of output to the terminal.
42 In test mode this is recorded in a list. Otherwise it is output to the
47 newline: True to add a new line at the end of the text
48 colour: Colour to use for the text
51 print_test_list.append(PrintLine(text, newline, colour))
55 text = col.Color(colour, text)
62 def SetPrintTestMode():
63 """Go into test mode, where all printing is recorded"""
64 global print_test_mode
66 print_test_mode = True
68 def GetPrintTestLines():
69 """Get a list of all lines output through Print()
72 A list of PrintLine objects
74 global print_test_list
80 def EchoPrintTestLines():
81 """Print out the text lines collected"""
82 for line in print_test_list:
85 print(col.Color(line.colour, line.text), end='')
87 print(line.text, end='')
93 """Conditionally wraps text in ANSI color escape sequences."""
94 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
96 BRIGHT_START = '\033[1;%dm'
97 NORMAL_START = '\033[22;%dm'
98 BOLD_START = '\033[1m'
101 def __init__(self, colored=COLOR_IF_TERMINAL):
102 """Create a new Color object, optionally disabling color output.
105 enabled: True if color output should be enabled. If False then this
106 class will not add color codes at all.
109 self._enabled = (colored == COLOR_ALWAYS or
110 (colored == COLOR_IF_TERMINAL and
111 os.isatty(sys.stdout.fileno())))
113 self._enabled = False
115 def Start(self, color, bright=True):
116 """Returns a start color code.
119 color: Color to use, .e.g BLACK, RED, etc.
122 If color is enabled, returns an ANSI sequence to start the given
123 color, otherwise returns empty string
126 base = self.BRIGHT_START if bright else self.NORMAL_START
127 return base % (color + 30)
131 """Retruns a stop color code.
134 If color is enabled, returns an ANSI color reset sequence,
135 otherwise returns empty string
141 def Color(self, color, text, bright=True):
142 """Returns text with conditionally added color escape sequences.
145 color: Text color -- one of the color constants defined in this
147 text: The text to color.
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
154 if not self._enabled:
156 if color == self.BOLD:
157 start = self.BOLD_START
159 base = self.BRIGHT_START if bright else self.NORMAL_START
160 start = base % (color + 30)
161 return start + text + self.RESET