Textualize / rich

Rich is a Python library for rich text and beautiful formatting in the terminal.
https://rich.readthedocs.io/en/latest/
MIT License
49.56k stars 1.73k forks source link

[REQUEST] Parse escape codes in pretty printer #2287

Closed dlukes closed 2 years ago

dlukes commented 2 years ago

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?

willmcgugan commented 2 years ago

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.

dlukes commented 2 years ago

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.

willmcgugan commented 2 years ago

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 commented 2 years ago

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.

darrenburns commented 2 years ago

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

github-actions[bot] commented 2 years ago

Did I solve your problem?

Why not buy the devs a coffee to say thanks?