Skip to content

Commit 70b6139

Browse files
committed
Renamed completion_hint to hint.
Renamed completion_error to error. Renamed completion_table to table and converted it from a string to a Rich Table.
1 parent a89beaf commit 70b6139

10 files changed

Lines changed: 132 additions & 91 deletions

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ prompt is displayed.
4242
- `Cmd.default_sort_key` moved to `utils.DEFAULT_STR_SORT_KEY`.
4343
- Moved completion state data, which previously resided in `Cmd`, into other classes.
4444
- `Cmd.matches_sorted` -> `Completions.is_sorted` and `Choices.is_sorted`
45-
- `Cmd.completion_hint` -> `Completions.completion_hint`
46-
- `Cmd.formatted_completions` -> `Completions.completion_table`
45+
- `Cmd.completion_hint` -> `Completions.hint`
46+
- `Cmd.formatted_completions` -> `Completions.table` (Now a Rich Table)
4747
- `Cmd.allow_appended_space/allow_closing_quote` -> `Completions.allow_finalization`
4848
- Removed `Cmd.matches_delimited` since it's no longer used.
4949
- Removed `flag_based_complete` and `index_based_complete` functions since their functionality

cmd2/argparse_completer.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from rich.text import Text
2626

2727
from .constants import INFINITY
28-
from .rich_utils import Cmd2GeneralConsole
2928

3029
if TYPE_CHECKING: # pragma: no cover
3130
from .cmd2 import Cmd
@@ -500,11 +499,11 @@ def _handle_last_token(
500499

501500
# If we have results, then return them
502501
if completions:
503-
if not completions.completion_hint:
502+
if not completions.hint:
504503
# Add a hint even though there are results in case Cmd.always_show_hint is True.
505504
completions = dataclasses.replace(
506505
completions,
507-
completion_hint=_build_hint(self._parser, flag_arg_state.action),
506+
hint=_build_hint(self._parser, flag_arg_state.action),
508507
)
509508

510509
return completions
@@ -528,11 +527,11 @@ def _handle_last_token(
528527

529528
# If we have results, then return them
530529
if completions:
531-
if not completions.completion_hint:
530+
if not completions.hint:
532531
# Add a hint even though there are results in case Cmd.always_show_hint is True.
533532
completions = dataclasses.replace(
534533
completions,
535-
completion_hint=_build_hint(self._parser, pos_arg_state.action),
534+
hint=_build_hint(self._parser, pos_arg_state.action),
536535
)
537536
return completions
538537

@@ -592,8 +591,8 @@ def _complete_flags(self, text: str, line: str, begidx: int, endidx: int, used_f
592591

593592
return Completions(items)
594593

595-
def _format_completions(self, arg_state: _ArgumentState, completions: Completions) -> Completions:
596-
"""Format CompletionItems into completion table."""
594+
def _build_completion_table(self, arg_state: _ArgumentState, completions: Completions) -> Completions:
595+
"""Build a rich.Table for completion results if applicable."""
597596
# Skip table generation for single results or if the list exceeds the
598597
# user-defined threshold for table display.
599598
if len(completions) < 2 or len(completions) > self._cmd2_app.max_completion_table_items:
@@ -627,19 +626,14 @@ def _format_completions(self, arg_state: _ArgumentState, completions: Completion
627626
column if isinstance(column, Column) else Column(column, overflow="fold") for column in table_header
628627
)
629628

630-
# Add the data rows
631-
hint_table = Table(*rich_columns, box=SIMPLE_HEAD, show_edge=False, border_style=Cmd2Style.TABLE_BORDER)
629+
# Build the table
630+
table = Table(*rich_columns, box=SIMPLE_HEAD, show_edge=False, border_style=Cmd2Style.TABLE_BORDER)
632631
for item in completions:
633-
hint_table.add_row(Text.from_ansi(item.display), *item.table_row)
634-
635-
# Generate the table string
636-
console = Cmd2GeneralConsole(file=self._cmd2_app.stdout)
637-
with console.capture() as capture:
638-
console.print(hint_table, end="", soft_wrap=False)
632+
table.add_row(Text.from_ansi(item.display), *item.table_row)
639633

640634
return dataclasses.replace(
641635
completions,
642-
completion_table=capture.get(),
636+
table=table,
643637
)
644638

645639
def complete_subcommand_help(self, text: str, line: str, begidx: int, endidx: int, tokens: Sequence[str]) -> Completions:
@@ -780,7 +774,7 @@ def _complete_arg(
780774
filtered = [choice for choice in all_choices if choice.text not in used_values]
781775
completions = self._cmd2_app.basic_complete(text, line, begidx, endidx, filtered)
782776

783-
return self._format_completions(arg_state, completions)
777+
return self._build_completion_table(arg_state, completions)
784778

785779

786780
# The default ArgparseCompleter class for a cmd2 app

cmd2/cmd2.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,26 +2467,26 @@ def complete(
24672467
return completions # noqa: TRY300
24682468

24692469
except CompletionError as ex:
2470-
err_str = str(ex)
2471-
completion_error = ""
2470+
error_msg = str(ex)
2471+
formatted_error = ""
24722472

24732473
# Don't display anything if the error is blank (e.g. _NoResultsError for an argument which supresses hints)
2474-
if err_str:
2474+
if error_msg:
24752475
# _NoResultsError completion hints already include a trailing "\n".
24762476
end = "" if isinstance(ex, argparse_completer._NoResultsError) else "\n"
24772477

24782478
console = Cmd2GeneralConsole(file=self.stdout)
24792479
with console.capture() as capture:
24802480
console.print(
2481-
err_str,
2481+
error_msg,
24822482
style=Cmd2Style.ERROR if ex.apply_style else "",
24832483
end=end,
24842484
)
2485-
completion_error = capture.get()
2486-
return Completions(completion_error=completion_error)
2485+
formatted_error = capture.get()
2486+
return Completions(error=formatted_error)
24872487
except Exception as ex: # noqa: BLE001
24882488
formatted_exception = self.format_exception(ex)
2489-
return Completions(completion_error=formatted_exception)
2489+
return Completions(error=formatted_exception)
24902490

24912491
def in_script(self) -> bool:
24922492
"""Return whether a text script is running."""

cmd2/completion.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
overload,
1919
)
2020

21+
from rich.table import Table
22+
2123
from . import string_utils as su
2224

2325
if sys.version_info >= (3, 11):
@@ -214,14 +216,14 @@ class Choices(CompletionResultsBase):
214216
class Completions(CompletionResultsBase):
215217
"""The results of a completion operation."""
216218

217-
# An optional hint which prints above completion suggestions
218-
completion_hint: str = ""
219+
# Optional hint which prints above completion suggestions
220+
hint: str = ""
219221

220222
# Optional message to display if an error occurs during completion
221-
completion_error: str = ""
223+
error: str = ""
222224

223-
# An optional table string populated by the argparse completer
224-
completion_table: str = ""
225+
# Optional Rich table which provides more context for the data being completed
226+
table: Table | None = None
225227

226228
# If True, the completion engine is allowed to finalize a completion
227229
# when a single match is found by appending a trailing space and

cmd2/pt_utils.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,20 @@ def get_completions(self, document: Document, _complete_event: object) -> Iterab
8686
text, line=line, begidx=begidx, endidx=endidx, custom_settings=self.custom_settings
8787
)
8888

89-
if completions.completion_error:
90-
print_formatted_text(pt_filter_style(completions.completion_error))
89+
if completions.error:
90+
print_formatted_text(pt_filter_style(completions.error))
9191
return
9292

9393
# Print completion table if present
94-
if completions.completion_table:
95-
print_formatted_text(pt_filter_style("\n" + completions.completion_table))
94+
if completions.table is not None:
95+
console = ru.Cmd2GeneralConsole(file=self.cmd_app.stdout)
96+
with console.capture() as capture:
97+
console.print(completions.table, end="", soft_wrap=False)
98+
print_formatted_text(pt_filter_style("\n" + capture.get()))
9699

97100
# Print hint if present and settings say we should
98-
if completions.completion_hint and (self.cmd_app.always_show_hint or not completions):
99-
print_formatted_text(pt_filter_style(completions.completion_hint))
101+
if completions.hint and (self.cmd_app.always_show_hint or not completions):
102+
print_formatted_text(pt_filter_style(completions.hint))
100103

101104
if not completions:
102105
return

examples/argparse_completion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def choices_arg_tokens(self, arg_tokens: dict[str, list[str]]) -> Choices:
114114
choices_provider=choices_completion_tables,
115115
metavar="ITEM_ID",
116116
table_header=["Description"],
117-
help="demonstrate use of CompletionItems",
117+
help="demonstrate use of completion table",
118118
)
119119

120120
# Demonstrate use of arg_tokens dictionary

0 commit comments

Comments
 (0)