1
- """
2
- Support for ANSI escape sequences which are used for things like applying style to text,
1
+ """Support for ANSI escape sequences which are used for things like applying style to text,
3
2
setting the window title, and asynchronous alerts.
4
3
"""
5
4
@@ -83,8 +82,7 @@ def __repr__(self) -> str:
83
82
84
83
85
84
def strip_style (text : str ) -> str :
86
- """
87
- Strip ANSI style sequences from a string.
85
+ """Strip ANSI style sequences from a string.
88
86
89
87
:param text: string which may contain ANSI style sequences
90
88
:return: the same string with any ANSI style sequences removed
@@ -93,8 +91,7 @@ def strip_style(text: str) -> str:
93
91
94
92
95
93
def style_aware_wcswidth (text : str ) -> int :
96
- """
97
- Wrap wcswidth to make it compatible with strings that contain ANSI style sequences.
94
+ """Wrap wcswidth to make it compatible with strings that contain ANSI style sequences.
98
95
This is intended for single line strings. If text contains a newline, this
99
96
function will return -1. For multiline strings, call widest_line() instead.
100
97
@@ -108,8 +105,7 @@ def style_aware_wcswidth(text: str) -> int:
108
105
109
106
110
107
def widest_line (text : str ) -> int :
111
- """
112
- Return the width of the widest line in a multiline string. This wraps style_aware_wcswidth()
108
+ """Return the width of the widest line in a multiline string. This wraps style_aware_wcswidth()
113
109
so it handles ANSI style sequences and has the same restrictions on non-printable characters.
114
110
115
111
:param text: the string being measured
@@ -128,8 +124,7 @@ def widest_line(text: str) -> int:
128
124
129
125
130
126
def style_aware_write (fileobj : IO [str ], msg : str ) -> None :
131
- """
132
- Write a string to a fileobject and strip its ANSI style sequences if required by allow_style setting
127
+ """Write a string to a fileobject and strip its ANSI style sequences if required by allow_style setting
133
128
134
129
:param fileobj: the file object being written to
135
130
:param msg: the string being written
@@ -143,8 +138,7 @@ def style_aware_write(fileobj: IO[str], msg: str) -> None:
143
138
# Utility functions which create various ANSI sequences
144
139
####################################################################################
145
140
def set_title (title : str ) -> str :
146
- """
147
- Generate a string that, when printed, sets a terminal's window title.
141
+ """Generate a string that, when printed, sets a terminal's window title.
148
142
149
143
:param title: new title for the window
150
144
:return: the set title string
@@ -153,8 +147,7 @@ def set_title(title: str) -> str:
153
147
154
148
155
149
def clear_screen (clear_type : int = 2 ) -> str :
156
- """
157
- Generate a string that, when printed, clears a terminal screen based on value of clear_type.
150
+ """Generate a string that, when printed, clears a terminal screen based on value of clear_type.
158
151
159
152
:param clear_type: integer which specifies how to clear the screen (Defaults to 2)
160
153
Possible values:
@@ -171,8 +164,7 @@ def clear_screen(clear_type: int = 2) -> str:
171
164
172
165
173
166
def clear_line (clear_type : int = 2 ) -> str :
174
- """
175
- Generate a string that, when printed, clears a line based on value of clear_type.
167
+ """Generate a string that, when printed, clears a line based on value of clear_type.
176
168
177
169
:param clear_type: integer which specifies how to clear the line (Defaults to 2)
178
170
Possible values:
@@ -194,15 +186,13 @@ class AnsiSequence:
194
186
"""Base class to create ANSI sequence strings"""
195
187
196
188
def __add__ (self , other : Any ) -> str :
197
- """
198
- Support building an ANSI sequence string when self is the left operand
189
+ """Support building an ANSI sequence string when self is the left operand
199
190
e.g. Fg.LIGHT_MAGENTA + "hello"
200
191
"""
201
192
return str (self ) + str (other )
202
193
203
194
def __radd__ (self , other : Any ) -> str :
204
- """
205
- Support building an ANSI sequence string when self is the right operand
195
+ """Support building an ANSI sequence string when self is the right operand
206
196
e.g. "hello" + Fg.RESET
207
197
"""
208
198
return str (other ) + str (self )
@@ -272,17 +262,15 @@ class TextStyle(AnsiSequence, Enum):
272
262
UNDERLINE_DISABLE = 24
273
263
274
264
def __str__ (self ) -> str :
275
- """
276
- Return ANSI text style sequence instead of enum name
265
+ """Return ANSI text style sequence instead of enum name
277
266
This is helpful when using a TextStyle in an f-string or format() call
278
267
e.g. my_str = f"{TextStyle.UNDERLINE_ENABLE}hello{TextStyle.UNDERLINE_DISABLE}"
279
268
"""
280
269
return f"{ CSI } { self .value } m"
281
270
282
271
283
272
class Fg (FgColor , Enum ):
284
- """
285
- Create ANSI sequences for the 16 standard terminal foreground text colors.
273
+ """Create ANSI sequences for the 16 standard terminal foreground text colors.
286
274
A terminal's color settings affect how these colors appear.
287
275
To reset any foreground color, use Fg.RESET.
288
276
"""
@@ -307,17 +295,15 @@ class Fg(FgColor, Enum):
307
295
RESET = 39
308
296
309
297
def __str__ (self ) -> str :
310
- """
311
- Return ANSI color sequence instead of enum name
298
+ """Return ANSI color sequence instead of enum name
312
299
This is helpful when using an Fg in an f-string or format() call
313
300
e.g. my_str = f"{Fg.BLUE}hello{Fg.RESET}"
314
301
"""
315
302
return f"{ CSI } { self .value } m"
316
303
317
304
318
305
class Bg (BgColor , Enum ):
319
- """
320
- Create ANSI sequences for the 16 standard terminal background text colors.
306
+ """Create ANSI sequences for the 16 standard terminal background text colors.
321
307
A terminal's color settings affect how these colors appear.
322
308
To reset any background color, use Bg.RESET.
323
309
"""
@@ -342,17 +328,15 @@ class Bg(BgColor, Enum):
342
328
RESET = 49
343
329
344
330
def __str__ (self ) -> str :
345
- """
346
- Return ANSI color sequence instead of enum name
331
+ """Return ANSI color sequence instead of enum name
347
332
This is helpful when using a Bg in an f-string or format() call
348
333
e.g. my_str = f"{Bg.BLACK}hello{Bg.RESET}"
349
334
"""
350
335
return f"{ CSI } { self .value } m"
351
336
352
337
353
338
class EightBitFg (FgColor , Enum ):
354
- """
355
- Create ANSI sequences for 8-bit terminal foreground text colors. Most terminals support 8-bit/256-color mode.
339
+ """Create ANSI sequences for 8-bit terminal foreground text colors. Most terminals support 8-bit/256-color mode.
356
340
The first 16 colors correspond to the 16 colors from Fg and behave the same way.
357
341
To reset any foreground color, including 8-bit, use Fg.RESET.
358
342
"""
@@ -615,17 +599,15 @@ class EightBitFg(FgColor, Enum):
615
599
GRAY_93 = 255
616
600
617
601
def __str__ (self ) -> str :
618
- """
619
- Return ANSI color sequence instead of enum name
602
+ """Return ANSI color sequence instead of enum name
620
603
This is helpful when using an EightBitFg in an f-string or format() call
621
604
e.g. my_str = f"{EightBitFg.SLATE_BLUE_1}hello{Fg.RESET}"
622
605
"""
623
606
return f"{ CSI } 38;5;{ self .value } m"
624
607
625
608
626
609
class EightBitBg (BgColor , Enum ):
627
- """
628
- Create ANSI sequences for 8-bit terminal background text colors. Most terminals support 8-bit/256-color mode.
610
+ """Create ANSI sequences for 8-bit terminal background text colors. Most terminals support 8-bit/256-color mode.
629
611
The first 16 colors correspond to the 16 colors from Bg and behave the same way.
630
612
To reset any background color, including 8-bit, use Bg.RESET.
631
613
"""
@@ -888,23 +870,20 @@ class EightBitBg(BgColor, Enum):
888
870
GRAY_93 = 255
889
871
890
872
def __str__ (self ) -> str :
891
- """
892
- Return ANSI color sequence instead of enum name
873
+ """Return ANSI color sequence instead of enum name
893
874
This is helpful when using an EightBitBg in an f-string or format() call
894
875
e.g. my_str = f"{EightBitBg.KHAKI_3}hello{Bg.RESET}"
895
876
"""
896
877
return f"{ CSI } 48;5;{ self .value } m"
897
878
898
879
899
880
class RgbFg (FgColor ):
900
- """
901
- Create ANSI sequences for 24-bit (RGB) terminal foreground text colors. The terminal must support 24-bit/true-color mode.
881
+ """Create ANSI sequences for 24-bit (RGB) terminal foreground text colors. The terminal must support 24-bit/true-color.
902
882
To reset any foreground color, including 24-bit, use Fg.RESET.
903
883
"""
904
884
905
885
def __init__ (self , r : int , g : int , b : int ) -> None :
906
- """
907
- RgbFg initializer
886
+ """RgbFg initializer
908
887
909
888
:param r: integer from 0-255 for the red component of the color
910
889
:param g: integer from 0-255 for the green component of the color
@@ -917,23 +896,20 @@ def __init__(self, r: int, g: int, b: int) -> None:
917
896
self ._sequence = f"{ CSI } 38;2;{ r } ;{ g } ;{ b } m"
918
897
919
898
def __str__ (self ) -> str :
920
- """
921
- Return ANSI color sequence instead of enum name
899
+ """Return ANSI color sequence instead of enum name
922
900
This is helpful when using an RgbFg in an f-string or format() call
923
901
e.g. my_str = f"{RgbFg(0, 55, 100)}hello{Fg.RESET}"
924
902
"""
925
903
return self ._sequence
926
904
927
905
928
906
class RgbBg (BgColor ):
929
- """
930
- Create ANSI sequences for 24-bit (RGB) terminal background text colors. The terminal must support 24-bit/true-color mode.
907
+ """Create ANSI sequences for 24-bit (RGB) terminal background text colors. The terminal must support 24-bit/true-color.
931
908
To reset any background color, including 24-bit, use Bg.RESET.
932
909
"""
933
910
934
911
def __init__ (self , r : int , g : int , b : int ) -> None :
935
- """
936
- RgbBg initializer
912
+ """RgbBg initializer
937
913
938
914
:param r: integer from 0-255 for the red component of the color
939
915
:param g: integer from 0-255 for the green component of the color
@@ -946,8 +922,7 @@ def __init__(self, r: int, g: int, b: int) -> None:
946
922
self ._sequence = f"{ CSI } 48;2;{ r } ;{ g } ;{ b } m"
947
923
948
924
def __str__ (self ) -> str :
949
- """
950
- Return ANSI color sequence instead of enum name
925
+ """Return ANSI color sequence instead of enum name
951
926
This is helpful when using an RgbBg in an f-string or format() call
952
927
e.g. my_str = f"{RgbBg(100, 255, 27)}hello{Bg.RESET}"
953
928
"""
@@ -966,8 +941,7 @@ def style(
966
941
strikethrough : Optional [bool ] = None ,
967
942
underline : Optional [bool ] = None ,
968
943
) -> str :
969
- """
970
- Apply ANSI colors and/or styles to a string and return it.
944
+ """Apply ANSI colors and/or styles to a string and return it.
971
945
The styling is self contained which means that at the end of the string reset code(s) are issued
972
946
to undo whatever styling was done at the beginning.
973
947
@@ -1056,7 +1030,6 @@ def async_alert_str(*, terminal_columns: int, prompt: str, line: str, cursor_off
1056
1030
:param alert_msg: the message to display to the user
1057
1031
:return: the correct string so that the alert message appears to the user to be printed above the current line.
1058
1032
"""
1059
-
1060
1033
# Split the prompt lines since it can contain newline characters.
1061
1034
prompt_lines = prompt .splitlines () or ['' ]
1062
1035
0 commit comments