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

Provide pass-through ANSI on Windows 10 powershell #133

Closed
wants to merge 5 commits into from

Conversation

MinchinWeb
Copy link

@MinchinWeb MinchinWeb commented Jun 11, 2017

This provides pass-thru to ANSI codes on new enough Windows 10 versions, and to Powershell only. It does this by using psutil to check the process name that is running python, and if that matches powershell.exe, ANSI pass-thru is enabled.

This does add psutil as a dependency on Windows.

Closes #105

Basic local testing on both Python 2.7 and Python 3.6

@MinchinWeb
Copy link
Author

Pictures!

CMD (so only 16 color mode, but that works):

image

Powershell (full 256 color glory!):

image

@segevfiner
Copy link
Contributor

segevfiner commented Jul 14, 2017

@MinchinWeb Are you sure this only works in powershell? I think you simply need to enable ENABLE_VIRTUAL_TERMINAL_PROCESSING.

image

0x4 is ENABLE_VIRTUAL_TERMINAL_PROCESSING. cmd has a weird behavior that powershell doesn't have. It enables ENABLE_VIRTUAL_TERMINAL_PROCESSING on start, disables it when it starts a child program and doesn't disable it on exit (If you start cmd as a child of Python it will leak ENABLE_VIRTUAL_TERMINAL_PROCESSING). powershell just enables it on start and leaves it at that.

Reference: https://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx

Copy link
Contributor

@segevfiner segevfiner left a comment

Choose a reason for hiding this comment

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

Note: I'm not the maintainer of colorama.

winreg.CloseKey(key)

# Windows 10 supports ANSI cmd since release 1511
if releaseId >= 1511:
Copy link
Contributor

Choose a reason for hiding this comment

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

You probably need to check that this is Windows 10 too, I'm not sure ReleaseID isn't used in previous versions of Windows. l Windows since 8.1 has a nasty habit of lying about it's version though. I think platform.win32_ver() does work around that but be sure to check (sys.getwindowsversion doesn't workaround this at least on Python 2.7.13 so you get the version of Windows 8).

# Windows 10 supports ANSI cmd since release 1511
if releaseId >= 1511:
# but only on powershell
import psutil
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this isn't really related to powershell but to the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag. colorama should enable this flag when supported and restore it's previous value on exit.

I think we can simply try to SetConsoleMode(h, GetConsoleMode(h) | ENABLE_VIRTUAL_TERMINAL_PROCESSING) to check for support of this (Need to be sure the stream is a tty/console before calling {Get,Set}ConsoleMode). I think it fails with INVALID_ARGUMENT when unsupported. But this needs some testing.

See: #133 (comment) & https://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx

@@ -64,6 +64,9 @@ def get_version(path):
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Topic :: Terminals',
]
],
extras_require={
Copy link
Contributor

Choose a reason for hiding this comment

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

Indentation is messed up.

# 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
Copy link
Contributor

Choose a reason for hiding this comment

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

You still need to check that the stream isn't closed and is a tty otherwise we will skip stripping ansi codes with redirected output. Be careful, all those boolean conditions are quite confusing.

# Check the registry for release ID
key = r"SOFTWARE\Microsoft\Windows NT\CurrentVersion"
val = r"ReleaseID"
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key)
Copy link
Contributor

Choose a reason for hiding this comment

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

Use try finally (It is a context manager since 2.6 but I think colorama still declares compatibility with Python 2.5).

@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

This seems like it fixes a genuine problem, and if this is the best way to detect powershell then I'd like to merge, but I am hazy on whether this change is still needed if we merge #139.

I'll experiment with them both later this week. If anyone has any insight (I do not know what I'm doing) I'd be grateful.

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

@tartley
Copy link
Owner

tartley commented Oct 7, 2021

Also: We can't merge this until someone provides corresponding changes to the tests.

@MinchinWeb
Copy link
Author

I think that if #139 is robust, that would be preferred just because it doesn't add another dependency to the project. But I'd be happy to see either merged!

@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

5 participants