From 0ccdc036665e30552a6d3dac79fe3402e4ee84ab Mon Sep 17 00:00:00 2001 From: Hrishi Hiraskar Date: Fri, 5 Oct 2018 03:37:23 +0530 Subject: [PATCH] Implemented show help on ? key press --- ishell/command.py | 3 ++- ishell/console.py | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ishell/command.py b/ishell/command.py index 90e9fee..d2a03ad 100644 --- a/ishell/command.py +++ b/ishell/command.py @@ -111,7 +111,8 @@ def run(self, line): _print("Exec %s(line=%s), overwrite this method!" % (self.name, line)) return - print("\nIncomplete Command: %s\n" % line) + if len(str(line)) > 0 and line[-1] != "?": + print("\nIncomplete Command: %s\n" % line) self.print_childs_help() def __repr__(self): diff --git a/ishell/console.py b/ishell/console.py index a620810..23cec4f 100644 --- a/ishell/console.py +++ b/ishell/console.py @@ -49,7 +49,8 @@ def walk(self, buf, state, run=False, full_line=None): return cmd.complete(line_commands[1:], buf, state, run, full_line) if run: - print("Unknown Command: %s" % buf) + if buf != "?": + print("Unknown Command: %s" % buf) self.print_childs_help() return # Needing completion @@ -79,6 +80,7 @@ def exit(self): def loop(self): previous_completer = readline.get_completer() readline.parse_and_bind("tab: complete") + readline.parse_and_bind('?: "--help^\n"') readline.set_completer(self.walk) prompt = self.prompt + self.prompt_delim if not ishell._current_prompt: @@ -86,13 +88,32 @@ def loop(self): else: previous_prompt = ishell._current_prompt ishell._current_prompt = prompt + previous_command = "" while 1: try: sys.stdout.write("\r") if self._exit: break sys.stdout.write("\033[K") + readline.set_startup_hook( + lambda: readline.insert_text(previous_command)) input_ = input(prompt + " ") + readline.set_startup_hook() + if len(str(input_)) >= 7 and input_[-7:] == "--help^": + sys.stdout.write('\x1b[1A') + sys.stdout.write('\x1b[2K') + input_ = input_[:-7] + "?" + previous_command = input_[0:-1] + history_len = readline.get_current_history_length() + readline.remove_history_item(history_len - 1) + if len(str(input_)) > 1 and input_[-2] != " ": + previous_command += "?" + continue + else: + readline.add_history(previous_command + "?") + print(prompt + " " + previous_command + "?") + else: + previous_command = "" if not input_.strip(): self.print_childs_help() elif input_ in ('quit', 'exit'):