summaryrefslogtreecommitdiff
path: root/cmd2/cmd2.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2019-08-07 20:29:50 -0400
committerGitHub <noreply@github.com>2019-08-07 20:29:50 -0400
commitcc3d5cd210902e899ee34cd4f71c344fdc075ecb (patch)
treeeccdd7cfb4c189cdb834a2939a89a5cd47e7bfc2 /cmd2/cmd2.py
parent89a5a646ffa2f72154b932f0b2dbf23fd87866e3 (diff)
parenta14c63d9c8edb8c07a8402a507993a6fed2068f8 (diff)
downloadcmd2-git-cc3d5cd210902e899ee34cd4f71c344fdc075ecb.tar.gz
Merge pull request #754 from python-cmd2/no_tab_select
Disabled tab completion during a select call
Diffstat (limited to 'cmd2/cmd2.py')
-rwxr-xr-xcmd2/cmd2.py44
1 files changed, 40 insertions, 4 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index d82e58c9..a7cd8fac 100755
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -684,7 +684,7 @@ class Cmd(cmd.Cmd):
final_msg = ansi.style_error(final_msg)
if not self.debug:
- warning = "\nTo enable full traceback, run the following command: 'set debug true'"
+ warning = "\nTo enable full traceback, run the following command: 'set debug true'"
final_msg += ansi.style_warning(warning)
# Set apply_style to False since style has already been applied
@@ -2890,6 +2890,28 @@ class Cmd(cmd.Cmd):
| a list of tuples -> interpreted as (value, text), so
that the return value can differ from
the text advertised to the user """
+
+ completion_disabled = False
+ orig_completer = None
+
+ def disable_completion():
+ """Turn off completion during the select input line"""
+ nonlocal orig_completer
+ nonlocal completion_disabled
+
+ if rl_type != RlType.NONE and not completion_disabled:
+ orig_completer = readline.get_completer()
+ readline.set_completer(lambda *args, **kwargs: None)
+ completion_disabled = True
+
+ def enable_completion():
+ """Restore tab completion when select is done reading input"""
+ nonlocal completion_disabled
+
+ if rl_type != RlType.NONE and completion_disabled:
+ readline.set_completer(orig_completer)
+ completion_disabled = False
+
local_opts = opts
if isinstance(opts, str):
local_opts = list(zip(opts.split(), opts.split()))
@@ -2904,15 +2926,28 @@ class Cmd(cmd.Cmd):
fulloptions.append((opt[0], opt[0]))
for (idx, (_, text)) in enumerate(fulloptions):
self.poutput(' %2d. %s' % (idx + 1, text))
+
while True:
safe_prompt = rl_make_safe_prompt(prompt)
- response = input(safe_prompt)
+
+ try:
+ with self.sigint_protection:
+ disable_completion()
+ response = input(safe_prompt)
+ except EOFError:
+ response = ''
+ self.poutput('\n', end='')
+ finally:
+ with self.sigint_protection:
+ enable_completion()
+
+ if not response:
+ continue
if rl_type != RlType.NONE:
hlen = readline.get_current_history_length()
- if hlen >= 1 and response != '':
+ if hlen >= 1:
readline.remove_history_item(hlen - 1)
-
try:
choice = int(response)
if choice < 1:
@@ -2922,6 +2957,7 @@ class Cmd(cmd.Cmd):
except (ValueError, IndexError):
self.poutput("{!r} isn't a valid choice. Pick a number between 1 and {}:".format(
response, len(fulloptions)))
+
return result
def _get_read_only_settings(self) -> str: