Skip to content

Commit 323e5e9

Browse files
committed
Enabled styled text for completion display and display_meta values.
1 parent 054f172 commit 323e5e9

File tree

4 files changed

+49
-19
lines changed

4 files changed

+49
-19
lines changed

cmd2/cmd2.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,12 +1189,20 @@ def allow_style_type(value: str) -> ru.AllowStyle:
11891189
f"must be {ru.AllowStyle.ALWAYS}, {ru.AllowStyle.NEVER}, or {ru.AllowStyle.TERMINAL} (case-insensitive)"
11901190
) from ex
11911191

1192+
settable_description = Text.assemble(
1193+
'Allow styled text in output (Options: ',
1194+
(str(ru.AllowStyle.ALWAYS), Style(bold=True)),
1195+
", ",
1196+
(str(ru.AllowStyle.NEVER), Style(bold=True)),
1197+
", ",
1198+
(str(ru.AllowStyle.TERMINAL), Style(bold=True)),
1199+
")",
1200+
)
11921201
self.add_settable(
11931202
Settable(
11941203
'allow_style',
11951204
allow_style_type,
1196-
'Allow ANSI text style sequences in output (valid values: '
1197-
f'{ru.AllowStyle.ALWAYS}, {ru.AllowStyle.NEVER}, {ru.AllowStyle.TERMINAL})',
1205+
ru.rich_text_to_string(settable_description),
11981206
self,
11991207
choices_provider=get_allow_style_choices,
12001208
)
@@ -1211,15 +1219,15 @@ def allow_style_type(value: str) -> ru.AllowStyle:
12111219
Settable(
12121220
'max_completion_table_items',
12131221
int,
1214-
"Maximum number of completion results allowed for a completion table to appear",
1222+
"Max results allowed to display a table",
12151223
self,
12161224
)
12171225
)
12181226
self.add_settable(
12191227
Settable(
12201228
'max_column_completion_results',
12211229
int,
1222-
"Maximum number of completion results to display in a single column",
1230+
"Max results to display in a single column",
12231231
self,
12241232
)
12251233
)
@@ -2496,11 +2504,13 @@ def _get_settable_choices(self) -> Choices:
24962504
items: list[CompletionItem] = []
24972505

24982506
for name, settable in self.settables.items():
2507+
value_str = str(settable.value)
24992508
table_row = [
2500-
str(settable.value),
2509+
value_str,
25012510
settable.description,
25022511
]
2503-
items.append(CompletionItem(name, display_meta=str(settable.value), table_row=table_row))
2512+
display_meta = f"[Current: {su.stylize(value_str, Style(bold=True))}] {settable.description}"
2513+
items.append(CompletionItem(name, display_meta=display_meta, table_row=table_row))
25042514

25052515
return Choices(items=items)
25062516

cmd2/pt_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ def get_completions(self, document: Document, _complete_event: object) -> Iterab
129129
yield Completion(
130130
match_text,
131131
start_position=start_position,
132-
display=item.display,
133-
display_meta=item.display_meta,
132+
display=ANSI(item.display),
133+
display_meta=ANSI(item.display_meta),
134134
)
135135

136136

tests/test_cmd2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,9 +2349,9 @@ def test_get_settable_choices(base_app: cmd2.Cmd) -> None:
23492349
assert cur_settable is not None
23502350

23512351
str_value = str(cur_settable.value)
2352-
assert cur_choice.display_meta == str_value
2353-
assert cur_choice.table_row[0] == str_value
2354-
assert cur_choice.table_row[1] == cur_settable.description
2352+
assert str_value in cur_choice.display_meta
2353+
assert ru.rich_text_to_string(cur_choice.table_row[0]) == str_value
2354+
assert ru.rich_text_to_string(cur_choice.table_row[1]) == cur_settable.description
23552355

23562356

23572357
def test_completion_supported(base_app) -> None:

tests/test_pt_utils.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@
77
import pytest
88
from prompt_toolkit.buffer import Buffer
99
from prompt_toolkit.document import Document
10+
from prompt_toolkit.formatted_text import (
11+
ANSI,
12+
to_formatted_text,
13+
)
1014

1115
import cmd2
12-
from cmd2 import pt_utils, utils
16+
from cmd2 import (
17+
Cmd2Style,
18+
pt_utils,
19+
stylize,
20+
utils,
21+
)
1322
from cmd2.history import HistoryItem
1423
from cmd2.parsing import Statement
1524

@@ -191,24 +200,35 @@ def test_get_completions(self, mock_cmd_app: MockCmd, monkeypatch) -> None:
191200
line = ""
192201
document = Document(line, cursor_position=0)
193202

203+
# Test plain and styled values for display and display_meta
204+
foo_text = "foo"
205+
foo_display = "Foo Display"
206+
foo_meta = "Foo Meta"
207+
208+
bar_text = "bar"
209+
bar_display = stylize("Bar Display", Cmd2Style.SUCCESS)
210+
bar_meta = stylize("Bar Meta", Cmd2Style.WARNING)
211+
194212
# Set up matches
195213
completion_items = [
196-
cmd2.CompletionItem("foo", display="Foo Display"),
197-
cmd2.CompletionItem("bar", display="Bar Display"),
214+
cmd2.CompletionItem(foo_text, display=foo_display, display_meta=foo_meta),
215+
cmd2.CompletionItem(bar_text, display=bar_display, display_meta=bar_meta),
198216
]
199217
cmd2_completions = cmd2.Completions(completion_items, completion_table="Table Data")
200218
mock_cmd_app.complete.return_value = cmd2_completions
201219

202220
# Call get_completions
203221
completions = list(completer.get_completions(document, None))
204222

205-
# Verify completions which are sorted by display field.
206223
assert len(completions) == len(cmd2_completions)
207-
assert completions[0].text == "bar"
208-
assert completions[0].display == [('', 'Bar Display')]
209224

210-
assert completions[1].text == "foo"
211-
assert completions[1].display == [('', 'Foo Display')]
225+
assert completions[0].text == bar_text
226+
assert to_formatted_text(completions[0].display) == to_formatted_text(ANSI(bar_display))
227+
assert to_formatted_text(completions[0].display_meta) == to_formatted_text(ANSI(bar_meta))
228+
229+
assert completions[1].text == foo_text
230+
assert to_formatted_text(completions[1].display) == to_formatted_text(ANSI(foo_display))
231+
assert to_formatted_text(completions[1].display_meta) == to_formatted_text(ANSI(foo_meta))
212232

213233
# Verify that only the completion table printed
214234
assert mock_print.call_count == 1

0 commit comments

Comments
 (0)