33import fnmatch
44import math
55import os
6- import platform
76import re
87import sys
98import xml .etree .ElementTree as ET
109from typing import Dict , List , Set
1110
11+ sys .path .insert (0 , os .path .join (os .path .dirname (os .path .abspath (__file__ )), "../../" ))
12+
13+ from misc .utility .color import Ansi , force_stdout_color , is_stdout_color
14+
1215################################################################################
1316# Config #
1417################################################################################
1518
1619flags = {
17- "c" : platform . platform () != "Windows" , # Disable by default on windows, since we use ANSI escape codes
20+ "c" : is_stdout_color (),
1821 "b" : False ,
1922 "g" : False ,
2023 "s" : False ,
8588 "Constructors" ,
8689]
8790colors = {
88- "name" : [36 ], # cyan
89- "part_big_problem" : [4 , 31 ], # underline, red
90- "part_problem" : [31 ], # red
91- "part_mostly_good" : [33 ], # yellow
92- "part_good" : [32 ], # green
93- "url" : [4 , 34 ], # underline, blue
94- "section" : [1 , 4 ], # bold, underline
95- "state_off" : [36 ], # cyan
96- "state_on" : [1 , 35 ], # bold, magenta/plum
97- "bold" : [1 ], # bold
91+ "name" : [Ansi . CYAN ], # cyan
92+ "part_big_problem" : [Ansi . RED , Ansi . UNDERLINE ], # underline, red
93+ "part_problem" : [Ansi . RED ], # red
94+ "part_mostly_good" : [Ansi . YELLOW ], # yellow
95+ "part_good" : [Ansi . GREEN ], # green
96+ "url" : [Ansi . BLUE , Ansi . UNDERLINE ], # underline, blue
97+ "section" : [Ansi . BOLD , Ansi . UNDERLINE ], # bold, underline
98+ "state_off" : [Ansi . CYAN ], # cyan
99+ "state_on" : [Ansi . BOLD , Ansi . MAGENTA ], # bold, magenta/plum
100+ "bold" : [Ansi . BOLD ], # bold
98101}
99102overall_progress_description_weight = 10
100103
@@ -111,13 +114,10 @@ def validate_tag(elem: ET.Element, tag: str) -> None:
111114
112115
113116def color (color : str , string : str ) -> str :
114- if flags ["c" ] and terminal_supports_color ():
115- color_format = ""
116- for code in colors [color ]:
117- color_format += "\033 [" + str (code ) + "m"
118- return color_format + string + "\033 [0m"
119- else :
117+ if not is_stdout_color ():
120118 return string
119+ color_format = "" .join ([str (x ) for x in colors [color ]])
120+ return f"{ color_format } { string } { Ansi .RESET } "
121121
122122
123123ansi_escape = re .compile (r"\x1b[^m]*m" )
@@ -127,16 +127,6 @@ def nonescape_len(s: str) -> int:
127127 return len (ansi_escape .sub ("" , s ))
128128
129129
130- def terminal_supports_color ():
131- p = sys .platform
132- supported_platform = p != "Pocket PC" and (p != "win32" or "ANSICON" in os .environ )
133-
134- is_a_tty = hasattr (sys .stdout , "isatty" ) and sys .stdout .isatty ()
135- if not supported_platform or not is_a_tty :
136- return False
137- return True
138-
139-
140130################################################################################
141131# Classes #
142132################################################################################
@@ -342,6 +332,7 @@ def generate_for_class(c: ET.Element):
342332 table_column_names .append ("Docs URL" )
343333 table_columns .append ("url" )
344334
335+ force_stdout_color (flags ["c" ])
345336
346337################################################################################
347338# Help #
0 commit comments