diff options
| author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2021-11-15 21:28:26 -0500 |
|---|---|---|
| committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2021-11-15 21:31:34 -0500 |
| commit | 7db85fb86b85c0c870f196ec9719783e06a58a90 (patch) | |
| tree | 90b207043aa1446dbd1f823f0d24f5bb3ef26888 /cmd2/argparse_completer.py | |
| parent | e52748bc71aabd07e4f657a13b6853331ca2c342 (diff) | |
| download | cmd2-git-completion_item_choices.tar.gz | |
ArgparseCompleter now sorts CompletionItems created with numerical values as numberscompletion_item_choices
Diffstat (limited to 'cmd2/argparse_completer.py')
| -rw-r--r-- | cmd2/argparse_completer.py | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/cmd2/argparse_completer.py b/cmd2/argparse_completer.py index 26835513..e97aadb0 100644 --- a/cmd2/argparse_completer.py +++ b/cmd2/argparse_completer.py @@ -547,15 +547,29 @@ class ArgparseCompleter: return matches def _format_completions(self, arg_state: _ArgumentState, completions: Union[List[str], List[CompletionItem]]) -> List[str]: - # Check if the results are CompletionItems and that there aren't too many to display - if 1 < len(completions) <= self._cmd2_app.max_completion_items and isinstance(completions[0], CompletionItem): - completion_items = cast(List[CompletionItem], completions) - four_spaces = 4 * ' ' + """Format CompletionItems into hint table""" + + # Nothing to do if we don't have at least 2 completions which are all CompletionItems + if len(completions) < 2 or not all(isinstance(c, CompletionItem) for c in completions): + return cast(List[str], completions) + + completion_items = cast(List[CompletionItem], completions) - # If the user has not already sorted the CompletionItems, then sort them before appending the descriptions - if not self._cmd2_app.matches_sorted: + # Sort CompletionItems before building the hint table + if not self._cmd2_app.matches_sorted: + # If all orig_value types are numbers, then sort by that value + if all(isinstance(c.orig_value, numbers.Number) for c in completion_items): + completion_items.sort(key=lambda c: c.orig_value) # type: ignore[no-any-return] + + # Otherwise sort as strings + else: completion_items.sort(key=self._cmd2_app.default_sort_key) - self._cmd2_app.matches_sorted = True + + self._cmd2_app.matches_sorted = True + + # Check if there are too many CompletionItems to display as a table + if len(completions) <= self._cmd2_app.max_completion_items: + four_spaces = 4 * ' ' # If a metavar was defined, use that instead of the dest field destination = arg_state.action.metavar if arg_state.action.metavar else arg_state.action.dest @@ -595,6 +609,7 @@ class ArgparseCompleter: table_data = [[item, item.description] for item in completion_items] self._cmd2_app.formatted_completions = hint_table.generate_table(table_data, row_spacing=0) + # Return sorted list of completions return cast(List[str], completions) def complete_subcommand_help(self, text: str, line: str, begidx: int, endidx: int, tokens: List[str]) -> List[str]: |
