Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(574)

Unified Diff: setup_color.py

Issue 1851283002: Fix coloring madness in depot_tools. (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Work on non-windows Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« git_common.py ('K') | « git_map_branches.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: setup_color.py
diff --git a/setup_color.py b/setup_color.py
new file mode 100755
index 0000000000000000000000000000000000000000..0235ba8277ef2be9dca5513eeda23daee46418c4
--- /dev/null
+++ b/setup_color.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+# Copyright (c) 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import os
+import sys
+from third_party import colorama
+
+IS_TTY = None
+OUT_TYPE = 'unknown'
+
+def init():
+ should_wrap = True
+ global IS_TTY, OUT_TYPE
+ IS_TTY = sys.stdout.isatty()
+ if IS_TTY:
+ OUT_TYPE = 'console'
iannucci 2016/04/02 06:08:20 yay, we detected it via the normal mechanism! This
brucedawson 2016/04/02 20:42:50 Can you make this explanation and others comments
iannucci 2016/04/02 23:45:34 Yeah should have done that in the first place :).
+ elif sys.platform.startswith('win'):
+ OUT_TYPE = 'pipe'
iannucci 2016/04/02 06:08:21 Assume it's a pipe
+
+ import msvcrt
+ import ctypes
+ h = msvcrt.get_osfhandle(sys.stdout.fileno())
iannucci 2016/04/02 06:08:20 h is the win32 HANDLE for stdout.
+ ftype = ctypes.windll.kernel32.GetFileType(h)
+ if ftype == 2: # FILE_TYPE_CHAR
iannucci 2016/04/02 06:08:20 This happens when were running in cmd but via some
+ OUT_TYPE = 'console (cmd via msys)'
+ IS_TTY = True
+ elif ftype == 3: # FILE_TYPE_PIPE
iannucci 2016/04/02 06:08:21 This happens if we're running under bash in intera
+ OUT_TYPE = 'pipe'
+ def NT_SUCCESS(status):
+ if status >= 0 and status <= 0x3FFFFFFF:
+ return True
+ if status >= 0x40000000 and status <= 0x7FFFFFFF:
+ return True
+ return False
+
+ class UNICODE_STRING(ctypes.Structure):
+ _fields_ = [('Length', ctypes.c_ushort),
+ ('MaximumLength', ctypes.c_ushort),
+ ('Buffer', ctypes.c_wchar_p)]
+
+ class OBJECT_NAME_INFORMATION(ctypes.Structure):
+ _fields_ = [('Name', UNICODE_STRING),
+ ('NameBuffer', ctypes.c_wchar_p)]
iannucci 2016/04/02 06:08:21 These are all defined in the MSDN documentation un
+
+ # 1: ObjectNameInformation
+ buf = ctypes.create_string_buffer('\0', 1024)
+ if NT_SUCCESS(ctypes.windll.ntdll.NtQueryObject(h, 1, buf, len(buf)-2,
iannucci 2016/04/02 06:08:21 Ask NT what the name of thing attached to the stdo
+ None)):
+ # MSYS console, but not an MSYS pipe
+ out = OBJECT_NAME_INFORMATION.from_buffer(buf)
+ name = out.Name.Buffer.split('\\')[-1]
+ IS_TTY = name.startswith('msys-') and '-pty' in name
iannucci 2016/04/02 06:08:21 The full name is something like "\Device\NamedPipe
+ should_wrap = False
iannucci 2016/04/02 06:08:21 either way, we don't need to do the weird win32 sy
+ if IS_TTY:
+ OUT_TYPE = 'console (msys)'
+ else:
iannucci 2016/04/02 06:08:20 Not on windows, so isatty is reliable.
+ OUT_TYPE = 'pipe'
+
+ colorama.init(wrap=should_wrap)
+
+if __name__ == '__main__':
+ init()
+ print 'IS_TTY:', IS_TTY
+ print 'OUT_TYPE:', OUT_TYPE
« git_common.py ('K') | « git_map_branches.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698