setup_color.py 4.5 KB

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