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] Full-width content missing newline #3039

Closed
2 tasks done
daviewales opened this issue Jul 19, 2023 · 6 comments
Closed
2 tasks done

[BUG] Full-width content missing newline #3039

daviewales opened this issue Jul 19, 2023 · 6 comments

Comments

@daviewales
Copy link

Describe the bug

Full width content appears to be missing the newline character, so increasing the width of the terminal after printing scrambles the output. Observe in the screenshots below that the narrow table remains nicely formatted when the width of the terminal is increased, but the full-width table wraps as if there are no newlines at the end of the line.
Note that this occurs for any full-width content, not just tables.

from rich import print
from rich.table import Table

narrow_table = Table("number", "square", title="Narrow table")
for i in range(5):
    narrow_table.add_row(str(i), str(i**2))

print(narrow_table)

wide_table = Table("string", "longer string", title="Wide table")
string = "test"
for i in range(1, 6):
    wide_table.add_row(string * i, string * i**2)

print(wide_table)

Original output:
image

After increasing width of terminal:
image

I'm questioning myself now.
If I copy the text from the terminal and paste it below, it looks like it does contain newlines.
But if I resize the terminal first, then copy it, it looks like it doesn't contain newlines?!

Is this perhaps a Windows Terminal bug rather than a rich bug?

Copied from original terminal width:

   Narrow table
┏━━━━━━━━┳━━━━━━━━┓
┃ number ┃ square ┃
┡━━━━━━━━╇━━━━━━━━┩
│ 0      │ 0      │
│ 1      │ 1      │
│ 2      │ 4      │
│ 3      │ 9      │
│ 4      │ 16     │
└────────┴────────┘
                                 Wide table
┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ string               ┃ longer string                                      ┃
┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ test                 │ test                                               │
│ testtest             │ testtesttesttest                                   │
│ testtesttest         │ testtesttesttesttesttesttesttesttest               │
│ testtesttesttest     │ testtesttesttesttesttesttesttesttesttesttesttestt… │
│ testtesttesttesttest │ testtesttesttesttesttesttesttesttesttesttesttestt… │
└──────────────────────┴────────────────────────────────────────────────────┘

Copied after increasing terminal width:

   Narrow table
┏━━━━━━━━┳━━━━━━━━┓
┃ number ┃ square ┃
┡━━━━━━━━╇━━━━━━━━┩
│ 0      │ 0      │
│ 1      │ 1      │
│ 2      │ 4      │
│ 3      │ 9      │
│ 4      │ 16     │
└────────┴────────┘
                                 Wide table
┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓┃ string               ┃ longer string                                      ┃┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩│ test                 │ test                                               ││ testtest             │ testtesttesttest                                   ││ testtesttest         │ testtesttesttesttesttesttesttesttest               ││ testtesttesttest     │ testtesttesttesttesttesttesttesttesttesttesttestt… ││ testtesttesttesttest │ testtesttesttesttesttesttesttesttesttesttesttestt… │└──────────────────────┴────────────────────────────────────────

Platform

Click to expand

What platform (Win/Linux/Mac) are you running on? What terminal software are you using?

  • Windows
  • Windows Terminal
  • PowerShell and WSL
$ python -m rich.diagnose
╭───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface.                                                  │
│                                                                                  │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=85 ColorSystem.EIGHT_BIT>                                     │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                  │
│     color_system = '256'                                                         │
│         encoding = 'utf-8'                                                       │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> │
│           height = 38                                                            │
│    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=85, height=38),              │
│                        legacy_windows=False,                                     │
│                        min_width=1,                                              │
│                        max_width=85,                                             │
│                        is_terminal=True,                                         │
│                        encoding='utf-8',                                         │
│                        max_height=38,                                            │
│                        justify=None,                                             │
│                        overflow=None,                                            │
│                        no_wrap=False,                                            │
│                        highlight=None,                                           │
│                        markup=None,                                              │
│                        height=None                                               │
│                    )                                                             │
│            quiet = False                                                         │
│           record = False                                                         │
│         safe_box = True                                                          │
│             size = ConsoleDimensions(width=85, height=38)                        │
│        soft_wrap = False                                                         │
│           stderr = False                                                         │
│            style = None                                                          │
│         tab_size = 8                                                             │
│            width = 85                                                            │
╰──────────────────────────────────────────────────────────────────────────────────╯
╭─── <class 'rich._windows.WindowsConsoleFeatures'> ────╮
│ Windows features available.                           │
│                                                       │
│ ╭───────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ ╰───────────────────────────────────────────────────╯ │
│                                                       │
│ truecolor = False                                     │
│        vt = False                                     │
╰───────────────────────────────────────────────────────╯
╭────── Environment Variables ───────╮
│ {                                  │
│     'TERM': 'xterm-256color',      │
│     'COLORTERM': None,             │
│     'CLICOLOR': None,              │
│     'NO_COLOR': None,              │
│     'TERM_PROGRAM': None,          │
│     'COLUMNS': None,               │
│     'LINES': None,                 │
│     'JUPYTER_COLUMNS': None,       │
│     'JUPYTER_LINES': None,         │
│     'JPY_PARENT_PID': None,        │
│     'VSCODE_VERBOSE_LOGGING': None │
│ }                                  │
╰────────────────────────────────────╯
platform="Linux"

$ pip freeze | grep rich
rich==13.4.2
@github-actions
Copy link

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

@daviewales
Copy link
Author

daviewales commented Jul 19, 2023

Update: I just tested this in xfce4-terminal on Alpine Linux in a VM, and everything works as expected.
The bug appears to only affect Windows Terminal.
Is rich doing something different in Windows Terminal, or is it a Windows Terminal bug?

Working as expected in xfce4-terminal

Run in narrow xfce4-terminal:
image

Increase width of terminal:
image

Including platform details from Alpine

Click to expand
$ python -m rich.diagnose
╭─────────────────────── <class 'rich.console.Console'> ───────────────────────╮
│ A high level console interface.                                              │
│                                                                              │
│ ╭──────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=80 ColorSystem.TRUECOLOR>                                 │ │
│ ╰──────────────────────────────────────────────────────────────────────────╯ │
│                                                                              │
│     color_system = 'truecolor'                                               │
│         encoding = 'utf-8'                                                   │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w'               │
│                    encoding='utf-8'>                                         │
│           height = 24                                                        │
│    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=80, height=24),          │
│                        legacy_windows=False,                                 │
│                        min_width=1,                                          │
│                        max_width=80,                                         │
│                        is_terminal=True,                                     │
│                        encoding='utf-8',                                     │
│                        max_height=24,                                        │
│                        justify=None,                                         │
│                        overflow=None,                                        │
│                        no_wrap=False,                                        │
│                        highlight=None,                                       │
│                        markup=None,                                          │
│                        height=None                                           │
│                    )                                                         │
│            quiet = False                                                     │
│           record = False                                                     │
│         safe_box = True                                                      │
│             size = ConsoleDimensions(width=80, height=24)                    │
│        soft_wrap = False                                                     │
│           stderr = False                                                     │
│            style = None                                                      │
│         tab_size = 8                                                         │
│            width = 80                                                        │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─── <class 'rich._windows.WindowsConsoleFeatures'> ────╮
│ Windows features available.                           │
│                                                       │
│ ╭───────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ ╰───────────────────────────────────────────────────╯ │
│                                                       │
│ truecolor = False                                     │
│        vt = False                                     │
╰───────────────────────────────────────────────────────╯
╭────── Environment Variables ───────╮
│ {                                  │
│     'TERM': 'xterm-256color',      │
│     'COLORTERM': 'truecolor',      │
│     'CLICOLOR': None,              │
│     'NO_COLOR': None,              │
│     'TERM_PROGRAM': None,          │
│     'COLUMNS': None,               │
│     'LINES': None,                 │
│     'JUPYTER_COLUMNS': None,       │
│     'JUPYTER_LINES': None,         │
│     'JPY_PARENT_PID': None,        │
│     'VSCODE_VERBOSE_LOGGING': None │
│ }                                  │
╰────────────────────────────────────╯
platform="Linux"
$ pip freeze | grep rich
rich==13.3.5

@willmcgugan
Copy link
Collaborator

This is an idiosyncrasy of Windows. You can reproduce it without Rich:

>>> import os
>>> width, _  = os.get_terminal_size()
>>> print("X" * width + "\nYYYYYYY")

I don't think Windows stores the new line characters at all. When you copy text it treats a run of spaces on the right as a new line, but if text goes all the way to the end it is assumed to be part of a single line.

@daviewales
Copy link
Author

Oh that's truly awful! Thank you. I'll open a bug with Windows Terminal.
As a workaround, would rich consider reducing the width by one and adding a space on Windows to work around this issue?

e.g.

import os
width, _ = os.get_terminal_size()
print("X" * (width-1) + " \nYYYYYYYY")

Before resize:
image

After resize:
image

@daviewales
Copy link
Author

Tracked down the existing Windows Terminal bug:
microsoft/terminal#3088

Closing this issue as not a rich bug.

@github-actions
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

No branches or pull requests

2 participants