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,
32setting the window title, and asynchronous alerts.
43"""
54
@@ -83,8 +82,7 @@ def __repr__(self) -> str:
8382
8483
8584def strip_style (text : str ) -> str :
86- """
87- Strip ANSI style sequences from a string.
85+ """Strip ANSI style sequences from a string.
8886
8987 :param text: string which may contain ANSI style sequences
9088 :return: the same string with any ANSI style sequences removed
@@ -93,8 +91,7 @@ def strip_style(text: str) -> str:
9391
9492
9593def 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.
9895 This is intended for single line strings. If text contains a newline, this
9996 function will return -1. For multiline strings, call widest_line() instead.
10097
@@ -108,8 +105,7 @@ def style_aware_wcswidth(text: str) -> int:
108105
109106
110107def 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()
113109 so it handles ANSI style sequences and has the same restrictions on non-printable characters.
114110
115111 :param text: the string being measured
@@ -128,8 +124,7 @@ def widest_line(text: str) -> int:
128124
129125
130126def 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
133128
134129 :param fileobj: the file object being written to
135130 :param msg: the string being written
@@ -143,8 +138,7 @@ def style_aware_write(fileobj: IO[str], msg: str) -> None:
143138# Utility functions which create various ANSI sequences
144139####################################################################################
145140def 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.
148142
149143 :param title: new title for the window
150144 :return: the set title string
@@ -153,8 +147,7 @@ def set_title(title: str) -> str:
153147
154148
155149def 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.
158151
159152 :param clear_type: integer which specifies how to clear the screen (Defaults to 2)
160153 Possible values:
@@ -171,8 +164,7 @@ def clear_screen(clear_type: int = 2) -> str:
171164
172165
173166def 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.
176168
177169 :param clear_type: integer which specifies how to clear the line (Defaults to 2)
178170 Possible values:
@@ -194,15 +186,13 @@ class AnsiSequence:
194186 """Base class to create ANSI sequence strings"""
195187
196188 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
199190 e.g. Fg.LIGHT_MAGENTA + "hello"
200191 """
201192 return str (self ) + str (other )
202193
203194 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
206196 e.g. "hello" + Fg.RESET
207197 """
208198 return str (other ) + str (self )
@@ -272,17 +262,15 @@ class TextStyle(AnsiSequence, Enum):
272262 UNDERLINE_DISABLE = 24
273263
274264 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
277266 This is helpful when using a TextStyle in an f-string or format() call
278267 e.g. my_str = f"{TextStyle.UNDERLINE_ENABLE}hello{TextStyle.UNDERLINE_DISABLE}"
279268 """
280269 return f"{ CSI } { self .value } m"
281270
282271
283272class 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.
286274 A terminal's color settings affect how these colors appear.
287275 To reset any foreground color, use Fg.RESET.
288276 """
@@ -307,17 +295,15 @@ class Fg(FgColor, Enum):
307295 RESET = 39
308296
309297 def __str__ (self ) -> str :
310- """
311- Return ANSI color sequence instead of enum name
298+ """Return ANSI color sequence instead of enum name
312299 This is helpful when using an Fg in an f-string or format() call
313300 e.g. my_str = f"{Fg.BLUE}hello{Fg.RESET}"
314301 """
315302 return f"{ CSI } { self .value } m"
316303
317304
318305class 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.
321307 A terminal's color settings affect how these colors appear.
322308 To reset any background color, use Bg.RESET.
323309 """
@@ -342,17 +328,15 @@ class Bg(BgColor, Enum):
342328 RESET = 49
343329
344330 def __str__ (self ) -> str :
345- """
346- Return ANSI color sequence instead of enum name
331+ """Return ANSI color sequence instead of enum name
347332 This is helpful when using a Bg in an f-string or format() call
348333 e.g. my_str = f"{Bg.BLACK}hello{Bg.RESET}"
349334 """
350335 return f"{ CSI } { self .value } m"
351336
352337
353338class 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.
356340 The first 16 colors correspond to the 16 colors from Fg and behave the same way.
357341 To reset any foreground color, including 8-bit, use Fg.RESET.
358342 """
@@ -615,17 +599,15 @@ class EightBitFg(FgColor, Enum):
615599 GRAY_93 = 255
616600
617601 def __str__ (self ) -> str :
618- """
619- Return ANSI color sequence instead of enum name
602+ """Return ANSI color sequence instead of enum name
620603 This is helpful when using an EightBitFg in an f-string or format() call
621604 e.g. my_str = f"{EightBitFg.SLATE_BLUE_1}hello{Fg.RESET}"
622605 """
623606 return f"{ CSI } 38;5;{ self .value } m"
624607
625608
626609class 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.
629611 The first 16 colors correspond to the 16 colors from Bg and behave the same way.
630612 To reset any background color, including 8-bit, use Bg.RESET.
631613 """
@@ -888,23 +870,20 @@ class EightBitBg(BgColor, Enum):
888870 GRAY_93 = 255
889871
890872 def __str__ (self ) -> str :
891- """
892- Return ANSI color sequence instead of enum name
873+ """Return ANSI color sequence instead of enum name
893874 This is helpful when using an EightBitBg in an f-string or format() call
894875 e.g. my_str = f"{EightBitBg.KHAKI_3}hello{Bg.RESET}"
895876 """
896877 return f"{ CSI } 48;5;{ self .value } m"
897878
898879
899880class 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.
902882 To reset any foreground color, including 24-bit, use Fg.RESET.
903883 """
904884
905885 def __init__ (self , r : int , g : int , b : int ) -> None :
906- """
907- RgbFg initializer
886+ """RgbFg initializer
908887
909888 :param r: integer from 0-255 for the red component of the color
910889 :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:
917896 self ._sequence = f"{ CSI } 38;2;{ r } ;{ g } ;{ b } m"
918897
919898 def __str__ (self ) -> str :
920- """
921- Return ANSI color sequence instead of enum name
899+ """Return ANSI color sequence instead of enum name
922900 This is helpful when using an RgbFg in an f-string or format() call
923901 e.g. my_str = f"{RgbFg(0, 55, 100)}hello{Fg.RESET}"
924902 """
925903 return self ._sequence
926904
927905
928906class 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.
931908 To reset any background color, including 24-bit, use Bg.RESET.
932909 """
933910
934911 def __init__ (self , r : int , g : int , b : int ) -> None :
935- """
936- RgbBg initializer
912+ """RgbBg initializer
937913
938914 :param r: integer from 0-255 for the red component of the color
939915 :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:
946922 self ._sequence = f"{ CSI } 48;2;{ r } ;{ g } ;{ b } m"
947923
948924 def __str__ (self ) -> str :
949- """
950- Return ANSI color sequence instead of enum name
925+ """Return ANSI color sequence instead of enum name
951926 This is helpful when using an RgbBg in an f-string or format() call
952927 e.g. my_str = f"{RgbBg(100, 255, 27)}hello{Bg.RESET}"
953928 """
@@ -966,8 +941,7 @@ def style(
966941 strikethrough : Optional [bool ] = None ,
967942 underline : Optional [bool ] = None ,
968943) -> 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.
971945 The styling is self contained which means that at the end of the string reset code(s) are issued
972946 to undo whatever styling was done at the beginning.
973947
@@ -1056,7 +1030,6 @@ def async_alert_str(*, terminal_columns: int, prompt: str, line: str, cursor_off
10561030 :param alert_msg: the message to display to the user
10571031 :return: the correct string so that the alert message appears to the user to be printed above the current line.
10581032 """
1059-
10601033 # Split the prompt lines since it can contain newline characters.
10611034 prompt_lines = prompt .splitlines () or ['' ]
10621035
0 commit comments