Textualize / rich

Rich is a Python library for rich text and beautiful formatting in the terminal.
https://rich.readthedocs.io/en/latest/
MIT License
48.79k stars 1.71k forks source link

[BUG] Console.size returns the wrong width if legacy_windows is True #3430

Open PalmtopTiger opened 1 month ago

PalmtopTiger commented 1 month ago

Describe the bug

There is no need to subtract self.legacy_windows from self._width in the size getter (link) because the self._width field already contains a value that takes legacy_windows into account (link).

For example, you can't directly get the size value from one console and assign it to another. The width of the second console will be 1 character smaller.

from rich import get_console
from rich.console import Console

console  = get_console()
econsole = Console(stderr=True)

print("Before:\nconsole.size  =", console.size, "\neconsole.size =", econsole.size)

econsole.size = console.size

print("After:\nconsole.size  =", console.size, "\neconsole.size =", econsole.size)

Output:

Before:
console.size  = ConsoleDimensions(width=182, height=26)
econsole.size = ConsoleDimensions(width=182, height=26)
After:
console.size  = ConsoleDimensions(width=182, height=26)
econsole.size = ConsoleDimensions(width=181, height=26)

Platform

Click to expand OS: Windows 10 22H2 Terminal: ConEmu ``` $ python -m rich.diagnose ┌───────────────────────── ─────────────────────────┐ │ A high level console interface. │ │ │ │ ┌──────────────────────────────────────────────────────────────────────────────┐ │ │ │ │ │ │ └──────────────────────────────────────────────────────────────────────────────┘ │ │ │ │ color_system = 'windows' │ │ encoding = 'utf-8' │ │ file = <_io.TextIOWrapper name='' mode='w' encoding='utf-8'> │ │ height = 26 │ │ is_alt_screen = False │ │ is_dumb_terminal = False │ │ is_interactive = True │ │ is_jupyter = False │ │ is_terminal = True │ │ legacy_windows = True │ │ no_color = False │ │ options = ConsoleOptions( │ │ size=ConsoleDimensions(width=182, height=26), │ │ legacy_windows=True, │ │ min_width=1, │ │ max_width=182, │ │ is_terminal=True, │ │ encoding='utf-8', │ │ max_height=26, │ │ justify=None, │ │ overflow=None, │ │ no_wrap=False, │ │ highlight=None, │ │ markup=None, │ │ height=None │ │ ) │ │ quiet = False │ │ record = False │ │ safe_box = True │ │ size = ConsoleDimensions(width=182, height=26) │ │ soft_wrap = False │ │ stderr = False │ │ style = None │ │ tab_size = 8 │ │ width = 182 │ └──────────────────────────────────────────────────────────────────────────────────┘ ┌─── ────┐ │ Windows features available. │ │ │ │ ┌───────────────────────────────────────────────────┐ │ │ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │ │ └───────────────────────────────────────────────────┘ │ │ │ │ truecolor = False │ │ vt = False │ └───────────────────────────────────────────────────────┘ ┌────── Environment Variables ───────┐ │ { │ │ 'TERM': None, │ │ 'COLORTERM': None, │ │ 'CLICOLOR': None, │ │ 'NO_COLOR': None, │ │ 'TERM_PROGRAM': None, │ │ 'COLUMNS': '184', │ │ 'LINES': '26', │ │ 'JUPYTER_COLUMNS': None, │ │ 'JUPYTER_LINES': None, │ │ 'JPY_PARENT_PID': None, │ │ 'VSCODE_VERBOSE_LOGGING': None │ │ } │ └────────────────────────────────────┘ platform="Windows" ``` ``` $ pip freeze | rg rich rich==13.7.1 ```
github-actions[bot] commented 1 month ago

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory