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/+/1785605
Reviewed-by: Robbie Iannucci <iannucci@chromium.org>
Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: Marc-Antoine Ruel <maruel@chromium.org>
Commit-Queue: Raul Tambre <raul@tambre.ee>
Auto-Submit: Raul Tambre <raul@tambre.ee>
changes/05/1785605/4
Raul Tambre 6 years ago committed by Commit Bot
parent 9d25ad4192
commit 73ec83f0fe

@ -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__':

Loading…
Cancel
Save