10 console_encoding = sys.getfilesystemencoding()
43 See https://msdn.microsoft.com/zh-cn/windows/apps/ms682088%28v=vs.100%29#_win32_character_attributes
49 name =
"windows console"
50 STD_INPUT_HANDLE = -10
51 STD_OUTPUT_HANDLE = -11
52 STD_ERROR_HANDLE = -12
54 FOREGROUND_BLACK = 0x0
55 FOREGROUND_BLUE = 0x01
56 FOREGROUND_GREEN = 0x02
58 FOREGROUND_INTENSITY = 0x08
60 BACKGROUND_BLUE = 0x10
61 BACKGROUND_GREEN = 0x20
63 BACKGROUND_INTENSITY = 0x80
66 print_style.FC_BLACK: FOREGROUND_BLACK,
67 print_style.FC_BLUE: FOREGROUND_BLUE | FOREGROUND_INTENSITY,
68 print_style.FC_GREEN: FOREGROUND_GREEN | FOREGROUND_INTENSITY,
70 FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
71 print_style.FC_RED: FOREGROUND_RED | FOREGROUND_INTENSITY,
72 print_style.FC_MAGENTA:
73 FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
74 print_style.FC_YELLOW:
75 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
77 FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED,
78 print_style.BC_BLACK: FOREGROUND_BLACK,
79 print_style.BC_BLUE: BACKGROUND_BLUE,
80 print_style.BC_GREEN: BACKGROUND_GREEN,
81 print_style.BC_CYAN: BACKGROUND_BLUE | BACKGROUND_GREEN,
82 print_style.BC_RED: BACKGROUND_RED,
83 print_style.BC_MAGENTA: BACKGROUND_RED | BACKGROUND_BLUE,
84 print_style.BC_YELLOW: BACKGROUND_RED | BACKGROUND_GREEN,
86 BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE,
87 print_style.FW_BOLD: BACKGROUND_INTENSITY,
94 return (Win32ConsoleColor.FOREGROUND_RED
95 | Win32ConsoleColor.FOREGROUND_GREEN
96 | Win32ConsoleColor.FOREGROUND_BLUE)
100 Example: set_cmd_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
102 bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
106 style = Win32ConsoleColor.FOREGROUND_BLACK
108 style = style | Win32ConsoleColor.COLOR_MAP[opt]
109 if style == Win32ConsoleColor.FOREGROUND_BLACK:
110 sys.stdout.write(text)
114 sys.stdout.write(text)
118 style = Win32ConsoleColor.FOREGROUND_BLACK
120 style = style | Win32ConsoleColor.COLOR_MAP[opt]
121 if style == Win32ConsoleColor.FOREGROUND_BLACK:
122 sys.stderr.write(text)
126 sys.stderr.write(text)
133 print_style.FC_BLACK:
"30",
134 print_style.FC_BLUE:
"34",
135 print_style.FC_GREEN:
"32",
136 print_style.FC_CYAN:
"36",
137 print_style.FC_RED:
"31",
138 print_style.FC_MAGENTA:
"35",
139 print_style.FC_YELLOW:
"33",
140 print_style.FC_WHITE:
"37",
141 print_style.BC_BLACK:
"40",
142 print_style.BC_BLUE:
"44",
143 print_style.BC_GREEN:
"42",
144 print_style.BC_CYAN:
"46",
145 print_style.BC_RED:
"41",
146 print_style.BC_MAGENTA:
"45",
147 print_style.BC_YELLOW:
"43",
148 print_style.BC_WHITE:
"47",
149 print_style.FW_BOLD:
"1",
155 style.append(TermColor.COLOR_MAP[opt])
158 sys.stdout.write(
"\033[" +
";".join(style) +
"m" + text +
161 sys.stdout.write(text)
166 style.append(TermColor.COLOR_MAP[opt])
169 sys.stderr.write(
"\033[" +
";".join(style) +
"m" + text +
172 sys.stderr.write(text)
178 print_style.FC_BLACK:
"color: {0}Black;",
179 print_style.FC_BLUE:
"color: {0}Blue;",
180 print_style.FC_GREEN:
"color: {0}Green;",
181 print_style.FC_CYAN:
"color: {0}Cyan;",
182 print_style.FC_RED:
"color: {0}Red;",
183 print_style.FC_MAGENTA:
"color: {0}Magenta;",
184 print_style.FC_YELLOW:
"color: {0}Yellow;",
185 print_style.FC_WHITE:
"color: {0}White;",
186 print_style.BC_BLACK:
"background-color: {0}Black;",
187 print_style.BC_BLUE:
"background-color: {0}Blue;",
188 print_style.BC_GREEN:
"background-color: {0}Green;",
189 print_style.BC_CYAN:
"background-color: {0}Cyan;",
190 print_style.BC_RED:
"background-color: {0}Red;",
191 print_style.BC_MAGENTA:
"background-color: {0}Magenta;",
192 print_style.BC_YELLOW:
"background-color: {0}Yellow;",
193 print_style.BC_WHITE:
"background-color: {0}White;",
194 print_style.FW_BOLD:
"font-weight: bold;",
200 if print_style.theme:
202 HtmlColor.COLOR_MAP[opt].format(print_style.theme +
"-"))
204 style.append(HtmlColor.COLOR_MAP[opt].format(
""))
207 sys.stdout.write(
'<span style="' +
" ".join(style) +
'">' +
208 text.replace(
'&',
'&').replace(
'<',
'<').replace(
'>',
'>') +
"</span>")
210 sys.stdout.write(text.replace(
'&',
'&').replace(
'<',
'<').replace(
'>',
'>'))
215 if print_style.theme:
217 HtmlColor.COLOR_MAP[opt].format(print_style.theme +
"-"))
219 style.append(HtmlColor.COLOR_MAP[opt].format(
""))
222 sys.stderr.write(
'<span style="' +
" ".join(style) +
'">' +
223 text.replace(
'&',
'&').replace(
'<',
'<').replace(
'>',
'>') +
"</span>")
225 sys.stderr.write(text.replace(
'&',
'&').replace(
'<',
'<').replace(
'>',
'>'))
232 sys.stdout.write(text)
235 sys.stderr.write(text)
240 if os.getenv(
"CPRINTF_MODE"):
241 return os.getenv(
"CPRINTF_MODE")
242 term_name = os.getenv(
"TERM")
245 if "dump" == term_name:
247 if re.search(
'-256(color)?$', term_name, re.IGNORECASE):
249 if re.search(
'^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux',
250 term_name, re.IGNORECASE):
252 if os.getenv(
"COLORTERM"):
254 if os.getenv(
"TF_BUILD")
and os.getenv(
"AGENT_NAME"):
258 'TRAVIS',
'CIRCLECI',
'APPVEYOR',
'GITLAB_CI',
259 'GITHUB_ACTIONS',
'BUILDKITE',
'DRONE'
261 if os.getenv(known_ci):
263 ci_name = os.getenv(
"CI_NAME")
264 if ci_name
and ci_name.lower() ==
"codeship":
267 if os.getenv(
"TEAMCITY_VERSION"):
268 TEAMCITY_VERSION = os.getenv(
"TEAMCITY_VERSION")
269 if re.search(
'^(9\.(0*[1-9]\d*)\.|\d{2,}\.)', TEAMCITY_VERSION,
274 if os.getenv(
"TERM_PROGRAM"):
275 if re.search(
'(iTerm.app|Apple_Terminal)', os.getenv(
"TERM_PROGRAM"),
279 if "windows" == platform.system().lower():
280 ostype_name = os.getenv(
"OSTYPE")
282 ostype_name = ostype_name.lower()
283 if "msys" == ostype_name
or "cygwin" == ostype_name:
285 return "win32_console"
290 mode_name = mode_name.lower()
291 if not mode_name
or mode_name ==
"auto":
294 elif mode_name ==
"none":
295 print_style.engine = NoneColor
297 elif mode_name ==
"term":
298 print_style.engine = TermColor
300 elif mode_name ==
"win32_console":
302 See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/windows_api_reference.asp
303 for information on Windows APIs.
305 Win32ConsoleColor.std_out_handle = ctypes.windll.kernel32.GetStdHandle(
306 Win32ConsoleColor.STD_OUTPUT_HANDLE)
307 Win32ConsoleColor.std_err_handle = ctypes.windll.kernel32.GetStdHandle(
308 Win32ConsoleColor.STD_ERROR_HANDLE)
310 print_style.engine = Win32ConsoleColor
312 elif mode_name ==
"html":
313 print_style.engine = HtmlColor
316 print_style.engine = NoneColor
320 if theme_name
is None:
321 if not os.getenv(
"CPRINTF_THEME")
is None:
324 print_style.theme = theme_name
330 ret = fmt.format(*text)
333 ret = fmt.decode(
"utf-8").encode(console_encoding).format(*text)
335 except EnvironmentError:
336 ret = fmt.decode(
"utf-8").encode(console_encoding).format(*text)
343 cp = print_style.engine()
349 cp = print_style.engine()
355 """ run as a executable """
356 if __name__ ==
"__main__":
357 from optparse
import OptionParser
359 usage =
"usage: %prog [options...] <format message> [format parameters...]"
360 parser = OptionParser(usage)
361 parser.disable_interspersed_args()
367 help=
"show version and exit",
375 "set font color.(any of: black, blue, green, cyan, red, magenta, yellow, white)",
381 "--background-color",
384 "set background color.(any of: black, blue, green, cyan, red, magenta, yellow, white)",
385 metavar=
"<background color>",
386 dest=
"background_color",
391 action=
"append_const",
392 help=
"set font weight to bold",
393 const=print_style.FW_BOLD,
400 help=
"set mode.(any of: auto, term, win32_console, none, html)",
401 metavar=
"<output mode>",
408 help=
"set output stream.(any of: stdout, stderr)",
417 "enable interpretation of backslash escapes(just like echo command in unix like system)",
423 action=
"store_false",
425 "disable interpretation of backslash escapes(just like echo command in unix like system)",
432 help=
"set theme in html mode(light or dark)",
438 (options, left_args) = parser.parse_args()
440 print_stream =
"stdout"
443 fc_list = [
"FC_" + x.upper()
for x
in options.color
or []]
444 bk_list = [
"BC_" + y.upper()
for y
in options.background_color
or []]
445 for style_list
in [fc_list, bk_list]:
446 for style_name
in style_list:
447 if style_name
in print_style.__dict__:
448 print_options.append(print_style.__dict__[style_name])
450 for style_code
in options.style
or []:
451 print_options.append(style_code)
462 print(print_style.version)
463 print(
"Color Engine: " + print_style.engine.name)
466 if len(left_args) > 0:
467 if options.interp_bse:
468 for i
in range(0, len(left_args)):
469 left_args[i] = eval(repr(left_args[i]).replace(
"\\\\",
"\\"))
470 if "stdout" == options.ostream:
def stdout_with_color(self, options, text)
def stderr_with_color(self, options, text)
def stdout_with_color(self, options, text)
def stderr_with_color(self, options, text)
def stdout_with_color(self, options, text)
def stderr_with_color(self, options, text)
def stdout_with_color(self, options, text)
def get_cmd_color(self, handle=std_out_handle)
def set_cmd_color(self, color, handle=std_out_handle)
def stderr_with_color(self, options, text)
def __contains__(self, value)
def cprintf_unpack_text(fmt, text)
def cprintf_stdout(options, fmt, *text)
def cprintf_resolve_auto_mode()
def cprintf_set_theme(theme_name=None)
def cprintf_set_mode(mode_name="auto")
def cprintf_stderr(options, fmt, *text)