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] Sometimes use console.print render table instance with keywords #987

Closed
b7wch opened this issue Feb 5, 2021 · 5 comments
Closed

Comments

@b7wch
Copy link

b7wch commented Feb 5, 2021

Describe the bug
Sometimes use console.print render table instance with keywords [, rich will crash. example under below.

ps:
if I change Console param width size bigger than a's length, this crash will disappear, at the same change the a's length little than width size, at the same as cancel justify or overflow param of add_column.

I thought this problem cause by words split when rendering table, and then use "####" to search style.

To Reproduce

from rich.console import Console
from rich.table import Table

cons = Console(width=100)
a = "[#######.................] xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx [#######.................]"
t = Table(title=f"xxx")
t.add_column("Output", justify="full", style="cyan", overflow="fold")
t.add_row(a)
cons.print(t)

Platform
full platform

Diagnose

# python -m rich.diagnose
<console width=187 ColorSystem.TRUECOLOR>
# python -m rich._windows
platform="Darwin"
WindowsConsoleFeatures(vt=False, truecolor=False)
# pip freeze | grep rich
rich==9.9.0
@nathanrpage97
Copy link
Contributor

nathanrpage97 commented Feb 5, 2021

Take a look at escaping.

What is your desired output?
Here is a working example, but will keeps the brackets instead of hiding them:

from rich.console import Console
from rich.table import Table
from rich.markup import escape


cons = Console(width=100)
a = "[#######.................] xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx [#######.................]"
t = Table(title=f"xxx")
t.add_column("Output", justify="full", style="cyan", overflow="fold")
t.add_row(escape(a))
cons.print(t)

Rich is trying to interpret the brackets as markup styling. Not sure why it stops erroring when the console width is significantly increased.

@willmcgugan
Copy link
Collaborator

Thanks @nathanrpage97

The markup is parses a style as "######.............", which doesn't exist and is omitted from the output. But when the text wraps it calls the method get_style_at_offset which doesn't provide a default style, and throws the exception.

So there is an inconsistency there. It shouldn't matter if it wraps. I think the fix would be to be a bit stricter about the parsing. If the style starts with a # and isn't a valid hex color, then it should raise a markup syntax error.

@b3wsh Nathan is correct, if your string looks like console markup, but isn't, you can escape it. Or use Text class, ie. add_row(Text(a))

@b7wch
Copy link
Author

b7wch commented Feb 5, 2021

Tanks @nathanrpage97 and @willmcgugan for your solutions. I have resolve my problem.

As @willmcgugan said, there is an inconsistency there, though Rich has provided escape method.

Do you think we need some change to make style parsing more robust?

@willmcgugan
Copy link
Collaborator

Do you think we need some change to make style parsing more robust?

There will be a fix in the next version.

@b7wch
Copy link
Author

b7wch commented Feb 7, 2021

Thank you for your excellent job, I have tried to solve this, but do not find a better way, just wanna for some good code to learn. Thanks!

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

3 participants