-
-
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] Text objects pre-formatted with ANSI escapes miscalculate their width #1670
Comments
Rich will not process ANSI codes in strings by default, and they will confuse the length calculations. If you have strings with ANSI codes, you can use the ANSIDecoder class to convert them to Text instances. https://github.com/willmcgugan/rich/blob/master/rich/ansi.py#L108 |
Did I solve your problem? Consider sponsoring the ongoing work on Rich and Textual. Or buy me a coffee to say thanks. |
I mean, yes, this will do it, but the docs don't really mention it, and the code itself doesn't really hint in this direction, either. And I was looking. I've humbly submitted a simple little helper method for guys like me in the future to use, should we find ourselves in similar situations. |
The ANSIDecoder is experimental, which is why it isn't documented. It's not a common thing to do. Generally you wouldn't need to insert ansi codes in to tables, since Rich is more than capable of generating them anyway. Do you have use case? I will look at your PR soon. |
Our particular use-case is fraught with boring, irrelevant backstory, but I think it can be summed up as: We are getting arbitrary text blobs from a third-party library, that already have ANSI formatting applied to them. We want to essentially "import" those blobs into Rich and continue styling them from there (well, WE just want to put those results into table cells and emit them, but others might want to do that other thing). Running all of our inputs through |
When attempting to use "pre-formatted" strings from a third-party library with Rich tables, it seems to use the "whole width" of the string for header calculation rather than the "visible width"
Steps to reproduce
A simple test case, I'm using colored to illustrate:
Expected result
Actual behavior
Errata
I see class methods like
Text.from_markup
andText.styled
that would appear to be useful for creatingText
objects from "cooked" inputs, but they don't appear to be made for this particular flavor. And attempting to adjust theText._length
property implicitly truncates the string.Platform
The text was updated successfully, but these errors were encountered: