-
-
Notifications
You must be signed in to change notification settings - Fork 50
Feature/color on win #57
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
Merged
samuelcolvin
merged 27 commits into
samuelcolvin:master
from
Cielquan:feature/color-on-win
Jun 16, 2020
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
6d14e83
refactored debug.Debug._env_bool to prettier.env_bool
Cielquan 292d30b
added tests for prettier.env_bool
Cielquan 34e6b69
added highlight module
Cielquan 2e6764c
implement use_highlight function in Debug cls
Cielquan 62ba8a6
added tests for highlight module to test_main
Cielquan 1d1f056
fixed imports
Cielquan 356f2a9
added type hints to `env_true` and `env_bool`
Cielquan 598bc12
updated `test_env_bool` because of type hints
Cielquan f2eb27e
change double quotes to single quotes in PR code
Cielquan a6b9150
added 'windows' to ci
Cielquan 6997084
added xfail to test failing on win
Cielquan 9fa9b3f
changed " to ' in test_expr_render
Cielquan 4fb13e4
refactored highlight.py into utils.py; refactored sys test for win in…
Cielquan bcfd996
changed " to ' in test_main
Cielquan 3025340
added covdefaults pkg
Cielquan 0199463
fixed lint
Cielquan 0a793fc
fixed lint (2)
Cielquan 37555cd
refactored env_bool & env_true from prettier into utils; movedd tests…
Cielquan 7a7b73a
fixed lint
Cielquan 7073538
fixed type hints in utils
Cielquan c312ee7
fixed Debug.__call__ rebase error
Cielquan 940dd3f
added cov req = 0 to pytest cmd
Cielquan 83df9c2
removed covdefaults; added nocover to win parts
Cielquan a5ea9e0
fixed make install
Cielquan 3c2b126
fixed double quotes to single
Cielquan 2718027
changed docstring format
Cielquan 9ec2d1b
refactored _enable_vt_mode subfunction into activate_win_color function
Cielquan 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,11 +1,98 @@ | ||
import os | ||
import sys | ||
|
||
__all__ = ('isatty',) | ||
|
||
MYPY = False | ||
if MYPY: | ||
from typing import Optional | ||
|
||
|
||
def isatty(stream=None): | ||
stream = stream or sys.stdout | ||
try: | ||
return stream.isatty() | ||
except Exception: | ||
return False | ||
|
||
|
||
def env_true(var_name: str, alt: 'Optional[bool]' = None) -> 'Optional[bool]': | ||
env = os.getenv(var_name, None) | ||
if env: | ||
return env.upper() in {'1', 'TRUE'} | ||
else: | ||
return alt | ||
|
||
|
||
def env_bool(value: 'Optional[bool]', env_name: str, env_default: 'Optional[bool]') -> 'Optional[bool]': | ||
if value is None: | ||
return env_true(env_name, env_default) | ||
else: | ||
return value | ||
|
||
|
||
def activate_win_color() -> bool: # pragma: no cover | ||
""" | ||
Activate ANSI support on windows consoles. | ||
|
||
As of Windows 10, the windows conolse got some support for ANSI escape | ||
sequences. Unfortunately it has to be enabled first using `SetConsoleMode`. | ||
See: https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences | ||
|
||
Code snippet source: https://bugs.python.org/msg291732 | ||
""" | ||
import os | ||
import msvcrt | ||
import ctypes | ||
|
||
from ctypes import wintypes | ||
|
||
def _check_bool(result, func, args): | ||
if not result: | ||
raise ctypes.WinError(ctypes.get_last_error()) | ||
return args | ||
|
||
ERROR_INVALID_PARAMETER = 0x0057 | ||
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004 | ||
|
||
LPDWORD = ctypes.POINTER(wintypes.DWORD) | ||
|
||
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) | ||
kernel32.GetConsoleMode.errcheck = _check_bool | ||
kernel32.GetConsoleMode.argtypes = (wintypes.HANDLE, LPDWORD) | ||
kernel32.SetConsoleMode.errcheck = _check_bool | ||
kernel32.SetConsoleMode.argtypes = (wintypes.HANDLE, wintypes.DWORD) | ||
|
||
def _set_conout_mode(new_mode, mask=0xFFFFFFFF): | ||
# don't assume StandardOutput is a console. | ||
# open CONOUT$ instead | ||
fdout = os.open('CONOUT$', os.O_RDWR) | ||
try: | ||
hout = msvcrt.get_osfhandle(fdout) | ||
old_mode = wintypes.DWORD() | ||
kernel32.GetConsoleMode(hout, ctypes.byref(old_mode)) | ||
mode = (new_mode & mask) | (old_mode.value & ~mask) | ||
kernel32.SetConsoleMode(hout, mode) | ||
return old_mode.value | ||
finally: | ||
os.close(fdout) | ||
|
||
mode = mask = ENABLE_VIRTUAL_TERMINAL_PROCESSING | ||
try: | ||
_set_conout_mode(mode, mask) | ||
except WindowsError as e: | ||
if e.winerror == ERROR_INVALID_PARAMETER: | ||
return False | ||
raise | ||
return True | ||
|
||
|
||
def use_highlight(highlight: 'Optional[bool]' = None, file_=None) -> bool: | ||
highlight = env_bool(highlight, 'PY_DEVTOOLS_HIGHLIGHT', None) | ||
|
||
if highlight is not None: | ||
return highlight | ||
|
||
if sys.platform == 'win32': # pragma: no cover | ||
return isatty(file_) and activate_win_color() | ||
return isatty(file_) |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.