[PATCH 05/30] u_boot_pylib: Improve ANSI-colour output with backgrounds
Simon Glass
sjg at chromium.org
Tue Apr 29 15:22:02 CEST 2025
The current implementation does not handle background colours very well:
- It outputs an incorrect code in some cases, leading to wrong colours
- Some functions lack a control for the background
Tidy this up so that background colours can be used in more places.
Signed-off-by: Simon Glass <sjg at chromium.org>
---
tools/u_boot_pylib/terminal.py | 38 ++++++++++++++++++++--------------
1 file changed, 23 insertions(+), 15 deletions(-)
diff --git a/tools/u_boot_pylib/terminal.py b/tools/u_boot_pylib/terminal.py
index 4b9a907a547..6bd790acc3f 100644
--- a/tools/u_boot_pylib/terminal.py
+++ b/tools/u_boot_pylib/terminal.py
@@ -139,7 +139,8 @@ def trim_ascii_len(text, size):
return out
-def tprint(text='', newline=True, colour=None, limit_to_line=False, bright=True):
+def tprint(text='', newline=True, colour=None, limit_to_line=False,
+ bright=True, back=None, col=None):
"""Handle a line of output to the terminal.
In test mode this is recorded in a list. Otherwise it is output to the
@@ -155,9 +156,10 @@ def tprint(text='', newline=True, colour=None, limit_to_line=False, bright=True)
if print_test_mode:
print_test_list.append(PrintLine(text, colour, newline, bright))
else:
- if colour:
- col = Color()
- text = col.build(colour, text, bright=bright)
+ if colour is not None:
+ if not col:
+ col = Color()
+ text = col.build(colour, text, bright=bright, back=back)
if newline:
print(text)
last_print_len = None
@@ -214,9 +216,10 @@ class Color(object):
"""Conditionally wraps text in ANSI color escape sequences."""
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
BOLD = -1
- BRIGHT_START = '\033[1;%dm'
- NORMAL_START = '\033[22;%dm'
+ BRIGHT_START = '\033[1;%d%sm'
+ NORMAL_START = '\033[22;%d%sm'
BOLD_START = '\033[1m'
+ BACK_EXTRA = ';%d'
RESET = '\033[0m'
def __init__(self, colored=COLOR_IF_TERMINAL):
@@ -233,7 +236,14 @@ class Color(object):
except:
self._enabled = False
- def start(self, color, bright=True):
+ def enabled(self):
+ """Check if colour is enabled
+
+ Return: True if enabled, else False
+ """
+ return self._enabled
+
+ def start(self, color, bright=True, back=None):
"""Returns a start color code.
Args:
@@ -244,8 +254,11 @@ class Color(object):
color, otherwise returns empty string
"""
if self._enabled:
+ if color == self.BOLD:
+ return self.BOLD_START
base = self.BRIGHT_START if bright else self.NORMAL_START
- return base % (color + 30)
+ extra = self.BACK_EXTRA % (back + 40) if back else ''
+ return base % (color + 30, extra)
return ''
def stop(self):
@@ -259,7 +272,7 @@ class Color(object):
return self.RESET
return ''
- def build(self, color, text, bright=True):
+ def build(self, color, text, bright=True, back=None):
"""Returns text with conditionally added color escape sequences.
Keyword arguments:
@@ -274,12 +287,7 @@ class Color(object):
"""
if not self._enabled:
return text
- if color == self.BOLD:
- start = self.BOLD_START
- else:
- base = self.BRIGHT_START if bright else self.NORMAL_START
- start = base % (color + 30)
- return start + text + self.RESET
+ return self.start(color, bright, back) + text + self.RESET
# Use this to suppress stdout/stderr output:
--
2.43.0
More information about the U-Boot
mailing list