-
Notifications
You must be signed in to change notification settings - Fork 96
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
Enable Virtual Terminal mode on legacy Windows terminal to support ANSI escape sequences #265
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
|
||
""" | ||
Enable Virtual Terminal mode on legacy Windows terminal to support ANSI escape sequences. | ||
Migrated from https://github.com/Azure/azure-cli/pull/12942 | ||
""" | ||
|
||
from ctypes import WinDLL, get_last_error, byref | ||
from ctypes.wintypes import HANDLE, LPDWORD, DWORD | ||
from msvcrt import get_osfhandle # pylint: disable=import-error | ||
from knack.log import get_logger | ||
|
||
logger = get_logger(__name__) | ||
|
||
ERROR_INVALID_PARAMETER = 0x0057 | ||
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004 | ||
|
||
|
||
def _check_zero(result, _, args): | ||
if not result: | ||
raise OSError(get_last_error()) | ||
return args | ||
|
||
|
||
# See: | ||
# - https://docs.microsoft.com/en-us/windows/console/getconsolemode | ||
# - https://docs.microsoft.com/en-us/windows/console/setconsolemode | ||
kernel32 = WinDLL("kernel32", use_last_error=True) | ||
kernel32.GetConsoleMode.errcheck = _check_zero | ||
kernel32.GetConsoleMode.argtypes = (HANDLE, LPDWORD) | ||
kernel32.SetConsoleMode.errcheck = _check_zero | ||
kernel32.SetConsoleMode.argtypes = (HANDLE, DWORD) | ||
|
||
|
||
def _get_conout_mode(): | ||
with open("CONOUT$", "w") as conout: # pylint: disable=unspecified-encoding | ||
mode = DWORD() | ||
conout_handle = get_osfhandle(conout.fileno()) | ||
kernel32.GetConsoleMode(conout_handle, byref(mode)) | ||
return mode.value | ||
|
||
|
||
def _set_conout_mode(mode): | ||
with open("CONOUT$", "w") as conout: # pylint: disable=unspecified-encoding | ||
conout_handle = get_osfhandle(conout.fileno()) | ||
kernel32.SetConsoleMode(conout_handle, mode) | ||
|
||
|
||
def _update_conout_mode(mode): | ||
old_mode = _get_conout_mode() | ||
if old_mode & mode != mode: | ||
mode = old_mode | mode # pylint: disable=unsupported-binary-operation | ||
_set_conout_mode(mode) | ||
|
||
|
||
def enable_vt_mode(): | ||
"""Enables virtual terminal mode for Windows 10 console. | ||
|
||
Windows 10 supports VT (virtual terminal) / ANSI escape sequences since version 1607. | ||
|
||
cmd.exe enables VT mode, but only for itself. It disables VT mode before starting other programs, | ||
and also at shutdown (See: https://bugs.python.org/issue30075). | ||
|
||
Return True if success, else False. | ||
""" | ||
try: | ||
_update_conout_mode(ENABLE_VIRTUAL_TERMINAL_PROCESSING) | ||
return True | ||
except OSError as e: | ||
if e.errno == ERROR_INVALID_PARAMETER: | ||
logger.debug("Unable to enable virtual terminal processing for legacy Windows terminal.") | ||
else: | ||
logger.debug("Unable to enable virtual terminal processing: %s.", e.errno) | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
argcomplete==1.12.2 | ||
colorama==0.4.4 | ||
flake8==4.0.1 | ||
jmespath==0.10.0 | ||
Pygments==2.8.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any side effect if we don't disable it.