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

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

Closed
2 tasks done
lewis-yeung opened this issue Jul 2, 2024 · 2 comments · Fixed by #3413
Closed
2 tasks done

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

lewis-yeung opened this issue Jul 2, 2024 · 2 comments · Fixed by #3413

Comments

@lewis-yeung
Copy link
Contributor

lewis-yeung commented Jul 2, 2024

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):

╭───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface.                                                  │
│                                                                                  │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=140 ColorSystem.TRUECOLOR>                                    │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                  │
│     color_system = 'truecolor'                                                   │
│         encoding = 'utf-8'                                                       │
│             file = <_io.TextIOWrapper name='<stdout>' 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                                                           │
╰──────────────────────────────────────────────────────────────────────────────────╯
╭── <class 'rich._windows.WindowsConsoleFeatures'> ───╮
│ 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"
Copy link

github-actions bot commented Jul 2, 2024

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

Copy link

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant