Skip to content

Fixed legacy_windows is auto-detected as True incorrectly, when not in terminal environment #3648

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

Open
wants to merge 2 commits 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


### Fixed

- Fixed `legacy_windows` is auto-detected as `True` incorrectly, when not in terminal environment.

## [13.9.4] - 2024-11-01

### Changed
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ The following people have contributed to the development of Rich:
- [L. Yeung](https://github.com/lewis-yeung)
- [chthollyphile](https://github.com/chthollyphile)
- [Jonathan Helmus](https://github.com/jjhelmus)
- [xymy](https://github.com/xymy)
39 changes: 21 additions & 18 deletions rich/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,11 +585,6 @@ def get_windows_console_features() -> "WindowsConsoleFeatures": # pragma: no co
return _windows_console_features


def detect_legacy_windows() -> bool:
"""Detect legacy Windows."""
return WINDOWS and not get_windows_console_features().vt


class Console:
"""A high level console interface.

Expand Down Expand Up @@ -687,10 +682,17 @@ def __init__(
self._emoji = emoji
self._emoji_variant: Optional[EmojiVariant] = emoji_variant
self._highlight = highlight
self.legacy_windows: bool = (
(detect_legacy_windows() and not self.is_jupyter)
if legacy_windows is None
else legacy_windows

self._force_terminal = None
if force_terminal is not None:
self._force_terminal = force_terminal

self._file = file
self.quiet = quiet
self.stderr = stderr

self.legacy_windows = (
self._detect_legacy_windows() if legacy_windows is None else legacy_windows
)

if width is None:
Expand All @@ -707,15 +709,6 @@ def __init__(
self._height = height

self._color_system: Optional[ColorSystem]

self._force_terminal = None
if force_terminal is not None:
self._force_terminal = force_terminal

self._file = file
self.quiet = quiet
self.stderr = stderr

if color_system is None:
self._color_system = None
elif color_system == "auto":
Expand Down Expand Up @@ -788,6 +781,16 @@ def _theme_stack(self) -> ThemeStack:
"""Get the thread local theme stack."""
return self._thread_locals.theme_stack

def _detect_legacy_windows(self) -> bool:
"""Detect legacy Windows."""
if not WINDOWS:
return False
if self.is_jupyter:
return False
if not self.is_terminal or self.is_dumb_terminal:
return False
return not get_windows_console_features().vt

def _detect_color_system(self) -> Optional[ColorSystem]:
"""Detect color system from env vars."""
if self.is_jupyter:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ def test_dumb_terminal():
assert height == 25


def test_legacy_windows():
output = io.StringIO()
console = Console(file=output)
assert console.legacy_windows is False


def test_soft_wrap():
console = Console(file=io.StringIO(), width=20, soft_wrap=True)
console.print("foo " * 10)
Expand Down
Loading