Skip to content

[BUG] Live outputs different amounts of newlines depending on if console is a terminal #4192

Description

@qha

Describe the bug

The Live class of Rich emits a newline on stopping but only if its console is connected to a terminal.

I use greenbone-feed-sync that is built with Rich and it makes the output captured by journald (when run as a service) hard to read.

I prepared a branch that fixes this: https://github.com/qha/rich/tree/fix-live-termination

Provide a minimal code example that demonstrates the issue if you can.

from time import sleep

from rich.console import Console
from rich.live import Live
from rich.spinner import Spinner as RichSpinner


class Spinner:
    def __init__(self, console: Console, status: str) -> None:
        self.console = console
        self._spinner = RichSpinner(
            "dots", text=status, style="status.spinner", speed=1.0
        )
        self._live = Live(
            self._spinner,
            console=console,
            refresh_per_second=12.5,
            transient=False,
        )

    def __enter__(self) -> "Spinner":
        self._live.start()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self._live.stop()


def showprogress(console):
    with Spinner(console,
                 "Downloading content from url"
                 " to destination"):
        sleep(1)
    console.print("Moving on to other work")


def test(force_terminal):
    console = Console(force_terminal=force_terminal)
    showprogress(console)


if __name__ == "__main__":
    test(None)
    test(False)
    test(True)

Please ensure that your example runs without modification (don't forget imports).

If the issue is visual in nature, consider posting a screenshot.

% python3 demo.py
⠙ Downloading content from url to destination
Moving on to other work
⠋ Downloading content from url to destinationMoving on to other work
⠙ Downloading content from url to destination
Moving on to other work

Note the lack of a newline between destination and Moving in the middle.

Platform

Click to expand

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

Linux, Xterm

% python -m rich.diagnose                      
╭─────────────────────── <class 'rich.console.Console'> ───────────────────────╮
│ A high level console interface.                                              │
│                                                                              │
│ ╭──────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=80 ColorSystem.STANDARD>                                  │ │
│ ╰──────────────────────────────────────────────────────────────────────────╯ │
│                                                                              │
│     color_system = 'standard'                                                │
│         encoding = 'utf-8'                                                   │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w'               │
│                    encoding='utf-8'>                                         │
│           height = 78                                                        │
│    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=78),          │
│                        legacy_windows=False,                                 │
│                        min_width=1,                                          │
│                        max_width=80,                                         │
│                        is_terminal=True,                                     │
│                        encoding='utf-8',                                     │
│                        max_height=78,                                        │
│                        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=78)                    │
│        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 ───────╮
│ {                                  │
│     'CLICOLOR': None,              │
│     'COLORTERM': None,             │
│     'COLUMNS': None,               │
│     'JPY_PARENT_PID': None,        │
│     'JUPYTER_COLUMNS': None,       │
│     'JUPYTER_LINES': None,         │
│     'LINES': None,                 │
│     'NO_COLOR': None,              │
│     'TERM_PROGRAM': None,          │
│     'TERM': 'xterm',               │
│     'TTY_COMPATIBLE': None,        │
│     'TTY_INTERACTIVE': None,       │
│     'VSCODE_VERBOSE_LOGGING': None │
│ }                                  │
╰────────────────────────────────────╯
platform="Linux"
% pip freeze | grep rich 
rich @ file:///home/qha/github/rich
% pwd
/home/qha/github/rich
% git show --oneline
9d8f9a37 (HEAD -> main, origin/main, origin/HEAD, fork/main) Merge pull request #4175 from Textualize/fix-readme-master-to-main

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions