Skip to content

[BUG] Using progress + console.log leads to squished output when printing to non-terminal  #1606

@ichard26

Description

@ichard26

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions