Skip to content
This repository was archived by the owner on Feb 20, 2026. It is now read-only.

Commit 646e7e1

Browse files
committed
Add a __repr__ method to the classes defined in this module
Useful when writing tests for the contents of cli ui messages
1 parent a8afccb commit 646e7e1

1 file changed

Lines changed: 27 additions & 17 deletions

File tree

cli_ui/__init__.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,32 @@ def _setup(**kwargs: ConfigValue) -> None:
7878
class Color:
7979
"""Represent an ANSI escape sequence """
8080

81-
def __init__(self, code: str):
81+
def __init__(self, name: str, code: str):
82+
self.name = name
8283
self.code = code
8384

85+
def __repr__(self) -> str:
86+
return f"Color({self.name})"
87+
8488

8589
# fmt: off
86-
reset = Color(colorama.Style.RESET_ALL)
87-
bold = Color(colorama.Style.BRIGHT)
88-
faint = Color(colorama.Style.DIM)
90+
reset = Color("reset", colorama.Style.RESET_ALL)
91+
bold = Color("bold", colorama.Style.BRIGHT)
92+
faint = Color("dim", colorama.Style.DIM)
8993
# for some reason those are not in colorama
90-
standout = Color('\x1b[3m')
91-
underline = Color('\x1b[4m')
92-
blink = Color('\x1b[5m')
93-
overline = Color('\x1b[6m')
94-
95-
black = Color(colorama.Fore.BLACK)
96-
red = Color(colorama.Fore.RED)
97-
green = Color(colorama.Fore.GREEN)
98-
yellow = Color(colorama.Fore.YELLOW)
99-
blue = Color(colorama.Fore.BLUE)
100-
magenta = Color(colorama.Fore.MAGENTA)
101-
cyan = Color(colorama.Fore.CYAN)
102-
white = Color(colorama.Fore.WHITE)
94+
standout = Color("standout", '\x1b[3m')
95+
underline = Color("underline", '\x1b[4m')
96+
blink = Color("blink", '\x1b[5m')
97+
overline = Color("overline", '\x1b[6m')
98+
99+
black = Color("black", colorama.Fore.BLACK)
100+
red = Color("red", colorama.Fore.RED)
101+
green = Color("green", colorama.Fore.GREEN)
102+
yellow = Color("yellow", colorama.Fore.YELLOW)
103+
blue = Color("blue", colorama.Fore.BLUE)
104+
magenta = Color("magenta", colorama.Fore.MAGENTA)
105+
cyan = Color("cyan", colorama.Fore.CYAN)
106+
white = Color("white", colorama.Fore.WHITE)
103107

104108
# backward compatibility:
105109
brown = yellow # used by ui.warning
@@ -131,6 +135,9 @@ def __init__(self, color: Color, as_unicode: str, as_ascii: str):
131135
def tuple(self) -> Tuple[Token, ...]:
132136
return (reset, self.color, self.as_string, reset)
133137

138+
def __repr__(self) -> str:
139+
return f"UnicodeSequence({repr(self.color)},{self.as_string})"
140+
134141

135142
ellipsis = UnicodeSequence(reset, "…", "...")
136143
check = UnicodeSequence(green, "✓", "ok")
@@ -144,6 +151,9 @@ def __init__(self, as_unicode: str, as_ascii: str):
144151
def tuple(self) -> Tuple[Token, ...]:
145152
return (self.as_string,)
146153

154+
def __repr__(self) -> str:
155+
return f"Symbol({self.as_string})"
156+
147157

148158
def using_colorama() -> bool:
149159
if os.name == "nt":

0 commit comments

Comments
 (0)