Skip to content
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

Using rich.text.Text in __rich_repr__ #3387

Open
epistoteles opened this issue Jun 16, 2024 · 2 comments
Open

Using rich.text.Text in __rich_repr__ #3387

epistoteles opened this issue Jun 16, 2024 · 2 comments

Comments

@epistoteles
Copy link

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__?

Copy link

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
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants