setup_color.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2016 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import ctypes
  6. import platform
  7. import subprocess
  8. import sys
  9. from third_party import colorama
  10. IS_TTY = None
  11. OUT_TYPE = 'unknown'
  12. def enable_native_ansi():
  13. """Enables native ANSI sequences in console. Windows 10 only.
  14. Returns whether successful.
  15. """
  16. kernel32 = ctypes.windll.kernel32
  17. ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x04
  18. out_handle = kernel32.GetStdHandle(subprocess.STD_OUTPUT_HANDLE)
  19. # GetConsoleMode fails if the terminal isn't native.
  20. mode = ctypes.wintypes.DWORD()
  21. if kernel32.GetConsoleMode(out_handle, ctypes.byref(mode)) == 0:
  22. return False
  23. if not (mode.value & ENABLE_VIRTUAL_TERMINAL_PROCESSING):
  24. if kernel32.SetConsoleMode(
  25. out_handle,
  26. mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0:
  27. print('kernel32.SetConsoleMode to enable ANSI sequences failed',
  28. file=sys.stderr)
  29. return False
  30. return True
  31. def init():
  32. # should_wrap instructs colorama to wrap stdout/stderr with an ANSI
  33. # colorcode interpreter that converts them to SetConsoleTextAttribute calls.
  34. # This only should be True in cases where we're connected to cmd.exe's
  35. # console. Setting this to True on non-windows systems has no effect.
  36. should_wrap = False
  37. global IS_TTY, OUT_TYPE
  38. IS_TTY = sys.stdout.isatty()
  39. is_windows = sys.platform.startswith('win')
  40. if IS_TTY:
  41. # Yay! We detected a console in the normal way. It doesn't really matter
  42. # if it's windows or not, we win.
  43. OUT_TYPE = 'console'
  44. should_wrap = True
  45. elif is_windows:
  46. # assume this is some sort of file
  47. OUT_TYPE = 'file (win)'
  48. import msvcrt
  49. h = msvcrt.get_osfhandle(sys.stdout.fileno())
  50. # h is the win32 HANDLE for stdout.
  51. ftype = ctypes.windll.kernel32.GetFileType(h)
  52. if ftype == 2: # FILE_TYPE_CHAR
  53. # This is a normal cmd console, but we'll only get here if we're
  54. # running inside a `git command` which is actually
  55. # git->bash->command. Not sure why isatty doesn't detect this case.
  56. OUT_TYPE = 'console (cmd via msys)'
  57. IS_TTY = True
  58. should_wrap = True
  59. elif ftype == 3: # FILE_TYPE_PIPE
  60. OUT_TYPE = 'pipe (win)'
  61. # This is some kind of pipe on windows. This could either be a real
  62. # pipe or this could be msys using a pipe to emulate a pty. We use
  63. # the same algorithm that msys-git uses to determine if it's
  64. # connected to a pty or not.
  65. # This function and the structures are defined in the MSDN
  66. # documentation using the same names.
  67. def NT_SUCCESS(status):
  68. # The first two bits of status are the severity. The success
  69. # severities are 0 and 1, and the !success severities are 2 and
  70. # 3. Therefore since ctypes interprets the default restype of
  71. # the call to be an 'C int' (which is guaranteed to be signed 32
  72. # bits), All success codes are positive, and all !success codes
  73. # are negative.
  74. return status >= 0
  75. class UNICODE_STRING(ctypes.Structure):
  76. _fields_ = [('Length', ctypes.c_ushort),
  77. ('MaximumLength', ctypes.c_ushort),
  78. ('Buffer', ctypes.c_wchar_p)]
  79. class OBJECT_NAME_INFORMATION(ctypes.Structure):
  80. _fields_ = [('Name', UNICODE_STRING),
  81. ('NameBuffer', ctypes.c_wchar_p)]
  82. buf = ctypes.create_string_buffer(1024)
  83. # Ask NT what the name of the object our stdout HANDLE is. It would
  84. # be possible to use GetFileInformationByHandleEx, but it's only
  85. # available on Vista+. If you're reading this in 2017 or later, feel
  86. # free to refactor this out.
  87. #
  88. # The '1' here is ObjectNameInformation
  89. if NT_SUCCESS(
  90. ctypes.windll.ntdll.NtQueryObject(h, 1, buf,
  91. len(buf) - 2, None)):
  92. out = OBJECT_NAME_INFORMATION.from_buffer(buf)
  93. name = out.Name.Buffer.split('\\')[-1]
  94. IS_TTY = name.startswith('msys-') and '-pty' in name
  95. if IS_TTY:
  96. OUT_TYPE = 'bash (msys)'
  97. else:
  98. # A normal file, or an unknown file type.
  99. pass
  100. else:
  101. # This is non-windows, so we trust isatty.
  102. OUT_TYPE = 'pipe or file'
  103. if IS_TTY and is_windows:
  104. # Wrapping may cause errors on some Windows versions
  105. # (crbug.com/1114548).
  106. if platform.release() != '10' or enable_native_ansi():
  107. should_wrap = False
  108. colorama.init(wrap=should_wrap)
  109. if __name__ == '__main__':
  110. init()
  111. print('IS_TTY:', IS_TTY)
  112. print('OUT_TYPE:', OUT_TYPE)