Skip to content

fancy_repr

The __fancy_repl__ protocol.

SupportsFancyRepr

Bases: Protocol

An object that supports the __fancy_repr__ dunder.

Source code in pytermgui/fancy_repr.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class SupportsFancyRepr(Protocol):  # pylint: disable=too-few-public-methods
    """An object that supports the `__fancy_repr__` dunder."""

    def __fancy_repr__(self) -> Generator[FancyYield, None, None]:
        """Yields some fancy text.

        Each value yielded can be one of two types. If a dictionary is yielded,
        it will be assumed to have `text` and `highlight` fields. `text` will be
        the string included in the repr, and `highlight` will be a boolean describing
        whether the part should be highlighted. At the moment highlighting is done by
        `highlight_python`, but this might be configurable once more highlighters are
        available.

        If a `str` is yielded, it is assumed to be a shorthand for:

            {"text": <your_text>, "highlight": True}
        """

__fancy_repr__()

Yields some fancy text.

Each value yielded can be one of two types. If a dictionary is yielded, it will be assumed to have text and highlight fields. text will be the string included in the repr, and highlight will be a boolean describing whether the part should be highlighted. At the moment highlighting is done by highlight_python, but this might be configurable once more highlighters are available.

If a str is yielded, it is assumed to be a shorthand for:

{"text": <your_text>, "highlight": True}
Source code in pytermgui/fancy_repr.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def __fancy_repr__(self) -> Generator[FancyYield, None, None]:
    """Yields some fancy text.

    Each value yielded can be one of two types. If a dictionary is yielded,
    it will be assumed to have `text` and `highlight` fields. `text` will be
    the string included in the repr, and `highlight` will be a boolean describing
    whether the part should be highlighted. At the moment highlighting is done by
    `highlight_python`, but this might be configurable once more highlighters are
    available.

    If a `str` is yielded, it is assumed to be a shorthand for:

        {"text": <your_text>, "highlight": True}
    """

build_fancy_repr(obj)

Interprets objects with the __fancy_repr__ protocol.

Source code in pytermgui/fancy_repr.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def build_fancy_repr(obj: SupportsFancyRepr) -> str:
    """Interprets objects with the `__fancy_repr__` protocol."""

    output = ""
    for item in obj.__fancy_repr__():
        if isinstance(item, str):
            output += highlight_python(item)
            continue

        text = item["text"]
        assert isinstance(text, str)

        highlight = item["highlight"]

        if highlight:
            text = highlight_python(text)

        output += text

    return output

supports_fancy_repr(obj)

Determines whether the given object supports the fancy repl protocol.

Source code in pytermgui/fancy_repr.py
37
38
39
40
def supports_fancy_repr(obj: object) -> bool:
    """Determines whether the given object supports the fancy repl protocol."""

    return hasattr(obj, "__fancy_repr__") and not isinstance(obj, type)