-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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] cell_len inaccurate for OSC8 links, occasionally breaking text wrapping #3561
Comments
I don't think If you convert your links into Rich from rich.text import Text
from rich import print
def make_osc_link(url: str, text: str) -> str:
return f"\x1b]8;;{url}\x1b\\{text}\x1b]8;;\x1b\\"
plain_text = "ansi"
short_link_ansi = make_osc_link("http://example.com/", plain_text)
long_link_ansi = make_osc_link("http://example.com/" + "x" * 100, plain_text)
short_link_text = Text.from_ansi(short_link_ansi)
long_link_text = Text.from_ansi(long_link_ansi)
print(short_link_text, short_link_text.cell_len)
print(long_link_text, long_link_text.cell_len) |
Tom is correct -- as usual!
It is better to let Rich generate all the escape sequences, or if you can't avoid escape sequences in your output, use |
Ah so Rich is intended to work with preexisting ANSI codes only via Text.from_ansi(), including all OSC codes? That makes sense! I'll give it a try and see if that fixes everything. I had text created with Text() and as you can imagine that led to subtle/latent wrapping bugs. Perhaps a warning in the docs and/or a method for creating OSC links in Rich itself (or even an exception for misuse like this?) would encourage correct usage? Thanks again! |
|
Ah thanks, yes though in my use case it makes more sense to generate links programmatically. Closing this since it's clear Text.from_ansi() is the best answer. Also just want to say, looking more closely the codebase have noticed how unusually clean and refreshingly well designed it is! Nice work @willmcgugan and team. |
I hope we solved your problem. If you like using Rich, you might also enjoy Textual |
Describe the bug
It seems that cell_len does not return the visible cell length for OSC8 links
I've reproduced and actually just had to patch this for my own project. Attaching a repro and the fix below.
Can PR too but thought I'd confirm this should be fixed, as probably my fix affects performance or
interacts with other things I've not thought of?
It may not be something everyone notices, but if you do use OSC8 links, it can make line breaking not work correctly. Among other things, rich can break a link in the middle (making a fragment of the URL visible).
Thanks so much!
Platform
Click to expand
What platform (Win/Linux/Mac) are you running on? What terminal software are you using?
v13.9.4 on macos, but as noted above you can see the issue on main branch.
I may ask you to copy and paste the output of the following commands. It may save some time if you do it now.
If you're using Rich in a terminal:
If you're using Rich in a Jupyter Notebook, run the following snippet in a cell
and paste the output in your bug report.
The text was updated successfully, but these errors were encountered: