Textualize / rich

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

Using rich.text.Text in __rich_repr__ #3387

Open epistoteles opened 2 weeks ago

epistoteles commented 2 weeks ago

When I print a rich ColorTriplet, I don't just want to see the values for red, green, and blue in my console. I also want to see the color.

I tried giving the ColorTriplet a new __rich_repr__ on the fly which also includes a small color swatch in its color.

ColorTriplet.__rich_repr__ = lambda self: [("red", self.red), 
                                           ("green", self.green),
                                           ("blue", self.blue),
                                           ("color", Text("██", style=f"rgb({self.red}, {self.green}, {self.blue})"))]

However, the resulting console output looks like this:

>>> ColorTriplet(12,24,25)
ColorTriplet(red=12, green=24, blue=25, color=<text '██' []>)

where '██' just gets displayed in the default green string color.

Can rich renderables not be used inside a __rich_repr__?

github-actions[bot] commented 2 weeks ago

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

willmcgugan commented 2 weeks ago

That methods creates reprs, which are intended to be formatted like a Python expression. It won't produce any escape sequences.

Why do you want to print a ColorTriplet? It was only intended for the Color class, which already generates the actual color if you print it.