2727ANSI_STYLE_RE = re .compile (r'\x1b\[[^m]*m' )
2828
2929
30+ class ColorBase (Enum ):
31+ """
32+ Base class for fg and bg classes
33+ This expects the base classes to define enums of: color name -> ANSI color sequence
34+ """
35+ def __str__ (self ) -> str :
36+ """Return ANSI color sequence instead of enum name"""
37+ return self .value
38+
39+ def __add__ (self , other : Any ) -> str :
40+ """Return self + other as string"""
41+ if isinstance (other , (fg , bg )):
42+ other = str (other )
43+ return self .value + other
44+
45+ def __radd__ (self , other : Any ) -> str :
46+ """Return other + self as string"""
47+ return other + self .value
48+
49+ @classmethod
50+ def colors (cls ) -> List [str ]:
51+ """Return a list of color names."""
52+ return [color .name for color in cls ]
53+
54+
3055# Foreground colors
3156# noinspection PyPep8Naming,DuplicatedCode
3257@unique
33- class fg (Enum ):
34- """Enum class for foreground colors (to support IDE autocompletion). """
58+ class fg (ColorBase ):
59+ """Enum class for foreground colors"""
3560 black = Fore .BLACK
3661 red = Fore .RED
3762 green = Fore .GREEN
@@ -50,17 +75,12 @@ class fg(Enum):
5075 bright_white = Fore .LIGHTWHITE_EX
5176 reset = Fore .RESET
5277
53- @staticmethod
54- def colors () -> List [str ]:
55- """Return a list of color names."""
56- return [color .name for color in fg ]
57-
5878
5979# Background colors
6080# noinspection PyPep8Naming,DuplicatedCode
6181@unique
62- class bg (Enum ):
63- """Enum class for background colors (to support IDE autocompletion). """
82+ class bg (ColorBase ):
83+ """Enum class for background colors"""
6484 black = Back .BLACK
6585 red = Back .RED
6686 green = Back .GREEN
@@ -79,11 +99,6 @@ class bg(Enum):
7999 bright_white = Back .LIGHTWHITE_EX
80100 reset = Back .RESET
81101
82- @staticmethod
83- def colors () -> List [str ]:
84- """Return a list of color names."""
85- return [color .name for color in bg ]
86-
87102
88103FG_RESET = fg .reset .value
89104BG_RESET = bg .reset .value
@@ -177,8 +192,10 @@ def style(text: Any, *, fg: Union[str, fg] = '', bg: Union[str, bg] = '', bold:
177192 to undo whatever styling was done at the beginning.
178193
179194 :param text: Any object compatible with str.format()
180- :param fg: foreground color. Relies on `fg_lookup()` to retrieve ANSI escape based on name or enum. Defaults to no color.
181- :param bg: background color. Relies on `bg_lookup()` to retrieve ANSI escape based on name or enum. Defaults to no color.
195+ :param fg: foreground color. Relies on `fg_lookup()` to retrieve ANSI escape based on name or enum.
196+ Defaults to no color.
197+ :param bg: background color. Relies on `bg_lookup()` to retrieve ANSI escape based on name or enum.
198+ Defaults to no color.
182199 :param bold: apply the bold style if True. Can be combined with dim. Defaults to False.
183200 :param dim: apply the dim style if True. Can be combined with bold. Defaults to False.
184201 :param underline: apply the underline style if True. Defaults to False.
0 commit comments