Skip to content

Commit 7fb8817

Browse files
authored
Fix enabling of windows color terminal processing (emscripten-core#25529)
This was broken in emscripten-core#25502 and we only have limited testing of this features. Specifically we only test that the ANSI codes are generated, not that they are successfully processed. The change here is not only to check for ENABLE_VIRTUAL_TERMINAL_PROCESSING mode but to actually set this mode. Replaces emscripten-core#25525
1 parent 443352e commit 7fb8817

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

tools/colored_logger.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,16 @@ def ansi_color_available():
6767
kernel32 = ctypes.windll.kernel32
6868
stdout_handle = kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
6969

70-
# Get the current console mode
7170
console_mode = ctypes.c_uint()
72-
if not kernel32.GetConsoleMode(stdout_handle, ctypes.byref(console_mode)):
73-
# Handle error if GetConsoleMode fails
71+
# Attempt to enable ANSI color processing (ENABLE_VIRTUAL_TERMINAL_PROCESSING).
72+
# Assume that failure of either GetConsoleMode or SetConsoleMode means that stdout
73+
# is not attached to a terminal or that the terminal does not support this mode.
74+
if kernel32.GetConsoleMode(stdout_handle, ctypes.byref(console_mode)) == 0:
75+
return False
76+
if kernel32.SetConsoleMode(stdout_handle, console_mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0:
7477
return False
7578

76-
# Check if the flag is set in the current console mode
77-
return (console_mode.value & ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0
79+
return True
7880

7981

8082
def add_coloring_to_emit_ansi(fn):
@@ -101,13 +103,15 @@ def new(*args):
101103

102104
def enable(force=False):
103105
global color_enabled
104-
if force or ansi_color_available():
105-
logging.StreamHandler.emit = add_coloring_to_emit_ansi(logging.StreamHandler.emit)
106-
color_enabled = True
106+
if not color_enabled:
107+
if ansi_color_available() or force:
108+
logging.StreamHandler.emit = add_coloring_to_emit_ansi(logging.StreamHandler.emit)
109+
color_enabled = True
107110

108111

109112
def disable():
110113
global color_enabled
111-
if hasattr(logging.StreamHandler.emit, 'orig_func'):
112-
logging.StreamHandler.emit = logging.StreamHandler.emit.orig_func
113-
color_enabled = False
114+
if color_enabled:
115+
if hasattr(logging.StreamHandler.emit, 'orig_func'):
116+
logging.StreamHandler.emit = logging.StreamHandler.emit.orig_func
117+
color_enabled = False

0 commit comments

Comments
 (0)