Skip to content

[REQUEST] Add support for comparison operators for rich.text.Text to allow sorting #3758

Closed
@peytondmurray

Description

@peytondmurray

Have you checked the issues for a similar suggestions? Yes

How would you improve Rich?

I'd like to implement __lt__ and __gt__ for rich.text.Text. This would allow sorting of Text objects, in the same way that you can sort str objects. The way I'd do this is by just casting the Text to a str before making a simple string comparison - which means only the plain text content of the Text would be used for the comparison, with any markup/styling being ignored.

What problem does it solve for you?

This would allow sorting of Iterable[Text | str] types. In particular the one place this shows up is in textual.widgets.DataTable.sort. Currently, textual.widgets.DataTable supports displaying rich.text.Text in cells, but when the table is sorted, the call fails because Text doesn't support comparison.

If this would be more appropriate to address in textual, I can take the discussion there. Otherwise I'm happy to be the one to implement this change here?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions