19
19
20
20
# Standard
21
21
from enum import Enum
22
- from typing import Union
22
+ from typing import Dict , Union
23
23
24
24
## Public ######################################################################
25
25
26
26
27
27
class Colors (Enum ):
28
- black = "black"
29
- dark_gray = "dark_gray"
30
- red = "red"
31
- light_red = "light_red"
32
- green = "green"
33
- light_green = "light_green"
34
- brown = "brown"
35
- orange = "orange"
36
- yellow = "yellow"
37
- blue = "blue"
38
- light_blue = "light_blue"
39
- purple = "purple"
40
- light_purple = "light_purple"
41
- cyan = "cyan"
42
- light_cyan = "light_cyan"
43
- light_gray = "light_gray"
44
- white = "white"
28
+ BLACK = "black"
29
+ DARK_GRAY = "dark_gray"
30
+ RED = "red"
31
+ LIGHT_RED = "light_red"
32
+ GREEN = "green"
33
+ LIGHT_GREEN = "light_green"
34
+ BROWN = "brown"
35
+ ORANGE = "orange"
36
+ YELLOW = "yellow"
37
+ BLUE = "blue"
38
+ LIGHT_BLUE = "light_blue"
39
+ PURPLE = "purple"
40
+ LIGHT_PURPLE = "light_purple"
41
+ CYAN = "cyan"
42
+ LIGHT_CYAN = "light_cyan"
43
+ LIGHT_GRAY = "light_gray"
44
+ WHITE = "white"
45
45
46
46
47
47
## Mapping from color name to ansi escape code for foreground color
48
- FG_COLOR_CODES = {
49
- Colors .black : "0;30" ,
50
- Colors .dark_gray : "1;30" ,
51
- Colors .red : "0;31" ,
52
- Colors .light_red : "1;31" ,
53
- Colors .green : "0;32" ,
54
- Colors .light_green : "1;32" ,
55
- Colors .brown : "0;33" ,
56
- Colors .orange : "0;33" ,
57
- Colors .yellow : "1;33" ,
58
- Colors .blue : "0;34" ,
59
- Colors .light_blue : "1;34" ,
60
- Colors .purple : "0;35" ,
61
- Colors .light_purple : "1;35" ,
62
- Colors .cyan : "0;36" ,
63
- Colors .light_cyan : "1;36" ,
64
- Colors .light_gray : "0;37" ,
65
- Colors .white : "1;37" ,
48
+ FG_COLOR_CODES : Dict [ Union [ Colors , str ], str ] = {
49
+ Colors .BLACK : "0;30" ,
50
+ Colors .DARK_GRAY : "1;30" ,
51
+ Colors .RED : "0;31" ,
52
+ Colors .LIGHT_RED : "1;31" ,
53
+ Colors .GREEN : "0;32" ,
54
+ Colors .LIGHT_GREEN : "1;32" ,
55
+ Colors .BROWN : "0;33" ,
56
+ Colors .ORANGE : "0;33" ,
57
+ Colors .YELLOW : "1;33" ,
58
+ Colors .BLUE : "0;34" ,
59
+ Colors .LIGHT_BLUE : "1;34" ,
60
+ Colors .PURPLE : "0;35" ,
61
+ Colors .LIGHT_PURPLE : "1;35" ,
62
+ Colors .CYAN : "0;36" ,
63
+ Colors .LIGHT_CYAN : "1;36" ,
64
+ Colors .LIGHT_GRAY : "0;37" ,
65
+ Colors .WHITE : "1;37" ,
66
66
}
67
- FG_COLOR_CODES .update (** {key .value : val for key , val in FG_COLOR_CODES . items () })
67
+ FG_COLOR_CODES .update (** {entry .value : FG_COLOR_CODES [ entry ] for entry in Colors })
68
68
69
69
## Mapping from color name to ansi escape code for background color
70
- BG_COLOR_CODES = {
71
- Colors .black : "0;40" ,
72
- Colors .dark_gray : "1;40" ,
73
- Colors .red : "0;41" ,
74
- Colors .light_red : "1;41" ,
75
- Colors .green : "0;42" ,
76
- Colors .light_green : "1;42" ,
77
- Colors .brown : "0;43" ,
78
- Colors .orange : "0;43" ,
79
- Colors .yellow : "1;43" ,
80
- Colors .blue : "0;44" ,
81
- Colors .light_blue : "1;44" ,
82
- Colors .purple : "0;45" ,
83
- Colors .light_purple : "1;45" ,
84
- Colors .cyan : "0;46" ,
85
- Colors .light_cyan : "1;46" ,
86
- Colors .light_gray : "0;47" ,
87
- Colors .white : "1;47" ,
70
+ BG_COLOR_CODES : Dict [ Union [ Colors , str ], str ] = {
71
+ Colors .BLACK : "0;40" ,
72
+ Colors .DARK_GRAY : "1;40" ,
73
+ Colors .RED : "0;41" ,
74
+ Colors .LIGHT_RED : "1;41" ,
75
+ Colors .GREEN : "0;42" ,
76
+ Colors .LIGHT_GREEN : "1;42" ,
77
+ Colors .BROWN : "0;43" ,
78
+ Colors .ORANGE : "0;43" ,
79
+ Colors .YELLOW : "1;43" ,
80
+ Colors .BLUE : "0;44" ,
81
+ Colors .LIGHT_BLUE : "1;44" ,
82
+ Colors .PURPLE : "0;45" ,
83
+ Colors .LIGHT_PURPLE : "1;45" ,
84
+ Colors .CYAN : "0;46" ,
85
+ Colors .LIGHT_CYAN : "1;46" ,
86
+ Colors .LIGHT_GRAY : "0;47" ,
87
+ Colors .WHITE : "1;47" ,
88
88
}
89
- BG_COLOR_CODES .update (** {key .value : val for key , val in BG_COLOR_CODES . items () })
89
+ BG_COLOR_CODES .update (** {entry .value : BG_COLOR_CODES [ entry ] for entry in Colors })
90
90
91
91
COLOR_START = "\033 ["
92
92
COLOR_END = "\033 [0m"
@@ -102,8 +102,7 @@ def colorize(x: str, color: Union[Colors, str]) -> str:
102
102
Returns:
103
103
x_color (str): The input string with color applied
104
104
"""
105
- assert color in FG_COLOR_CODES
106
- return f"{ COLOR_START } { FG_COLOR_CODES [color ]} m{ x } { COLOR_END } "
105
+ return _apply_color (x , color , FG_COLOR_CODES )
107
106
108
107
109
108
def bg_colorize (x : str , color : Union [Colors , str ]) -> str :
@@ -116,8 +115,7 @@ def bg_colorize(x: str, color: Union[Colors, str]) -> str:
116
115
Returns:
117
116
x_color (str): The input string with color applied
118
117
"""
119
- assert color in BG_COLOR_CODES
120
- return f"{ COLOR_START } { BG_COLOR_CODES [color ]} m{ x } { COLOR_END } "
118
+ return _apply_color (x , color , BG_COLOR_CODES )
121
119
122
120
123
121
def decolorize (x : str ) -> str :
@@ -135,3 +133,17 @@ def decolorize(x: str) -> str:
135
133
x = x .replace (COLOR_END , "" )
136
134
x = x .replace ("0m" , "" ) # TODO: Make this not prone to removing "1.0mb"
137
135
return x
136
+
137
+
138
+ ## Impl ########################################################################
139
+
140
+
141
+ def _apply_color (
142
+ x : str ,
143
+ color : Union [Colors , str ],
144
+ color_dict : Dict [Union [Colors , str ], str ],
145
+ ) -> str :
146
+ """Apply the color or raise a ValueError"""
147
+ if color_code := color_dict .get (color ):
148
+ return f"{ COLOR_START } { color_code } m{ x } { COLOR_END } "
149
+ raise ValueError (f"Invalid color: { color } " )
0 commit comments