22
22
# Controls when ANSI style style sequences are allowed in output
23
23
allow_style = STYLE_TERMINAL
24
24
25
- # Regular expression to match ANSI style sequences
26
- # This matches: colorama.ansi.CSI + 0 or more digits + m
27
- ANSI_STYLE_RE = re .compile (r'\033\[[0-9]*m' )
25
+ # Regular expression to match ANSI style sequences (including 8-bit and 24-bit colors)
26
+ ANSI_STYLE_RE = re .compile (r'\x1b\[[^m]*m' )
28
27
29
28
# Foreground color presets
30
29
FG_COLORS = {
72
71
BG_RESET = BG_COLORS ['reset' ]
73
72
RESET_ALL = Style .RESET_ALL
74
73
75
- BRIGHT = Style .BRIGHT
76
- NORMAL = Style .NORMAL
74
+ # Text intensities
75
+ INTENSITY_BRIGHT = Style .BRIGHT
76
+ INTENSITY_DIM = Style .DIM
77
+ INTENSITY_NORMAL = Style .NORMAL
77
78
78
79
# ANSI style sequences not provided by colorama
79
80
UNDERLINE_ENABLE = colorama .ansi .code_to_chars (4 )
@@ -139,7 +140,8 @@ def bg_lookup(bg_name: str) -> str:
139
140
return ansi_escape
140
141
141
142
142
- def style (text : Any , * , fg : str = '' , bg : str = '' , bold : bool = False , underline : bool = False ) -> str :
143
+ def style (text : Any , * , fg : str = '' , bg : str = '' , bold : bool = False ,
144
+ dim : bool = False , underline : bool = False ) -> str :
143
145
"""
144
146
Apply ANSI colors and/or styles to a string and return it.
145
147
The styling is self contained which means that at the end of the string reset code(s) are issued
@@ -148,7 +150,8 @@ def style(text: Any, *, fg: str = '', bg: str = '', bold: bool = False, underlin
148
150
:param text: Any object compatible with str.format()
149
151
:param fg: foreground color. Relies on `fg_lookup()` to retrieve ANSI escape based on name. Defaults to no color.
150
152
:param bg: background color. Relies on `bg_lookup()` to retrieve ANSI escape based on name. Defaults to no color.
151
- :param bold: apply the bold style if True. Defaults to False.
153
+ :param bold: apply the bold style if True. Can be combined with dim. Defaults to False.
154
+ :param dim: apply the dim style if True. Can be combined with bold. Defaults to False.
152
155
:param underline: apply the underline style if True. Defaults to False.
153
156
:return: the stylized string
154
157
"""
@@ -171,8 +174,12 @@ def style(text: Any, *, fg: str = '', bg: str = '', bold: bool = False, underlin
171
174
removals .append (BG_RESET )
172
175
173
176
if bold :
174
- additions .append (Style .BRIGHT )
175
- removals .append (Style .NORMAL )
177
+ additions .append (INTENSITY_BRIGHT )
178
+ removals .append (INTENSITY_NORMAL )
179
+
180
+ if dim :
181
+ additions .append (INTENSITY_DIM )
182
+ removals .append (INTENSITY_NORMAL )
176
183
177
184
if underline :
178
185
additions .append (UNDERLINE_ENABLE )
0 commit comments