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

fix checking Windows console features #3598

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rich/_win32_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from rich.style import Style

STDOUT = -11
STDERR = -12
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4

COORD = wintypes._COORD
Expand Down
38 changes: 22 additions & 16 deletions rich/_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class WindowsConsoleFeatures:

from rich._win32_console import (
ENABLE_VIRTUAL_TERMINAL_PROCESSING,
STDERR,
STDOUT,
GetConsoleMode,
GetStdHandle,
LegacyWindowsError,
Expand All @@ -43,22 +45,26 @@ def get_windows_console_features() -> WindowsConsoleFeatures:
Returns:
WindowsConsoleFeatures: An instance of WindowsConsoleFeatures.
"""
handle = GetStdHandle()
try:
console_mode = GetConsoleMode(handle)
success = True
except LegacyWindowsError:
console_mode = 0
success = False
vt = bool(success and console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)
truecolor = False
if vt:
win_version = sys.getwindowsversion()
truecolor = win_version.major > 10 or (
win_version.major == 10 and win_version.build >= 15063
)
features = WindowsConsoleFeatures(vt=vt, truecolor=truecolor)
return features
for h in (STDOUT, STDERR):
handle = GetStdHandle(h)
try:
console_mode = GetConsoleMode(handle)
success = True
except LegacyWindowsError:
console_mode = 0
success = False
if not success:
continue
vt = bool(console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)
truecolor = False
if vt:
win_version = sys.getwindowsversion()
truecolor = win_version.major > 10 or (
win_version.major == 10 and win_version.build >= 15063
)
features = WindowsConsoleFeatures(vt=vt, truecolor=truecolor)
return features
return WindowsConsoleFeatures()


if __name__ == "__main__":
Expand Down
Loading