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] Using progress + console.log leads to squished output when printing to non-terminal #1606

Closed
ichard26 opened this issue Oct 17, 2021 · 5 comments

Comments

@ichard26
Copy link

ichard26 commented Oct 17, 2021

Describe the bug

When printing to something that isn't detected as a terminal, say GitHub Actions or a file, the completed progress bar doesn't have a terminating newline resulting in the next console.log's output being joined to the progress bar.

To Reproduce

import time

import rich
import rich.progress

console = rich.get_console()

with rich.progress.Progress(console=console) as progress:
    setup_task = progress.add_task("[bold yellow]Setting up projects", total=50)
    for i in range(50):
        time.sleep(0.01)
        progress.advance(setup_task)

console.log("[bold orange3]hello!")

Then running the script redirecting stdout to a file breaks the output sadly:

(venv) ichard26@acer-ubuntu:~/programming/tools/diff-shades$ python test.py > log.txt
(venv) ichard26@acer-ubuntu:~/programming/tools/diff-shades$ cat log.txt
Setting up projects ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00[13:26:22] hello!                                                                                                                           test.py:14
(venv) ichard26@acer-ubuntu:~/programming/tools/diff-shades$ python test.py
Setting up projects ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00
[13:27:14] hello!                                                                                                                           test.py:14

Although as shown if rich is printing to a terminal it works just fine.

Platform

  • OS: Ubuntu 20.04.03 LTS
  • Python: CPython 3.8.5
  • Terminal: GNOME Terminal 3.36.2 using VTE 0.60.3 +BIDI +GNUTLS +ICU +SYSTEMD
  • Shell: bash, version 5.0.17(1)-release & fish, version 3.1.0
  • Rich: 10.12.0 (although the bug still exists on master as I write with commit eb673d1)

Diagnose

❯ python -m rich.diagnose
╭───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface.                                                  │
│                                                                                  │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=150 ColorSystem.TRUECOLOR>                                    │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                  │
│     color_system = 'truecolor'                                                   │
│         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=150, height=38),             │
│                        legacy_windows=False,                                     │
│                        min_width=1,                                              │
│                        max_width=150,                                            │
│                        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=150, height=38)                       │
│        soft_wrap = False                                                         │
│           stderr = False                                                         │
│            style = None                                                          │
│         tab_size = 8                                                             │
│            width = 150                                                           │
╰──────────────────────────────────────────────────────────────────────────────────╯

❯ python -m rich._windows
platform="Linux"
WindowsConsoleFeatures(vt=False, truecolor=False)pip freeze | grep rich

Other context

For the time being I'm using this workaround:

# `console` here is from rich.get_console()
if not console.is_terminal:
    # Curiously this is needed when redirecting to a file so the next emitted
    # line isn't attached to the (completed) progress bar.
    console.line()

And finally, thank you for the fantastic work on rich! It's a wonderful library and has made my output so much nicer :)

@lainisourgod
Copy link

Also seeing this

@joegester
Copy link

Same here

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Nov 14, 2021
10.13.0

Added

Added json.dumps parameters to print_json Textualize/rich#1638

Fixed

Fixed an edge case bug when console module try to detect if they are in a tty at the end of a pytest run
Fixed a bug where logging handler raises an exception when running with pythonw (related to https://bugs.python.org/issue13807)
Fixed issue with TERM env vars that have more than one hyphen Textualize/rich#1640
Fixed missing new line after progress bar when terminal is not interactive Textualize/rich#1606
Fixed exception in IPython when disabling pprint with %pprint Textualize/rich#1646
Fixed issue where values longer than the console width produced invalid JSON Textualize/rich#1653
Fixes trailing comma when pretty printing dataclass with last field repr=False Textualize/rich#1599

Changed

Markdown codeblocks now word-wrap
@willmcgugan
Copy link
Collaborator

Please try 10.14.0

@github-actions
Copy link

Did I solve your problem?

Consider sponsoring the ongoing work on Rich and Textual.

Or buy me a coffee to say thanks.

Will McGugan

@ichard26
Copy link
Author

Can confirm 10.14.0 fixes the issue, thank you!

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

4 participants