11# see also: http://misc.flogisoft.com/bash/tip_colors_and_formatting
22
33
4+ from typing import List , Optional
5+
46RESET = 0
57BLACK = 30
68RED = 31
3537
3638
3739def colorize (
38- text , color = None , background = None , effects = None , color_256 = None , background_256 = None , with_end = True
39- ):
40+ text : str ,
41+ color : Optional [int ] = None ,
42+ background : Optional [int ] = None ,
43+ effects : Optional [List [int ]] = None ,
44+ color_256 : Optional [int ] = None ,
45+ background_256 : Optional [int ] = None ,
46+ with_end : bool = True ,
47+ ) -> str :
4048 if effects is None :
4149 effects = []
4250 start = []
@@ -60,25 +68,25 @@ def colorize(
6068 start .append (effect )
6169 end .append (effect + _RESET_EFFECT )
6270
63- start_code = f"{ _ESC !s } [{ ';' .join ([str (s ) for s in start ])!s } m" if text != "" else ""
64- end_code = f"{ _ESC !s } [{ ';' .join ([str (e ) for e in end ])!s } m" if with_end else ""
65- return f"{ start_code !s } { text !s } { end_code !s } "
71+ start_code = f"{ _ESC } [{ ';' .join ([str (s ) for s in start ])} m" if text != "" else ""
72+ end_code = f"{ _ESC } [{ ';' .join ([str (e ) for e in end ])} m" if with_end else ""
73+ return f"{ start_code } { text } { end_code } "
6674
6775
68- def print_colors ():
76+ def print_colors () -> None :
6977 color_pivot = [0 ]
7078 color_pivot += [e * 6 + 16 for e in range (37 )]
7179 color_pivot .append (256 )
7280 color_pivot_start = color_pivot [:- 1 ]
7381 color_pivot_end = color_pivot [1 :]
74- color_table = [range (cs , ce ) for cs , ce in zip (color_pivot_start , color_pivot_end )]
82+ color_table_list = [range (cs , ce ) for cs , ce in zip (color_pivot_start , color_pivot_end )]
7583
76- for ct in color_table :
84+ for color_table in color_table_list :
7785 text = ""
78- for c in ct :
79- cs = str (c )
80- padding = "" .join ([" " for e in range (3 - len (cs ))])
81- text += colorize (f" { padding !s } { cs !s } " , background_256 = c , with_end = False )
86+ for color in color_table :
87+ color_string = str (color )
88+ padding = "" .join ([" " for e in range (3 - len (color_string ))])
89+ text += colorize (f" { padding } { color_string } " , background_256 = color , with_end = False )
8290 print (text + colorize ("" , background = DEFAULT ))
8391
8492
0 commit comments