rich icon indicating copy to clipboard operation
rich copied to clipboard

[REQUEST] Parse escape codes in pretty printer

Open dlukes opened this issue 3 years ago • 4 comments

I ran into this while using https://github.com/InvestmentSystems/static-frame:

image

StaticFrame does its own coloring, seen in Out[3]. But when I enable rich's REPL support (which I typically do by default), rich attempts to color the ANSI escape codes that are already there and corrupts them, resulting in the garbled output seen in Out[5].

Would it be possible to detect whether the output already contains ANSI escape codes, and if so, either strip them before applying rich's own coloring, or skip it entirely? Ideally, the behavior could be configured via rich.pretty.install or some similar mechanism.

Alternatively, maybe there is an existing way to deal with this when using rich that I'm not aware of?

dlukes avatar May 20 '22 11:05 dlukes

I'm guess this library is writing escape codes with it's repr, which is frankly a bad idea.

Yes, it would be possible to workaround. The simplest solution would be to not do any further processing if there is an escape code in the output.

willmcgugan avatar May 20 '22 13:05 willmcgugan

I'm guess this library is writing escape codes with it's repr, which is frankly a bad idea.

Ah, that's a good point. Yes, that's what it's doing, based on the cursory look I had before submitting this issue.

So maybe just leave such reprs as is, not to make matters worse, even though rich's expectation that there be no escape codes in the repr is reasonable? Bandwidth allowing of course.

dlukes avatar May 20 '22 13:05 dlukes

So maybe just leave such reprs as is, not to make matters worse, even though rich's expectation that there be no escape codes in the repr is reasonable? Bandwidth allowing of course.

It is reasonable. repr strings are used in many contexts where escape codes won't work. However, we've worked around unreasonable things that developers do before!

willmcgugan avatar Jun 10 '22 10:06 willmcgugan

I think what we'll do is in pretty.py, if a repr contains an escape we will attempt to parse that as ANSI escape codes.

willmcgugan avatar Jun 10 '22 10:06 willmcgugan

Added in https://github.com/Textualize/rich/pull/2470, so should be fixed in the next release. Let us know if not.

darrenburns avatar Aug 19 '22 14:08 darrenburns

Did I solve your problem?

Why not buy the devs a coffee to say thanks?

github-actions[bot] avatar Aug 19 '22 14:08 github-actions[bot]