Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Win 10 native ANSI cmd support #105

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 19 additions & 2 deletions colorama/ansitowin32.py
Expand Up @@ -21,6 +21,20 @@ def is_a_tty(stream):
return hasattr(stream, 'isatty') and stream.isatty()


def check_win_10_ansi_support():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to replace the windows release check using the "platform" module? platform.win32_ver() or platform.platform()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't found a way to check the build version using the platform module.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Slayther Try sys.getwindowsversion()[2].

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
Expand Down Expand Up @@ -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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change the name to "windows_ansi_support", in case it will be more than just Windows 10 in the future :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, also changed the function name.


# should we strip ANSI sequences from our output?
if strip is None:
if strip is None and not win_10_ansi_support:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to add the win10 check inside the if, this way "strip" will always be True/False and not be left as None. Same for 'convert'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

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

Expand Down