Commit 73ec83f0 authored by Raul Tambre's avatar Raul Tambre Committed by Commit Bot

setup_color: Don't output an error if GetConsoleMode fails

Turns out GetConsoleMode fails if the user's console isn't native. This is sometimes the case for Git Bash.

Also moved the checking code out of the if block so it applies in generic console cases too.
This went unnoticed, because setup_color.py is currently only really used in Git scripts, which are piped.

Bug: 1001187
Change-Id: I93357e479f84122f759e419d15c02500809657d3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1785605Reviewed-by: 's avatarRobbie Iannucci <iannucci@chromium.org>
Reviewed-by: 's avatarEdward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: 's avatarMarc-Antoine Ruel <maruel@chromium.org>
Commit-Queue: Raul Tambre <raul@tambre.ee>
Auto-Submit: Raul Tambre <raul@tambre.ee>
parent 9d25ad41
......@@ -25,15 +25,18 @@ def enable_native_ansi():
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x04
out_handle = kernel32.GetStdHandle(subprocess.STD_OUTPUT_HANDLE)
# GetConsoleMode fails if the terminal isn't native.
mode = ctypes.wintypes.DWORD()
if kernel32.GetConsoleMode(out_handle, ctypes.byref(mode)) == 0:
print('kernel32.GetConsoleMode failed')
return False
if not (mode.value & ENABLE_VIRTUAL_TERMINAL_PROCESSING):
if kernel32.SetConsoleMode(
out_handle, mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0:
print('kernel32.SetConsoleMode to enable ANSI sequences failed')
print(
'kernel32.SetConsoleMode to enable ANSI sequences failed',
file=sys.stderr)
return False
return True
......@@ -110,14 +113,15 @@ def init():
else:
# A normal file, or an unknown file type.
pass
# Enable native ANSI color codes on Windows 10.
if IS_TTY and platform.release() == '10':
should_wrap = not enable_native_ansi()
else:
# This is non-windows, so we trust isatty.
OUT_TYPE = 'pipe or file'
# Enable native ANSI color codes on Windows 10.
if IS_TTY and platform.release() == '10':
if enable_native_ansi():
should_wrap = False
colorama.init(wrap=should_wrap)
if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment