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.
34"""
45
56import functools
@@ -92,6 +93,7 @@ def strip_style(text: str) -> str:
9293
9394def style_aware_wcswidth (text : str ) -> int :
9495 """Wrap wcswidth to make it compatible with strings that contain ANSI style sequences.
96+
9597 This is intended for single line strings. If text contains a newline, this
9698 function will return -1. For multiline strings, call widest_line() instead.
9799
@@ -105,8 +107,10 @@ def style_aware_wcswidth(text: str) -> int:
105107
106108
107109def 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.
110114
111115 :param text: the string being measured
112116 :return: The width of the string when printed to the terminal if no errors occur.
@@ -186,14 +190,16 @@ class AnsiSequence:
186190 """Base class to create ANSI sequence strings."""
187191
188192 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"
191196 """
192197 return str (self ) + str (other )
193198
194199 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
197203 """
198204 return str (other ) + str (self )
199205
@@ -262,7 +268,8 @@ class TextStyle(AnsiSequence, Enum):
262268 UNDERLINE_DISABLE = 24
263269
264270 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+
266273 This is helpful when using a TextStyle in an f-string or format() call
267274 e.g. my_str = f"{TextStyle.UNDERLINE_ENABLE}hello{TextStyle.UNDERLINE_DISABLE}".
268275 """
@@ -271,6 +278,7 @@ def __str__(self) -> str:
271278
272279class Fg (FgColor , Enum ):
273280 """Create ANSI sequences for the 16 standard terminal foreground text colors.
281+
274282 A terminal's color settings affect how these colors appear.
275283 To reset any foreground color, use Fg.RESET.
276284 """
@@ -295,7 +303,8 @@ class Fg(FgColor, Enum):
295303 RESET = 39
296304
297305 def __str__ (self ) -> str :
298- """Return ANSI color sequence instead of enum name
306+ """Return ANSI color sequence instead of enum name.
307+
299308 This is helpful when using an Fg in an f-string or format() call
300309 e.g. my_str = f"{Fg.BLUE}hello{Fg.RESET}".
301310 """
@@ -304,6 +313,7 @@ def __str__(self) -> str:
304313
305314class Bg (BgColor , Enum ):
306315 """Create ANSI sequences for the 16 standard terminal background text colors.
316+
307317 A terminal's color settings affect how these colors appear.
308318 To reset any background color, use Bg.RESET.
309319 """
@@ -328,7 +338,8 @@ class Bg(BgColor, Enum):
328338 RESET = 49
329339
330340 def __str__ (self ) -> str :
331- """Return ANSI color sequence instead of enum name
341+ """Return ANSI color sequence instead of enum name.
342+
332343 This is helpful when using a Bg in an f-string or format() call
333344 e.g. my_str = f"{Bg.BLACK}hello{Bg.RESET}".
334345 """
@@ -337,6 +348,7 @@ def __str__(self) -> str:
337348
338349class EightBitFg (FgColor , Enum ):
339350 """Create ANSI sequences for 8-bit terminal foreground text colors. Most terminals support 8-bit/256-color mode.
351+
340352 The first 16 colors correspond to the 16 colors from Fg and behave the same way.
341353 To reset any foreground color, including 8-bit, use Fg.RESET.
342354 """
@@ -599,7 +611,8 @@ class EightBitFg(FgColor, Enum):
599611 GRAY_93 = 255
600612
601613 def __str__ (self ) -> str :
602- """Return ANSI color sequence instead of enum name
614+ """Return ANSI color sequence instead of enum name.
615+
603616 This is helpful when using an EightBitFg in an f-string or format() call
604617 e.g. my_str = f"{EightBitFg.SLATE_BLUE_1}hello{Fg.RESET}".
605618 """
@@ -608,6 +621,7 @@ def __str__(self) -> str:
608621
609622class EightBitBg (BgColor , Enum ):
610623 """Create ANSI sequences for 8-bit terminal background text colors. Most terminals support 8-bit/256-color mode.
624+
611625 The first 16 colors correspond to the 16 colors from Bg and behave the same way.
612626 To reset any background color, including 8-bit, use Bg.RESET.
613627 """
@@ -870,7 +884,8 @@ class EightBitBg(BgColor, Enum):
870884 GRAY_93 = 255
871885
872886 def __str__ (self ) -> str :
873- """Return ANSI color sequence instead of enum name
887+ """Return ANSI color sequence instead of enum name.
888+
874889 This is helpful when using an EightBitBg in an f-string or format() call
875890 e.g. my_str = f"{EightBitBg.KHAKI_3}hello{Bg.RESET}".
876891 """
@@ -879,6 +894,7 @@ def __str__(self) -> str:
879894
880895class RgbFg (FgColor ):
881896 """Create ANSI sequences for 24-bit (RGB) terminal foreground text colors. The terminal must support 24-bit/true-color.
897+
882898 To reset any foreground color, including 24-bit, use Fg.RESET.
883899 """
884900
@@ -896,7 +912,8 @@ def __init__(self, r: int, g: int, b: int) -> None:
896912 self ._sequence = f"{ CSI } 38;2;{ r } ;{ g } ;{ b } m"
897913
898914 def __str__ (self ) -> str :
899- """Return ANSI color sequence instead of enum name
915+ """Return ANSI color sequence instead of enum name.
916+
900917 This is helpful when using an RgbFg in an f-string or format() call
901918 e.g. my_str = f"{RgbFg(0, 55, 100)}hello{Fg.RESET}".
902919 """
@@ -905,6 +922,7 @@ def __str__(self) -> str:
905922
906923class RgbBg (BgColor ):
907924 """Create ANSI sequences for 24-bit (RGB) terminal background text colors. The terminal must support 24-bit/true-color.
925+
908926 To reset any background color, including 24-bit, use Bg.RESET.
909927 """
910928
@@ -922,7 +940,8 @@ def __init__(self, r: int, g: int, b: int) -> None:
922940 self ._sequence = f"{ CSI } 48;2;{ r } ;{ g } ;{ b } m"
923941
924942 def __str__ (self ) -> str :
925- """Return ANSI color sequence instead of enum name
943+ """Return ANSI color sequence instead of enum name.
944+
926945 This is helpful when using an RgbBg in an f-string or format() call
927946 e.g. my_str = f"{RgbBg(100, 255, 27)}hello{Bg.RESET}".
928947 """
@@ -942,6 +961,7 @@ def style(
942961 underline : Optional [bool ] = None ,
943962) -> str :
944963 """Apply ANSI colors and/or styles to a string and return it.
964+
945965 The styling is self contained which means that at the end of the string reset code(s) are issued
946966 to undo whatever styling was done at the beginning.
947967
0 commit comments