Textualize / rich

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

[BUG] Incorrect auto detection of terminal size on Windows #3412

Closed lewis-yeung closed 3 days ago

lewis-yeung commented 3 months ago

Describe the bug

I don't know why my PR #2916 didn't get any response and was finally closed. Anyway, I'm opening this issue to restate the bug.

Currently, the automatic detection of terminal size on Windows is only performed for _STDOUT_FILENO which is the default value passed to os.get_terminal_size. So if we redirect the standard output (stdout) to a file and create a Console on the standard error (stderr) with width and height left None, the auto-detected terminal size will always be a fallback value (80, 25) (supposing that legacy_windows is False).

A simple example to illustrate this problem: on Windows, run the following test script with stdout redirected to a file, while stderr was still connected to a terminal, e.g., with width = 140 and height = 21:

import sys
from rich.console import Console

console = Console(stderr=True, legacy_windows=False)
print(console.size, file=sys.stderr)

The output is

ConsoleDimensions(width=80, height=25)

which is not the expected:

ConsoleDimensions(width=140, height=21)

However, this works as expected On Linux.

Platform

Click to expand The output of `python -m rich.diagnose` (tested in Rich v13.7.1): ``` ╭───────────────────────── ─────────────────────────╮ │ A high level console interface. │ │ │ │ ╭──────────────────────────────────────────────────────────────────────────────╮ │ │ │ │ │ │ ╰──────────────────────────────────────────────────────────────────────────────╯ │ │ │ │ color_system = 'truecolor' │ │ encoding = 'utf-8' │ │ file = <_io.TextIOWrapper name='' mode='w' encoding='utf-8'> │ │ height = 21 │ │ is_alt_screen = False │ │ is_dumb_terminal = False │ │ is_interactive = True │ │ is_jupyter = False │ │ is_terminal = True │ │ legacy_windows = False │ │ no_color = False │ │ options = ConsoleOptions( │ │ size=ConsoleDimensions(width=140, height=21), │ │ legacy_windows=False, │ │ min_width=1, │ │ max_width=140, │ │ is_terminal=True, │ │ encoding='utf-8', │ │ max_height=21, │ │ justify=None, │ │ overflow=None, │ │ no_wrap=False, │ │ highlight=None, │ │ markup=None, │ │ height=None │ │ ) │ │ quiet = False │ │ record = False │ │ safe_box = True │ │ size = ConsoleDimensions(width=140, height=21) │ │ soft_wrap = False │ │ stderr = False │ │ style = None │ │ tab_size = 8 │ │ width = 140 │ ╰──────────────────────────────────────────────────────────────────────────────────╯ ╭── ───╮ │ Windows features available. │ │ │ │ ╭─────────────────────────────────────────────────╮ │ │ │ WindowsConsoleFeatures(vt=True, truecolor=True) │ │ │ ╰─────────────────────────────────────────────────╯ │ │ │ │ truecolor = True │ │ vt = True │ ╰─────────────────────────────────────────────────────╯ ╭────── Environment Variables ───────╮ │ { │ │ 'TERM': None, │ │ 'COLORTERM': 'truecolor', │ │ 'CLICOLOR': None, │ │ 'NO_COLOR': None, │ │ 'TERM_PROGRAM': 'vscode', │ │ 'COLUMNS': None, │ │ 'LINES': None, │ │ 'JUPYTER_COLUMNS': None, │ │ 'JUPYTER_LINES': None, │ │ 'JPY_PARENT_PID': None, │ │ 'VSCODE_VERBOSE_LOGGING': None │ │ } │ ╰────────────────────────────────────╯ platform="Windows" ```
github-actions[bot] commented 3 months 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

github-actions[bot] commented 3 days ago

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual