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

Conversation

Slayther
Copy link

No description provided.

@Slayther Slayther changed the title Added support for Win 10 cmd Added Win 10 native ANSI cmd support Aug 15, 2016
@Slayther
Copy link
Author

Not sure why on_windows check didn't prevent check_win_10_ansi_support from running..

@Slayther Slayther changed the title Added Win 10 native ANSI cmd support Add Win 10 native ANSI cmd support Aug 15, 2016
@@ -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].

# 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

@@ -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.

@Slayther
Copy link
Author

Slayther commented Oct 18, 2016

Well, it seems that the checks assume that Windows needs strip and convert. Not sure what I should do here. @wiggin15

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)
convert = not windows_ansi_support or (conversion_supported and not is_stream_closed(wrapped) and is_a_tty(wrapped))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you sure this should be "or"-ed and not "and"-ed? This way if the old condition with conversion_supported is True, then convert will be True even if windows_ansi_support is True, which (I think) is not the desired outcome. I think that there is the same problem with strip (but I may be mistaken). This is a difficult part.

Copy link
Author

Choose a reason for hiding this comment

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

I'm not really sure. That's why I put the check on the if line at first. I tried several combinations, but couldn't find one that fits for all cases.

Copy link
Author

Choose a reason for hiding this comment

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

I think we should have separate ifs, one for on_windows and one for other platforms.

@@ -21,6 +21,23 @@ def is_a_tty(stream):
return hasattr(stream, 'isatty') and stream.isatty()


def check_windows_ansi_support():
try:
import winreg
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is a problem here because winreg only exists in Python 3 but we support Python 2 too. Also, this function fails on older versions of Windows because the ReleaseID value doesn't exist in the registry. I am reluctant to pull this function - too much platform specific code has a high chance of breaking things.

Copy link
Author

Choose a reason for hiding this comment

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

I worked with Python 2 when I encountered the error and fixed it with this check function. Though, I agree it contains a lot of platform specific code, but I don't know what else would work.

Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure why I can't import it anymore. I am sure it worked in Python 2.7.x

Choose a reason for hiding this comment

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

The module is named _winreg on Python 2.7 (notice the leading underscore). https://docs.python.org/2/library/_winreg.html

@refi64
Copy link

refi64 commented Oct 18, 2016

I mentioned this in the review comments, but, just in case it got skipped over:

sys.getwindowsversion()[2] can be used to retrieve the Windows build number on Python versions as far back as 2.3. So check_windows_ansi_support can just be:

def check_windows_ansi_support():
    return sys.getwindowsversion()[2] >= 1511

@wiggin15
Copy link
Collaborator

On Windows 7 this returns True. The check should include the major and minor, not just the build. A tuple comparison should work.

@MinchinWeb
Copy link

This would be wonderful to have! Powershell (on Windows 10) now supports 24 bit color, but when colorama is initialized, we get dropped back to 16 color mode.

Below is the same script, the first run without colorama, the second with colorama initialized.

image

@MinchinWeb
Copy link

Adding to the complexity, this new functionality only works on Powershell, and not on CMD...

@mixmastamyk
Copy link

mixmastamyk commented Oct 5, 2018

@kirbyfan64

The build number is not the same as the ReleaseID, but they correspond:

So the code should look like this:

    def check_windows_ansi_support():
        return sys.getwindowsversion()[2] >= 10586

@tartley
Copy link
Owner

tartley commented Oct 13, 2020

Hey. FYI, yesterday I created a PR to test releases before we push them to PyPI. When that is merged, I'll be more confident about resuming merges and releases. I'll try to look at this PR soon. Thank you for creating it!

@tartley
Copy link
Owner

tartley commented Oct 7, 2021

I think this PR is a different way of implementing the same feature as is done by #139, is that right?

I have no idea how to decide which is the best way of doing it. I'll have a closer look soon, but if anyone has any insight, please tell.

Apologies for ignoring this (and all other PRs) for years. :-D

@tartley
Copy link
Owner

tartley commented Oct 7, 2021

Also, we'd need corresponding test changes before this could merge.

@njsmith
Copy link
Collaborator

njsmith commented Aug 25, 2022

#139 uses a more direct/reliable strategy to handle the ANSI stuff, so closing this in favor of it

@njsmith njsmith closed this Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants