blob: 4ceab189bf3ccf1fe8797b8708b381d1ac8ee9e8 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass0d24de92012-01-14 15:12:45 +00002# Copyright (c) 2011 The Chromium OS Authors.
3#
Simon Glass0d24de92012-01-14 15:12:45 +00004
5"""Terminal utilities
6
7This module handles terminal interaction including ANSI color codes.
8"""
9
Paul Burtona920a172016-09-27 16:03:50 +010010from __future__ import print_function
11
Simon Glassbbd01432012-12-15 10:42:01 +000012import os
13import sys
14
15# Selection of when we want our output to be colored
16COLOR_IF_TERMINAL, COLOR_ALWAYS, COLOR_NEVER = range(3)
17
Simon Glass3c6c0f82014-09-05 19:00:06 -060018# Initially, we are set up to print to the terminal
19print_test_mode = False
20print_test_list = []
21
22class 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
39def 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 Burtona920a172016-09-27 16:03:50 +010056 print(text, end='')
Simon Glass3c6c0f82014-09-05 19:00:06 -060057 if newline:
Paul Burtona920a172016-09-27 16:03:50 +010058 print()
Simon Glass8b4919e2016-09-18 16:48:30 -060059 else:
60 sys.stdout.flush()
Simon Glass3c6c0f82014-09-05 19:00:06 -060061
62def SetPrintTestMode():
63 """Go into test mode, where all printing is recorded"""
64 global print_test_mode
65
66 print_test_mode = True
67
68def 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
80def EchoPrintTestLines():
81 """Print out the text lines collected"""
82 for line in print_test_list:
83 if line.colour:
84 col = Color()
Paul Burtona920a172016-09-27 16:03:50 +010085 print(col.Color(line.colour, line.text), end='')
Simon Glass3c6c0f82014-09-05 19:00:06 -060086 else:
Paul Burtona920a172016-09-27 16:03:50 +010087 print(line.text, end='')
Simon Glass3c6c0f82014-09-05 19:00:06 -060088 if line.newline:
Paul Burtona920a172016-09-27 16:03:50 +010089 print()
Simon Glass3c6c0f82014-09-05 19:00:06 -060090
91
Simon Glass0d24de92012-01-14 15:12:45 +000092class Color(object):
Simon Glass6ba57372014-08-28 09:43:34 -060093 """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 Glass0d24de92012-01-14 15:12:45 +0000100
Simon Glass6ba57372014-08-28 09:43:34 -0600101 def __init__(self, colored=COLOR_IF_TERMINAL):
102 """Create a new Color object, optionally disabling color output.
Simon Glass0d24de92012-01-14 15:12:45 +0000103
Simon Glass6ba57372014-08-28 09:43:34 -0600104 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 Glasse752edc2014-08-28 09:43:35 -0600108 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 Glass0d24de92012-01-14 15:12:45 +0000114
Simon Glass6ba57372014-08-28 09:43:34 -0600115 def Start(self, color, bright=True):
116 """Returns a start color code.
Simon Glass0d24de92012-01-14 15:12:45 +0000117
Simon Glass6ba57372014-08-28 09:43:34 -0600118 Args:
119 color: Color to use, .e.g BLACK, RED, etc.
Simon Glass0d24de92012-01-14 15:12:45 +0000120
Simon Glass6ba57372014-08-28 09:43:34 -0600121 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 Glass0d24de92012-01-14 15:12:45 +0000129
Simon Glass6ba57372014-08-28 09:43:34 -0600130 def Stop(self):
131 """Retruns a stop color code.
Simon Glass0d24de92012-01-14 15:12:45 +0000132
Simon Glass6ba57372014-08-28 09:43:34 -0600133 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 Glass0d24de92012-01-14 15:12:45 +0000140
Simon Glass6ba57372014-08-28 09:43:34 -0600141 def Color(self, color, text, bright=True):
142 """Returns text with conditionally added color escape sequences.
Simon Glass0d24de92012-01-14 15:12:45 +0000143
Simon Glass6ba57372014-08-28 09:43:34 -0600144 Keyword arguments:
145 color: Text color -- one of the color constants defined in this
146 class.
147 text: The text to color.
Simon Glass0d24de92012-01-14 15:12:45 +0000148
Simon Glass6ba57372014-08-28 09:43:34 -0600149 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