diff --git a/colorama/ansitowin32.py b/colorama/ansitowin32.py index 8177d5b..17e4a64 100644 --- a/colorama/ansitowin32.py +++ b/colorama/ansitowin32.py @@ -21,6 +21,35 @@ def is_a_tty(stream): return hasattr(stream, 'isatty') and stream.isatty() +def check_windows_ansi_support(): + try: + import winreg + except ImportError: + try: + import _winreg # on Python 2 + except ImportError: + return False + else: + winreg = _winreg + + # Check the registry for release ID + key = r"SOFTWARE\Microsoft\Windows NT\CurrentVersion" + val = r"ReleaseID" + key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key) + releaseId = int(winreg.QueryValueEx(key,val)[0]) + winreg.CloseKey(key) + + # Windows 10 supports ANSI cmd since release 1511 + if releaseId >= 1511: + # but only on powershell + import psutil + python_pid = psutil.Process(os.getpid()) + shell_pid = psutil.Process(python_pid.ppid()) + return shell_pid.name() == 'powershell.exe' + else: + return False + + class StreamWrapper(object): ''' Wraps a stream (such as stdout), acting as a transparent proxy for all @@ -69,14 +98,23 @@ def __init__(self, wrapped, convert=None, strip=None, autoreset=False): # to support the ANSI codes. conversion_supported = on_windows and winapi_test() + # Does this version of Win 10 support ANSI? + windows_ansi_support = on_windows and check_windows_ansi_support() + # should we strip ANSI sequences from our output? if strip is None: - strip = conversion_supported or (not is_stream_closed(wrapped) and not is_a_tty(wrapped)) + if windows_ansi_support: + strip = False + else: + strip = conversion_supported or (not is_stream_closed(wrapped) and not is_a_tty(wrapped)) self.strip = strip # should we should convert ANSI sequences into win32 calls? if convert is None: - convert = conversion_supported and not is_stream_closed(wrapped) and is_a_tty(wrapped) + if windows_ansi_support: + convert = False + else: + convert = conversion_supported and not is_stream_closed(wrapped) and is_a_tty(wrapped) self.convert = convert # dict of ansi codes to win32 functions and parameters diff --git a/setup.py b/setup.py index 43225c2..82e542c 100644 --- a/setup.py +++ b/setup.py @@ -64,6 +64,9 @@ def get_version(path): 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Topic :: Terminals', - ] + ], +extras_require={ + ':sys_platform=="win32"': ['psutil'], + }, )