question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] One trailing newline ignored by rich.print in some cases

See original GitHub issue

Hi, thanks for this great library.

Not sure if this is expected behavior or a bug. In certain cases, rich.print handles newlines in a slightly different manner than the print built-in.

Example:

>>> for i in range(3): print('Hey' + '\n' * i)
... 
Hey
Hey

Hey


>>> from rich import print
>>> for i in range(3): print('Hey' + '\n' * i)
... 
Hey
Hey
Hey

>>> 

Apparently, when the printed string contains at least one trailing newline (i.e. when i == 1 or i == 2), one newline is ignored by rich.print.

A screenshot of a similar example: rich

(rich 3.3.2, Python 3.8.0, GNOME Terminal 3.18.3 on Linux Mint 18.2 64-bit)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
willmcgugancommented, Jul 28, 2020

Might be useful occasionally, but it would be difficult to test control codes and differences across platforms with doctests.

There are tests now for this case btw.

1reaction
mportesdevcommented, Jul 28, 2020

Btw I don’t know if you use doctest in your project, but this kind of output in the interactive interpreter can be easily tested with it, e.g.:

r"""
Test examples for `rich.print`:

    >>> from rich import print
    >>> for i in range(3): print('Hey' + '\n' * i)
    ...
    Hey
    Hey
    <BLANKLINE>
    Hey
    <BLANKLINE>
    <BLANKLINE>
    >>> print('[red]Hey[/]')
    Hey
    >>> print('[red]Hey[/]\n')
    Hey
    <BLANKLINE>
    >>> print('[red]Hey[/]\n\n')
    Hey
    <BLANKLINE>
    <BLANKLINE>

"""

import doctest

if __name__ == '__main__':
    doctest.testmod()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Trailing newline ignored · Issue #70 · tidyverse/glue - GitHub
I'm using glue for some codegen and I noticed it seems to trim a trailing newline. str(glue("\n")) ## empty string str(glue("\n\n")) ## one...
Read more >
Full Text Bug Listing - Red Hat Bugzilla
I've obfuscated the passwords, but dbpwd is 29 characters, and the last character is a newline, which is strange, but perhaps valid in...
Read more >
jinja2 how to remove trailing newline - python - Stack Overflow
You can avoid the trailing newline by first removing all newlines (using the minus signs at -%} and {%- in the for loop),...
Read more >
Line breaks are removed in posts made in plain text format
Describes the behavior when line breaks are removed in a plain text format post without any indication in Outlook.
Read more >
Your Guide to the Python print() Function
Sometimes you don't want to end your message with a trailing newline so that subsequent calls to print() will continue on the same...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found