summaryrefslogtreecommitdiff
path: root/cmd2/argparse_completer.py
diff options
context:
space:
mode:
authorEric Lin <anselor@gmail.com>2018-06-06 17:53:44 -0400
committerEric Lin <anselor@gmail.com>2018-06-06 17:53:44 -0400
commit13bb0655d1ceddcce1fb17aa069dcbf079b3bda8 (patch)
treea621906a8921ad2d4357d495fc159d18c28d7363 /cmd2/argparse_completer.py
parent98522702b9131c41741af07f7acdf74b7f25d4d7 (diff)
downloadcmd2-git-13bb0655d1ceddcce1fb17aa069dcbf079b3bda8.tar.gz
Adds possible support for tabular tab completion results.
Diffstat (limited to 'cmd2/argparse_completer.py')
-rwxr-xr-xcmd2/argparse_completer.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/cmd2/argparse_completer.py b/cmd2/argparse_completer.py
index d98a6eac..1522101c 100755
--- a/cmd2/argparse_completer.py
+++ b/cmd2/argparse_completer.py
@@ -59,6 +59,7 @@ Released under MIT license, see LICENSE file
import argparse
from colorama import Fore
+import os
import sys
from typing import List, Dict, Tuple, Callable, Union
@@ -78,6 +79,15 @@ ACTION_ARG_CHOICES = 'arg_choices'
ACTION_SUPPRESS_HINT = 'suppress_hint'
+class CompletionItem(str):
+ def __new__(cls, o, desc='', *args, **kwargs):
+ return str.__new__(cls, o, *args, **kwargs)
+
+ # noinspection PyMissingConstructor,PyUnusedLocal
+ def __init__(self, o, desc='', *args, **kwargs):
+ self.description = desc
+
+
class _RangeAction(object):
def __init__(self, nargs: Union[int, str, Tuple[int, int], None]):
self.nargs_min = None
@@ -413,6 +423,8 @@ class AutoCompleter(object):
completion_results = self._complete_for_arg(flag_action, text, line, begidx, endidx, consumed)
if not completion_results:
self._print_action_help(flag_action)
+ elif len(completion_results) > 1:
+ completion_results = self._format_completions(flag_action, completion_results)
# ok, we're not a flag, see if there's a positional argument to complete
else:
@@ -422,9 +434,37 @@ class AutoCompleter(object):
completion_results = self._complete_for_arg(pos_action, text, line, begidx, endidx, consumed)
if not completion_results:
self._print_action_help(pos_action)
+ elif len(completion_results) > 1:
+ completion_results = self._format_completions(pos_action, completion_results)
return completion_results
+ def _format_completions(self, action, completions: List[Union[str, CompletionItem]]):
+ if completions and len(completions) > 1 and \
+ isinstance(completions[0], CompletionItem):
+
+ token_width = len(action.dest)
+ completions_with_desc = []
+
+ for item in completions:
+ if len(item) > token_width:
+ token_width = len(item)
+
+ term_size = os.get_terminal_size()
+ fill_width = int(term_size.columns * .6) - (token_width + 2)
+ for item in completions:
+ entry = '{: <{token_width}}{: <{fill_width}}'.format(item, item.description,
+ token_width=token_width+2,
+ fill_width=fill_width)
+ completions_with_desc.append(entry)
+
+ header = '\n{: <{token_width}}{}'.format(action.dest, action.desc_header, token_width=token_width+2)
+ print(header)
+
+ self._cmd2_app.display_matches = completions_with_desc
+
+ return completions
+
def complete_command_help(self, tokens: List[str], text: str, line: str, begidx: int, endidx: int) -> List[str]:
"""Supports the completion of sub-commands for commands through the cmd2 help command."""
for idx, token in enumerate(tokens):