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] Colour markup ignored after linebreak #1755

Closed
HappyEinara opened this issue Dec 14, 2021 · 3 comments
Closed

[BUG] Colour markup ignored after linebreak #1755

HappyEinara opened this issue Dec 14, 2021 · 3 comments

Comments

@HappyEinara
Copy link

HappyEinara commented Dec 14, 2021

Describe the bug
I'm using Textual to monitor the generation of a map for an experimental game in real time however this is an issue with Rich. I've found that Rich doesn't appear to output the requested colour after a linebreak.

Here is a minimal code sample to reproduce this:

from rich.console import Console

MAP = (
    "[on orange4]          [on green]🌳🌳[on orange4]          \n"
    "                        \n"
    "                        \n"
    "                        \n"
    "              [bright_red on black]🎯[on orange4]        \n"
    "[on green]🌳[on orange4]                  [on green]🌳[on orange4]  \n"
    " [on green]🌳[on orange4]                   [on green]🌳\n"
    "[on orange4]                        \n"
    "          [on green]🌳🌳[on orange4]          \n"
)

console = Console()
console.print(MAP)

And here is a screenshot of the output:
Screenshot from 2021-12-14 16-31-17

Most obviously you can see that the [on orange4] tag, which works elsewhere, does not set the colour at the start of the penultimate line. But you can also see that the tree at the start of the fourth line up is intended to be on a green background but is not. The common factor in many cases I've encountered is that the code immediately follows a linebreak. On the third line up you can see the difference when a space precedes the tree.

A workaround is to end each line with [default on default]\n (note the final space which starts the new line and makes following tags work). This adds a (default) left border and means resetting the actual desired colour anew on each line even where it hasn't changed from the end of the preceding line. This overhead adds a lot of bytes and complexity and makes Rich do unnecessary work processing the extra tags; on a fullscreen map refreshed every second in a live/Textual context this might become unpleasant. Update: I couldn't, in fact, make this work nicely.

Platform
I'm on Linux. This happens in both gnome-terminal and alacritty, with or without tmux running.

python -m rich.diagnose

╭───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface.                                                  │
│                                                                                  │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=192 ColorSystem.TRUECOLOR>                                    │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                  │
│     color_system = 'truecolor'                                                   │
│         encoding = 'utf-8'                                                       │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> │
│           height = 61                                                            │
│    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=192, height=61),             │
│                        legacy_windows=False,                                     │
│                        min_width=1,                                              │
│                        max_width=192,                                            │
│                        is_terminal=True,                                         │
│                        encoding='utf-8',                                         │
│                        max_height=61,                                            │
│                        justify=None,                                             │
│                        overflow=None,                                            │
│                        no_wrap=False,                                            │
│                        highlight=None,                                           │
│                        markup=None,                                              │
│                        height=None                                               │
│                    )                                                             │
│            quiet = False                                                         │
│           record = False                                                         │
│         safe_box = True                                                          │
│             size = ConsoleDimensions(width=192, height=61)                       │
│        soft_wrap = False                                                         │
│           stderr = False                                                         │
│            style = None                                                          │
│         tab_size = 8                                                             │
│            width = 192                                                           │
╰──────────────────────────────────────────────────────────────────────────────────╯
python -m rich._windows

platform="Linux"
WindowsConsoleFeatures(vt=False, truecolor=False)
pip freeze | grep rich

rich==10.16.0

(Thanks in advance and good luck with Textual; it's very exciting!)

@willmcgugan
Copy link
Collaborator

Please give version 10.16.1a1 a try:

pip install rich==10.16.1a1

Although you've definitely found a bug, your code is a little odd. Markup tags should really be closed. i.e. for every [foo] you would expect there to be a [/foo]. Your code above has lots of overlapping tags.

@HappyEinara
Copy link
Author

That works! Thanks for the speedy fix!

Re. closing the tags, I think I just had brain fog and was thinking about ANSI/fish/tmux etc. where any new colour settings just overwrite the old ones. In other words, wasn't thinking in "markup" mode. I'm really glad there was a real bug and I didn't waste your time because of that. All the best, Will!

@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

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Jan 5, 2022
10.16.2

Fixed

Fixed @ not being escaped in markup

10.16.1

Fixed

Fixed issues with overlapping tags Textualize/rich#1755

10.16.0

Fixed

Double print of progress bar in Jupyter Textualize/rich#1737

Added

Added Text.markup property Textualize/rich#1751
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

2 participants