blob: 137265fc81541ebb77726cce41e17c771fd9126d [file] [log] [blame]
Simon Glass0d24de92012-01-14 15:12:45 +00001# Copyright (c) 2011 The Chromium OS Authors.
2#
Wolfgang Denk1a459662013-07-08 09:37:19 +02003# SPDX-License-Identifier: GPL-2.0+
Simon Glass0d24de92012-01-14 15:12:45 +00004#
5
6"""Terminal utilities
7
8This module handles terminal interaction including ANSI color codes.
9"""
10
Paul Burtona920a172016-09-27 16:03:50 +010011from __future__ import print_function
12
Simon Glassbbd01432012-12-15 10:42:01 +000013import os
14import sys
15
16# Selection of when we want our output to be colored
17COLOR_IF_TERMINAL, COLOR_ALWAYS, COLOR_NEVER = range(3)
18
Simon Glass3c6c0f82014-09-05 19:00:06 -060019# Initially, we are set up to print to the terminal
20print_test_mode = False
21print_test_list = []
22
23class PrintLine:
24 """A line of text output
25
26 Members:
27 text: Text line that was printed
28 newline: True to output a newline after the text
29 colour: Text colour to use
30 """
31 def __init__(self, text, newline, colour):
32 self.text = text
33 self.newline = newline
34 self.colour = colour
35
36 def __str__(self):
37 return 'newline=%s, colour=%s, text=%s' % (self.newline, self.colour,
38 self.text)
39
40def Print(text='', newline=True, colour=None):
41 """Handle a line of output to the terminal.
42
43 In test mode this is recorded in a list. Otherwise it is output to the
44 terminal.
45
46 Args:
47 text: Text to print
48 newline: True to add a new line at the end of the text
49 colour: Colour to use for the text
50 """
51 if print_test_mode:
52 print_test_list.append(PrintLine(text, newline, colour))
53 else:
54 if colour:
55 col = Color()
56 text = col.Color(colour, text)
Paul Burtona920a172016-09-27 16:03:50 +010057 print(text, end='')
Simon Glass3c6c0f82014-09-05 19:00:06 -060058 if newline:
Paul Burtona920a172016-09-27 16:03:50 +010059 print()
Simon Glass8b4919e2016-09-18 16:48:30 -060060 else:
61 sys.stdout.flush()
Simon Glass3c6c0f82014-09-05 19:00:06 -060062
63def SetPrintTestMode():
64 """Go into test mode, where all printing is recorded"""
65 global print_test_mode
66
67 print_test_mode = True
68
69def GetPrintTestLines():
70 """Get a list of all lines output through Print()
71
72 Returns:
73 A list of PrintLine objects
74 """
75 global print_test_list
76
77 ret = print_test_list
78 print_test_list = []
79 return ret
80
81def EchoPrintTestLines():
82 """Print out the text lines collected"""
83 for line in print_test_list:
84 if line.colour:
85 col = Color()
Paul Burtona920a172016-09-27 16:03:50 +010086 print(col.Color(line.colour, line.text), end='')
Simon Glass3c6c0f82014-09-05 19:00:06 -060087 else:
Paul Burtona920a172016-09-27 16:03:50 +010088 print(line.text, end='')
Simon Glass3c6c0f82014-09-05 19:00:06 -060089 if line.newline:
Paul Burtona920a172016-09-27 16:03:50 +010090 print()
Simon Glass3c6c0f82014-09-05 19:00:06 -060091
92
Simon Glass0d24de92012-01-14 15:12:45 +000093class Color(object):
Simon Glass6ba57372014-08-28 09:43:34 -060094 """Conditionally wraps text in ANSI color escape sequences."""
95 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
96 BOLD = -1
97 BRIGHT_START = '\033[1;%dm'
98 NORMAL_START = '\033[22;%dm'
99 BOLD_START = '\033[1m'
100 RESET = '\033[0m'
Simon Glass0d24de92012-01-14 15:12:45 +0000101
Simon Glass6ba57372014-08-28 09:43:34 -0600102 def __init__(self, colored=COLOR_IF_TERMINAL):
103 """Create a new Color object, optionally disabling color output.
Simon Glass0d24de92012-01-14 15:12:45 +0000104
Simon Glass6ba57372014-08-28 09:43:34 -0600105 Args:
106 enabled: True if color output should be enabled. If False then this
107 class will not add color codes at all.
108 """
Simon Glasse752edc2014-08-28 09:43:35 -0600109 try:
110 self._enabled = (colored == COLOR_ALWAYS or
111 (colored == COLOR_IF_TERMINAL and
112 os.isatty(sys.stdout.fileno())))
113 except:
114 self._enabled = False
Simon Glass0d24de92012-01-14 15:12:45 +0000115
Simon Glass6ba57372014-08-28 09:43:34 -0600116 def Start(self, color, bright=True):
117 """Returns a start color code.
Simon Glass0d24de92012-01-14 15:12:45 +0000118
Simon Glass6ba57372014-08-28 09:43:34 -0600119 Args:
120 color: Color to use, .e.g BLACK, RED, etc.
Simon Glass0d24de92012-01-14 15:12:45 +0000121
Simon Glass6ba57372014-08-28 09:43:34 -0600122 Returns:
123 If color is enabled, returns an ANSI sequence to start the given
124 color, otherwise returns empty string
125 """
126 if self._enabled:
127 base = self.BRIGHT_START if bright else self.NORMAL_START
128 return base % (color + 30)
129 return ''
Simon Glass0d24de92012-01-14 15:12:45 +0000130
Simon Glass6ba57372014-08-28 09:43:34 -0600131 def Stop(self):
132 """Retruns a stop color code.
Simon Glass0d24de92012-01-14 15:12:45 +0000133
Simon Glass6ba57372014-08-28 09:43:34 -0600134 Returns:
135 If color is enabled, returns an ANSI color reset sequence,
136 otherwise returns empty string
137 """
138 if self._enabled:
139 return self.RESET
140 return ''
Simon Glass0d24de92012-01-14 15:12:45 +0000141
Simon Glass6ba57372014-08-28 09:43:34 -0600142 def Color(self, color, text, bright=True):
143 """Returns text with conditionally added color escape sequences.
Simon Glass0d24de92012-01-14 15:12:45 +0000144
Simon Glass6ba57372014-08-28 09:43:34 -0600145 Keyword arguments:
146 color: Text color -- one of the color constants defined in this
147 class.
148 text: The text to color.
Simon Glass0d24de92012-01-14 15:12:45 +0000149
Simon Glass6ba57372014-08-28 09:43:34 -0600150 Returns:
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
153 color.
154 """
155 if not self._enabled:
156 return text
157 if color == self.BOLD:
158 start = self.BOLD_START
159 else:
160 base = self.BRIGHT_START if bright else self.NORMAL_START
161 start = base % (color + 30)
162 return start + text + self.RESET