blob: af3593eb34e795abaad3da4cafb025d937b1f9ae [file] [log] [blame]
Simon Glass26132882012-01-14 15:12:45 +00001# Copyright (c) 2011 The Chromium OS Authors.
2#
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02003# SPDX-License-Identifier: GPL-2.0+
Simon Glass26132882012-01-14 15:12:45 +00004#
5
6"""Terminal utilities
7
8This module handles terminal interaction including ANSI color codes.
9"""
10
Simon Glassa9f7edb2012-12-15 10:42:01 +000011import os
12import sys
13
14# Selection of when we want our output to be colored
15COLOR_IF_TERMINAL, COLOR_ALWAYS, COLOR_NEVER = range(3)
16
Simon Glassfb35f9f2014-09-05 19:00:06 -060017# Initially, we are set up to print to the terminal
18print_test_mode = False
19print_test_list = []
20
21class PrintLine:
22 """A line of text output
23
24 Members:
25 text: Text line that was printed
26 newline: True to output a newline after the text
27 colour: Text colour to use
28 """
29 def __init__(self, text, newline, colour):
30 self.text = text
31 self.newline = newline
32 self.colour = colour
33
34 def __str__(self):
35 return 'newline=%s, colour=%s, text=%s' % (self.newline, self.colour,
36 self.text)
37
38def Print(text='', newline=True, colour=None):
39 """Handle a line of output to the terminal.
40
41 In test mode this is recorded in a list. Otherwise it is output to the
42 terminal.
43
44 Args:
45 text: Text to print
46 newline: True to add a new line at the end of the text
47 colour: Colour to use for the text
48 """
49 if print_test_mode:
50 print_test_list.append(PrintLine(text, newline, colour))
51 else:
52 if colour:
53 col = Color()
54 text = col.Color(colour, text)
55 print text,
56 if newline:
57 print
Simon Glass9c45a4e2016-09-18 16:48:30 -060058 else:
59 sys.stdout.flush()
Simon Glassfb35f9f2014-09-05 19:00:06 -060060
61def SetPrintTestMode():
62 """Go into test mode, where all printing is recorded"""
63 global print_test_mode
64
65 print_test_mode = True
66
67def GetPrintTestLines():
68 """Get a list of all lines output through Print()
69
70 Returns:
71 A list of PrintLine objects
72 """
73 global print_test_list
74
75 ret = print_test_list
76 print_test_list = []
77 return ret
78
79def EchoPrintTestLines():
80 """Print out the text lines collected"""
81 for line in print_test_list:
82 if line.colour:
83 col = Color()
84 print col.Color(line.colour, line.text),
85 else:
86 print line.text,
87 if line.newline:
88 print
89
90
Simon Glass26132882012-01-14 15:12:45 +000091class Color(object):
Simon Glass381fad82014-08-28 09:43:34 -060092 """Conditionally wraps text in ANSI color escape sequences."""
93 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
94 BOLD = -1
95 BRIGHT_START = '\033[1;%dm'
96 NORMAL_START = '\033[22;%dm'
97 BOLD_START = '\033[1m'
98 RESET = '\033[0m'
Simon Glass26132882012-01-14 15:12:45 +000099
Simon Glass381fad82014-08-28 09:43:34 -0600100 def __init__(self, colored=COLOR_IF_TERMINAL):
101 """Create a new Color object, optionally disabling color output.
Simon Glass26132882012-01-14 15:12:45 +0000102
Simon Glass381fad82014-08-28 09:43:34 -0600103 Args:
104 enabled: True if color output should be enabled. If False then this
105 class will not add color codes at all.
106 """
Simon Glassb0cd3412014-08-28 09:43:35 -0600107 try:
108 self._enabled = (colored == COLOR_ALWAYS or
109 (colored == COLOR_IF_TERMINAL and
110 os.isatty(sys.stdout.fileno())))
111 except:
112 self._enabled = False
Simon Glass26132882012-01-14 15:12:45 +0000113
Simon Glass381fad82014-08-28 09:43:34 -0600114 def Start(self, color, bright=True):
115 """Returns a start color code.
Simon Glass26132882012-01-14 15:12:45 +0000116
Simon Glass381fad82014-08-28 09:43:34 -0600117 Args:
118 color: Color to use, .e.g BLACK, RED, etc.
Simon Glass26132882012-01-14 15:12:45 +0000119
Simon Glass381fad82014-08-28 09:43:34 -0600120 Returns:
121 If color is enabled, returns an ANSI sequence to start the given
122 color, otherwise returns empty string
123 """
124 if self._enabled:
125 base = self.BRIGHT_START if bright else self.NORMAL_START
126 return base % (color + 30)
127 return ''
Simon Glass26132882012-01-14 15:12:45 +0000128
Simon Glass381fad82014-08-28 09:43:34 -0600129 def Stop(self):
130 """Retruns a stop color code.
Simon Glass26132882012-01-14 15:12:45 +0000131
Simon Glass381fad82014-08-28 09:43:34 -0600132 Returns:
133 If color is enabled, returns an ANSI color reset sequence,
134 otherwise returns empty string
135 """
136 if self._enabled:
137 return self.RESET
138 return ''
Simon Glass26132882012-01-14 15:12:45 +0000139
Simon Glass381fad82014-08-28 09:43:34 -0600140 def Color(self, color, text, bright=True):
141 """Returns text with conditionally added color escape sequences.
Simon Glass26132882012-01-14 15:12:45 +0000142
Simon Glass381fad82014-08-28 09:43:34 -0600143 Keyword arguments:
144 color: Text color -- one of the color constants defined in this
145 class.
146 text: The text to color.
Simon Glass26132882012-01-14 15:12:45 +0000147
Simon Glass381fad82014-08-28 09:43:34 -0600148 Returns:
149 If self._enabled is False, returns the original text. If it's True,
150 returns text with color escape sequences based on the value of
151 color.
152 """
153 if not self._enabled:
154 return text
155 if color == self.BOLD:
156 start = self.BOLD_START
157 else:
158 base = self.BRIGHT_START if bright else self.NORMAL_START
159 start = base % (color + 30)
160 return start + text + self.RESET