Skip to content

[BUG] rich.print does not overwrite previous line with \r like built-in print #4068

@1Doomdie1

Description

@1Doomdie1

Describe the bug

I'm trying to overwrite the previous line in stdout using rich.print(), in the same way this is commonly done with the built-in print() function using \r (carriage return) and flushing the output.

Below is a minimal example that demonstrates the behavior I'm seeing.

import sys
from time import sleep
from rich import print as rprint


for i in range(10):
    rprint(f"\rNumber: {i}", end="")
    sleep(0.5)

print()

for i in range(10):
    print(f"\rNumber: {i}", end="", flush=True)
    sys.stdout.flush()
    sleep(0.5)

In the second loop (using print()), the output behaves as expected: the same line is continuously overwritten with the updated value of Number.
However, in the first loop (using rich.print()), the output does not overwrite the previous line and instead prints successive output differently.
What I find confusing is that the documentation for rich.print() states that Rich always flushes output. Based on that, I would expect the two loops to behave the same way, but they clearly don't.
I'm trying to understand:

Why \r does not produce the same overwrite behavior when using rich.print()
Whether this is expected behavior due to how Rich handles rendering and cursor control
Or whether I'm misunderstanding the documentation around flushing.

Also if there is any other way to achieve the same behavior do let me know!

Thank you!

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