From 0b3258cdf2afd02828cefef46cf6e0e2f3bc953e Mon Sep 17 00:00:00 2001 From: naryal2580 Date: Thu, 24 Oct 2019 13:00:08 +0545 Subject: [PATCH 1/2] Added Tab Completion (if possible) --- adventurelib.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/adventurelib.py b/adventurelib.py index 4057eaf..53e3d32 100644 --- a/adventurelib.py +++ b/adventurelib.py @@ -114,6 +114,32 @@ def _match_context(context, active_context): active_context[clen:clen + len(CONTEXT_SEP)] in ('', CONTEXT_SEP) ) +class TabCompleter(object): + """Class, used if `readline` is available for Tab/Auto Comlpetion""" + def __init__(self, commands): + self.commands = commands + + def complete(self, text, state): + space = re.compile('.*\s+$', re.M) + "Generic readline completion entry point." + buffer = readline.get_line_buffer() + line = readline.get_line_buffer().split() + # show all commands + if not line: + return [c + ' ' for c in self.commands][state] + # account for last argument ending in a space + if space.match(buffer): + line.append('') + # resolve command to the implementation function + cmd = line[0].strip() + if cmd in self.commands: + impl = getattr(self, 'complete_%s' % cmd) + args = line[1:] + if args: + return (impl(args) + [None])[state] + return [cmd + ' '][state] + results = [c + ' ' for c in self.commands if c.startswith(cmd)] + [None] + return results[state] class InvalidCommand(Exception): """A command is not defined correctly.""" @@ -523,6 +549,17 @@ def start(help=True): qmark.orig_pattern = '?' commands.insert(0, (Pattern('help'), help, {})) commands.insert(0, (qmark, help, {})) + try: + commands = [] + for pattern, func, kwargs in _available_commands(): + commands.append(pattern.prefix[0]) + commands = list(set(commands)) + completer = TabCompleter(commands) + readline.set_completer_delims(' \t\n;') + readline.parse_and_bind("tab: complete") + readline.set_completer(completer.complete) + except NameError: + pass while True: try: cmd = input(prompt()).strip() From 882747f7d6d3d6362b6bb7c821872cfac61cb151 Mon Sep 17 00:00:00 2001 From: naryal2580 Date: Thu, 24 Oct 2019 13:19:34 +0545 Subject: [PATCH 2/2] Fixed CI checks --- adventurelib.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/adventurelib.py b/adventurelib.py index 53e3d32..d20f255 100644 --- a/adventurelib.py +++ b/adventurelib.py @@ -114,13 +114,14 @@ def _match_context(context, active_context): active_context[clen:clen + len(CONTEXT_SEP)] in ('', CONTEXT_SEP) ) + class TabCompleter(object): """Class, used if `readline` is available for Tab/Auto Comlpetion""" def __init__(self, commands): self.commands = commands def complete(self, text, state): - space = re.compile('.*\s+$', re.M) + space = re.compile('.*\\s+$', re.M) "Generic readline completion entry point." buffer = readline.get_line_buffer() line = readline.get_line_buffer().split() @@ -138,9 +139,11 @@ def complete(self, text, state): if args: return (impl(args) + [None])[state] return [cmd + ' '][state] - results = [c + ' ' for c in self.commands if c.startswith(cmd)] + [None] + results = [c + ' ' for c in self.commands if c.startswith(cmd)] \ + + [None] return results[state] + class InvalidCommand(Exception): """A command is not defined correctly.""" @@ -550,11 +553,11 @@ def start(help=True): commands.insert(0, (Pattern('help'), help, {})) commands.insert(0, (qmark, help, {})) try: - commands = [] + _commands = [] for pattern, func, kwargs in _available_commands(): - commands.append(pattern.prefix[0]) - commands = list(set(commands)) - completer = TabCompleter(commands) + _commands.append(pattern.prefix[0]) + _commands = list(set(_commands)) + completer = TabCompleter(_commands) readline.set_completer_delims(' \t\n;') readline.parse_and_bind("tab: complete") readline.set_completer(completer.complete)