From 3c93185cd3789bbbfc6d7b49349ecf1f6a08cd7f Mon Sep 17 00:00:00 2001 From: Slayther Date: Mon, 15 Aug 2016 14:40:23 +0200 Subject: [PATCH 1/4] Added support for Win 10 cmd --- colorama/ansitowin32.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/colorama/ansitowin32.py b/colorama/ansitowin32.py index b7ff6f2..a973ffe 100644 --- a/colorama/ansitowin32.py +++ b/colorama/ansitowin32.py @@ -21,6 +21,20 @@ def is_a_tty(stream): return hasattr(stream, 'isatty') and stream.isatty() +def check_win_10_ansi_support(): + import 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 + return releaseId >= 1511 + + class StreamWrapper(object): ''' Wraps a stream (such as stdout), acting as a transparent proxy for all @@ -66,13 +80,16 @@ 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? + win_10_ansi_support = on_windows and check_win_10_ansi_support() + # should we strip ANSI sequences from our output? - if strip is None: + if strip is None and not win_10_ansi_support: 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: + if convert is None and not win_10_ansi_support: convert = conversion_supported and not is_stream_closed(wrapped) and is_a_tty(wrapped) self.convert = convert From 053989a4fea8d950a5028141b54a747dd5f4abc4 Mon Sep 17 00:00:00 2001 From: Slayther Date: Mon, 15 Aug 2016 19:21:49 +0200 Subject: [PATCH 2/4] Fixed import error --- colorama/ansitowin32.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/colorama/ansitowin32.py b/colorama/ansitowin32.py index a973ffe..ce93ada 100644 --- a/colorama/ansitowin32.py +++ b/colorama/ansitowin32.py @@ -22,7 +22,10 @@ def is_a_tty(stream): def check_win_10_ansi_support(): - import winreg + try: + import winreg + except ImportError: + return False # Check the registry for release ID key = r"SOFTWARE\Microsoft\Windows NT\CurrentVersion" From 083ccdbfaf7963cf3e63a37f2217e06ff8e69ffa Mon Sep 17 00:00:00 2001 From: Slayther Date: Tue, 18 Oct 2016 20:06:41 +0200 Subject: [PATCH 3/4] Minor changes --- colorama/ansitowin32.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/colorama/ansitowin32.py b/colorama/ansitowin32.py index b9ac12b..5d91761 100644 --- a/colorama/ansitowin32.py +++ b/colorama/ansitowin32.py @@ -21,7 +21,7 @@ def is_a_tty(stream): return hasattr(stream, 'isatty') and stream.isatty() -def check_win_10_ansi_support(): +def check_windows_ansi_support(): try: import winreg except ImportError: @@ -87,16 +87,16 @@ def __init__(self, wrapped, convert=None, strip=None, autoreset=False): conversion_supported = on_windows and winapi_test() # Does this version of Win 10 support ANSI? - win_10_ansi_support = on_windows and check_win_10_ansi_support() + windows_ansi_support = on_windows and check_windows_ansi_support() # should we strip ANSI sequences from our output? - if strip is None and not win_10_ansi_support: - strip = conversion_supported or (not is_stream_closed(wrapped) and not is_a_tty(wrapped)) + if strip is None: + strip = conversion_supported or not windows_ansi_support 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 and not win_10_ansi_support: - convert = conversion_supported and not is_stream_closed(wrapped) and is_a_tty(wrapped) + if convert is None: + convert = not windows_ansi_support or (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 From 344b08e6771f9b9e104cb9414f6bffb62d43ed87 Mon Sep 17 00:00:00 2001 From: MinchinWeb Date: Sat, 10 Jun 2017 23:25:32 -0600 Subject: [PATCH 4/4] Support pass thru ANSI codes on Powershell but not CMD --- colorama/ansitowin32.py | 26 ++++++++++++++++++++++---- setup.py | 5 ++++- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/colorama/ansitowin32.py b/colorama/ansitowin32.py index 5d91761..17e4a64 100644 --- a/colorama/ansitowin32.py +++ b/colorama/ansitowin32.py @@ -25,7 +25,12 @@ def check_windows_ansi_support(): try: import winreg except ImportError: - return False + 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" @@ -35,7 +40,14 @@ def check_windows_ansi_support(): winreg.CloseKey(key) # Windows 10 supports ANSI cmd since release 1511 - return releaseId >= 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): @@ -91,12 +103,18 @@ def __init__(self, wrapped, convert=None, strip=None, autoreset=False): # should we strip ANSI sequences from our output? if strip is None: - strip = conversion_supported or not windows_ansi_support 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 = not windows_ansi_support or (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'], + }, )