Replies: 1 comment 8 replies
-
Beta Was this translation helpful? Give feedback.
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
It seems that rich is heavily geared towards just outputting something directly at the place where we want to create it.
I have several projects that use coloured output (using ansi color sequences) and thinking about enhancing it and using a library like rich.
The code works mostly by assembling strings and colour information from various sources from multiple sources into one. Often
__str__
is used for that too. The code could look for a simple case of (configurable) colouor output of an object like:A lot of these strings will be cached for performance reasons.
Now with rich, this is not as simple anymore. As per https://rich.readthedocs.io/en/stable/console.html#capturing-output one could capture the output that is printed.
One could possibly write a function like
But that seems to be overly complicated and slow. It feels like in the end rich itself will anyways generate a string ( or multiple ones ) and pass that to whatever is necessary for output.
An alternative way that has been proposed to me was that one could just pass around all the formatting (bbcode like) strings instead of
print( s, style=xxx )
string results, but in that case at the point where I want to get the string I have to do the above anyways, plus it has to parse the string which costs more than just passing around strings containing the ansi codes already.The speed is already noticeably impacted for cases where there are tens of thousands of tiny strings in the current approach and I really don't want to slow it down even more.
TL;DR
So is there an easier and possibly more performant way to get just the string than to go the roundabout way to capture the output?
Beta Was this translation helpful? Give feedback.
All reactions