Skip to content

Commit 7546619

Browse files
committed
Support rich Text objects in tab completion metadta
1 parent 33e9a67 commit 7546619

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

cmd2/pt_utils.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
from prompt_toolkit.formatted_text import ANSI
1919
from prompt_toolkit.history import History
2020
from prompt_toolkit.lexers import Lexer
21+
from rich.text import Text
2122

2223
from . import (
2324
constants,
25+
rich_utils,
2426
utils,
2527
)
2628
from .argparse_custom import CompletionItem
@@ -103,9 +105,13 @@ def get_completions(self, document: Document, _complete_event: object) -> Iterab
103105

104106
for i, match in enumerate(matches):
105107
display = display_matches[i] if use_display_matches else match
106-
display_meta = None
107-
if isinstance(match, CompletionItem) and match.descriptive_data and isinstance(match.descriptive_data[0], str):
108-
display_meta = match.descriptive_data[0]
108+
display_meta: str | ANSI | None = None
109+
if isinstance(match, CompletionItem) and match.descriptive_data:
110+
if isinstance(match.descriptive_data[0], str):
111+
display_meta = match.descriptive_data[0]
112+
elif isinstance(match.descriptive_data[0], Text):
113+
# Convert rich renderable to prompt-toolkit formatted text
114+
display_meta = ANSI(rich_utils.rich_text_to_string(match.descriptive_data[0]))
109115

110116
# prompt_toolkit replaces the word before cursor by default if we use the default Completer?
111117
# No, we yield Completion(text, start_position=...).

0 commit comments

Comments
 (0)