@@ -37,7 +37,7 @@ class MessageStyle(NamedTuple):
37
37
style : tuple [str , ...] = ()
38
38
"""Tuple of style strings (see `ANSI_COLORS` for available values)."""
39
39
40
- def _get_ansi_code (self ) -> str :
40
+ def __get_ansi_code (self ) -> str :
41
41
"""Return ANSI escape code corresponding to color and style.
42
42
43
43
:raise KeyError: if a nonexistent color or style identifier is given
@@ -55,6 +55,16 @@ def _get_ansi_code(self) -> str:
55
55
return ANSI_PREFIX + ";" .join (ansi_code ) + ANSI_END
56
56
return ""
57
57
58
+ def _colorize_ansi (self , msg : str ) -> str :
59
+ if self .color is None and len (self .style ) == 0 :
60
+ # If both color and style are not defined, then leave the text as is.
61
+ return msg
62
+ escape_code = self .__get_ansi_code ()
63
+ # If invalid (or unknown) color, don't wrap msg with ANSI codes
64
+ if escape_code :
65
+ return f"{ escape_code } { msg } { ANSI_RESET } "
66
+ return msg
67
+
58
68
59
69
ColorMappingDict = Dict [str , MessageStyle ]
60
70
@@ -90,14 +100,7 @@ def _get_ansi_code(self) -> str:
90
100
91
101
def colorize_ansi (msg : str , msg_style : MessageStyle ) -> str :
92
102
"""Colorize message by wrapping it with ANSI escape codes."""
93
- if msg_style .color is None and len (msg_style .style ) == 0 :
94
- # If both color and style are not defined, then leave the text as is.
95
- return msg
96
- escape_code = msg_style ._get_ansi_code ()
97
- # If invalid (or unknown) color, don't wrap msg with ANSI codes
98
- if escape_code :
99
- return f"{ escape_code } { msg } { ANSI_RESET } "
100
- return msg
103
+ return msg_style ._colorize_ansi (msg )
101
104
102
105
103
106
def make_header (msg : Message ) -> str :
0 commit comments