diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c7332fee..3f909f802 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index d8985ca13..c73e360e0 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -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) diff --git a/rich/console.py b/rich/console.py index 3ec9a8aab..de0479a05 100644 --- a/rich/console.py +++ b/rich/console.py @@ -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. @@ -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: @@ -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": @@ -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: diff --git a/tests/test_console.py b/tests/test_console.py index 407a138ec..b75cd2fba 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -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)