|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from typing import TYPE_CHECKING, Callable, Iterable, List, Tuple, Union, cast |
| 3 | +from typing import TYPE_CHECKING, Any, Callable, Iterable, List, Tuple, Union, cast |
4 | 4 |
|
5 | 5 | from prompt_toolkit.mouse_events import MouseEvent |
6 | 6 |
|
@@ -40,10 +40,19 @@ class MagicFormattedText(Protocol): |
40 | 40 |
|
41 | 41 | def __pt_formatted_text__(self) -> StyleAndTextTuples: ... |
42 | 42 |
|
| 43 | + class RichFormattedText(Protocol): |
| 44 | + """ |
| 45 | + Any rich text object from the rich library that implements |
| 46 | + ``__rich_console__``. |
| 47 | + """ |
| 48 | + |
| 49 | + def __rich_console__(self, console: Any = ..., options: Any = ...) -> Any: ... |
| 50 | + |
43 | 51 |
|
44 | 52 | AnyFormattedText = Union[ |
45 | 53 | str, |
46 | 54 | "MagicFormattedText", |
| 55 | + "RichFormattedText", |
47 | 56 | StyleAndTextTuples, |
48 | 57 | Callable[[], "AnyFormattedText"], |
49 | 58 | None, |
@@ -77,6 +86,10 @@ def to_formatted_text( |
77 | 86 | result = value # StyleAndTextTuples |
78 | 87 | elif hasattr(value, "__pt_formatted_text__"): |
79 | 88 | result = cast("MagicFormattedText", value).__pt_formatted_text__() |
| 89 | + elif hasattr(value, "__rich_console__"): |
| 90 | + from .rich import Rich |
| 91 | + |
| 92 | + result = Rich(value).__pt_formatted_text__() |
80 | 93 | elif callable(value): |
81 | 94 | return to_formatted_text(value(), style=style) |
82 | 95 | elif auto_convert: |
|
0 commit comments