Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,12 +1189,20 @@ def allow_style_type(value: str) -> ru.AllowStyle:
f"must be {ru.AllowStyle.ALWAYS}, {ru.AllowStyle.NEVER}, or {ru.AllowStyle.TERMINAL} (case-insensitive)"
) from ex

settable_description = Text.assemble(
'Allow styled text in output (Options: ',
(str(ru.AllowStyle.ALWAYS), Style(bold=True)),
", ",
(str(ru.AllowStyle.NEVER), Style(bold=True)),
", ",
(str(ru.AllowStyle.TERMINAL), Style(bold=True)),
")",
)
self.add_settable(
Settable(
'allow_style',
allow_style_type,
'Allow ANSI text style sequences in output (valid values: '
f'{ru.AllowStyle.ALWAYS}, {ru.AllowStyle.NEVER}, {ru.AllowStyle.TERMINAL})',
ru.rich_text_to_string(settable_description),
self,
choices_provider=get_allow_style_choices,
)
Expand All @@ -1211,15 +1219,15 @@ def allow_style_type(value: str) -> ru.AllowStyle:
Settable(
'max_completion_table_items',
int,
"Maximum number of completion results allowed for a completion table to appear",
"Max results allowed to display a table",
self,
)
)
self.add_settable(
Settable(
'max_column_completion_results',
int,
"Maximum number of completion results to display in a single column",
"Max results to display in a single column",
self,
)
)
Expand Down Expand Up @@ -2496,11 +2504,13 @@ def _get_settable_choices(self) -> Choices:
items: list[CompletionItem] = []

for name, settable in self.settables.items():
value_str = str(settable.value)
table_row = [
str(settable.value),
value_str,
settable.description,
]
items.append(CompletionItem(name, display_meta=str(settable.value), table_row=table_row))
display_meta = f"[Current: {su.stylize(value_str, Style(bold=True))}] {settable.description}"
items.append(CompletionItem(name, display_meta=display_meta, table_row=table_row))

return Choices(items=items)

Expand Down
4 changes: 2 additions & 2 deletions cmd2/pt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ def get_completions(self, document: Document, _complete_event: object) -> Iterab
yield Completion(
match_text,
start_position=start_position,
display=item.display,
display_meta=item.display_meta,
display=ANSI(item.display),
display_meta=ANSI(item.display_meta),
)


Expand Down
6 changes: 3 additions & 3 deletions tests/test_cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2349,9 +2349,9 @@ def test_get_settable_choices(base_app: cmd2.Cmd) -> None:
assert cur_settable is not None

str_value = str(cur_settable.value)
assert cur_choice.display_meta == str_value
assert cur_choice.table_row[0] == str_value
assert cur_choice.table_row[1] == cur_settable.description
assert str_value in cur_choice.display_meta
assert ru.rich_text_to_string(cur_choice.table_row[0]) == str_value
assert ru.rich_text_to_string(cur_choice.table_row[1]) == cur_settable.description


def test_completion_supported(base_app) -> None:
Expand Down
36 changes: 28 additions & 8 deletions tests/test_pt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@
import pytest
from prompt_toolkit.buffer import Buffer
from prompt_toolkit.document import Document
from prompt_toolkit.formatted_text import (
ANSI,
to_formatted_text,
)

import cmd2
from cmd2 import pt_utils, utils
from cmd2 import (
Cmd2Style,
pt_utils,
stylize,
utils,
)
from cmd2.history import HistoryItem
from cmd2.parsing import Statement

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

# Test plain and styled values for display and display_meta
foo_text = "foo"
foo_display = "Foo Display"
foo_meta = "Foo Meta"

bar_text = "bar"
bar_display = stylize("Bar Display", Cmd2Style.SUCCESS)
bar_meta = stylize("Bar Meta", Cmd2Style.WARNING)

# Set up matches
completion_items = [
cmd2.CompletionItem("foo", display="Foo Display"),
cmd2.CompletionItem("bar", display="Bar Display"),
cmd2.CompletionItem(foo_text, display=foo_display, display_meta=foo_meta),
cmd2.CompletionItem(bar_text, display=bar_display, display_meta=bar_meta),
]
cmd2_completions = cmd2.Completions(completion_items, completion_table="Table Data")
mock_cmd_app.complete.return_value = cmd2_completions

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

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

assert completions[1].text == "foo"
assert completions[1].display == [('', 'Foo Display')]
assert completions[0].text == bar_text
assert to_formatted_text(completions[0].display) == to_formatted_text(ANSI(bar_display))
assert to_formatted_text(completions[0].display_meta) == to_formatted_text(ANSI(bar_meta))

assert completions[1].text == foo_text
assert to_formatted_text(completions[1].display) == to_formatted_text(ANSI(foo_display))
assert to_formatted_text(completions[1].display_meta) == to_formatted_text(ANSI(foo_meta))

# Verify that only the completion table printed
assert mock_print.call_count == 1
Expand Down
Loading