1
- """Support for ANSI escape sequences which are used for things like applying style to text,
2
- setting the window title, and asynchronous alerts.
1
+ """Support for ANSI escape sequences.
2
+
3
+ These are used for things like applying style to text, setting the window title, and asynchronous alerts.
3
4
"""
4
5
5
6
import functools
@@ -92,6 +93,7 @@ def strip_style(text: str) -> str:
92
93
93
94
def style_aware_wcswidth (text : str ) -> int :
94
95
"""Wrap wcswidth to make it compatible with strings that contain ANSI style sequences.
96
+
95
97
This is intended for single line strings. If text contains a newline, this
96
98
function will return -1. For multiline strings, call widest_line() instead.
97
99
@@ -105,8 +107,10 @@ def style_aware_wcswidth(text: str) -> int:
105
107
106
108
107
109
def widest_line (text : str ) -> int :
108
- """Return the width of the widest line in a multiline string. This wraps style_aware_wcswidth()
109
- so it handles ANSI style sequences and has the same restrictions on non-printable characters.
110
+ """Return the width of the widest line in a multiline string.
111
+
112
+ This wraps style_aware_wcswidth() so it handles ANSI style sequences and has the same
113
+ restrictions on non-printable characters.
110
114
111
115
:param text: the string being measured
112
116
:return: The width of the string when printed to the terminal if no errors occur.
@@ -186,14 +190,16 @@ class AnsiSequence:
186
190
"""Base class to create ANSI sequence strings."""
187
191
188
192
def __add__ (self , other : Any ) -> str :
189
- """Support building an ANSI sequence string when self is the left operand
190
- e.g. Fg.LIGHT_MAGENTA + "hello".
193
+ """Support building an ANSI sequence string when self is the left operand.
194
+
195
+ e.g. Fg.LIGHT_MAGENTA + "hello"
191
196
"""
192
197
return str (self ) + str (other )
193
198
194
199
def __radd__ (self , other : Any ) -> str :
195
- """Support building an ANSI sequence string when self is the right operand
196
- e.g. "hello" + Fg.RESET.
200
+ """Support building an ANSI sequence string when self is the right operand.
201
+
202
+ e.g. "hello" + Fg.RESET
197
203
"""
198
204
return str (other ) + str (self )
199
205
@@ -262,7 +268,8 @@ class TextStyle(AnsiSequence, Enum):
262
268
UNDERLINE_DISABLE = 24
263
269
264
270
def __str__ (self ) -> str :
265
- """Return ANSI text style sequence instead of enum name
271
+ """Return ANSI text style sequence instead of enum name.
272
+
266
273
This is helpful when using a TextStyle in an f-string or format() call
267
274
e.g. my_str = f"{TextStyle.UNDERLINE_ENABLE}hello{TextStyle.UNDERLINE_DISABLE}".
268
275
"""
@@ -271,6 +278,7 @@ def __str__(self) -> str:
271
278
272
279
class Fg (FgColor , Enum ):
273
280
"""Create ANSI sequences for the 16 standard terminal foreground text colors.
281
+
274
282
A terminal's color settings affect how these colors appear.
275
283
To reset any foreground color, use Fg.RESET.
276
284
"""
@@ -295,7 +303,8 @@ class Fg(FgColor, Enum):
295
303
RESET = 39
296
304
297
305
def __str__ (self ) -> str :
298
- """Return ANSI color sequence instead of enum name
306
+ """Return ANSI color sequence instead of enum name.
307
+
299
308
This is helpful when using an Fg in an f-string or format() call
300
309
e.g. my_str = f"{Fg.BLUE}hello{Fg.RESET}".
301
310
"""
@@ -304,6 +313,7 @@ def __str__(self) -> str:
304
313
305
314
class Bg (BgColor , Enum ):
306
315
"""Create ANSI sequences for the 16 standard terminal background text colors.
316
+
307
317
A terminal's color settings affect how these colors appear.
308
318
To reset any background color, use Bg.RESET.
309
319
"""
@@ -328,7 +338,8 @@ class Bg(BgColor, Enum):
328
338
RESET = 49
329
339
330
340
def __str__ (self ) -> str :
331
- """Return ANSI color sequence instead of enum name
341
+ """Return ANSI color sequence instead of enum name.
342
+
332
343
This is helpful when using a Bg in an f-string or format() call
333
344
e.g. my_str = f"{Bg.BLACK}hello{Bg.RESET}".
334
345
"""
@@ -337,6 +348,7 @@ def __str__(self) -> str:
337
348
338
349
class EightBitFg (FgColor , Enum ):
339
350
"""Create ANSI sequences for 8-bit terminal foreground text colors. Most terminals support 8-bit/256-color mode.
351
+
340
352
The first 16 colors correspond to the 16 colors from Fg and behave the same way.
341
353
To reset any foreground color, including 8-bit, use Fg.RESET.
342
354
"""
@@ -599,7 +611,8 @@ class EightBitFg(FgColor, Enum):
599
611
GRAY_93 = 255
600
612
601
613
def __str__ (self ) -> str :
602
- """Return ANSI color sequence instead of enum name
614
+ """Return ANSI color sequence instead of enum name.
615
+
603
616
This is helpful when using an EightBitFg in an f-string or format() call
604
617
e.g. my_str = f"{EightBitFg.SLATE_BLUE_1}hello{Fg.RESET}".
605
618
"""
@@ -608,6 +621,7 @@ def __str__(self) -> str:
608
621
609
622
class EightBitBg (BgColor , Enum ):
610
623
"""Create ANSI sequences for 8-bit terminal background text colors. Most terminals support 8-bit/256-color mode.
624
+
611
625
The first 16 colors correspond to the 16 colors from Bg and behave the same way.
612
626
To reset any background color, including 8-bit, use Bg.RESET.
613
627
"""
@@ -870,7 +884,8 @@ class EightBitBg(BgColor, Enum):
870
884
GRAY_93 = 255
871
885
872
886
def __str__ (self ) -> str :
873
- """Return ANSI color sequence instead of enum name
887
+ """Return ANSI color sequence instead of enum name.
888
+
874
889
This is helpful when using an EightBitBg in an f-string or format() call
875
890
e.g. my_str = f"{EightBitBg.KHAKI_3}hello{Bg.RESET}".
876
891
"""
@@ -879,6 +894,7 @@ def __str__(self) -> str:
879
894
880
895
class RgbFg (FgColor ):
881
896
"""Create ANSI sequences for 24-bit (RGB) terminal foreground text colors. The terminal must support 24-bit/true-color.
897
+
882
898
To reset any foreground color, including 24-bit, use Fg.RESET.
883
899
"""
884
900
@@ -896,7 +912,8 @@ def __init__(self, r: int, g: int, b: int) -> None:
896
912
self ._sequence = f"{ CSI } 38;2;{ r } ;{ g } ;{ b } m"
897
913
898
914
def __str__ (self ) -> str :
899
- """Return ANSI color sequence instead of enum name
915
+ """Return ANSI color sequence instead of enum name.
916
+
900
917
This is helpful when using an RgbFg in an f-string or format() call
901
918
e.g. my_str = f"{RgbFg(0, 55, 100)}hello{Fg.RESET}".
902
919
"""
@@ -905,6 +922,7 @@ def __str__(self) -> str:
905
922
906
923
class RgbBg (BgColor ):
907
924
"""Create ANSI sequences for 24-bit (RGB) terminal background text colors. The terminal must support 24-bit/true-color.
925
+
908
926
To reset any background color, including 24-bit, use Bg.RESET.
909
927
"""
910
928
@@ -922,7 +940,8 @@ def __init__(self, r: int, g: int, b: int) -> None:
922
940
self ._sequence = f"{ CSI } 48;2;{ r } ;{ g } ;{ b } m"
923
941
924
942
def __str__ (self ) -> str :
925
- """Return ANSI color sequence instead of enum name
943
+ """Return ANSI color sequence instead of enum name.
944
+
926
945
This is helpful when using an RgbBg in an f-string or format() call
927
946
e.g. my_str = f"{RgbBg(100, 255, 27)}hello{Bg.RESET}".
928
947
"""
@@ -942,6 +961,7 @@ def style(
942
961
underline : Optional [bool ] = None ,
943
962
) -> str :
944
963
"""Apply ANSI colors and/or styles to a string and return it.
964
+
945
965
The styling is self contained which means that at the end of the string reset code(s) are issued
946
966
to undo whatever styling was done at the beginning.
947
967
0 commit comments