From 745a9d433dda375636540f6ce0e32a644b4d1df9 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Mon, 7 May 2018 15:11:16 -0400 Subject: Backporting readline warning --- cmd2.py | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/cmd2.py b/cmd2.py index 82f79ce2..98907b67 100755 --- a/cmd2.py +++ b/cmd2.py @@ -155,15 +155,21 @@ if 'pyreadline' in sys.modules: orig_pyreadline_display = readline.rl.mode._display_completions elif 'gnureadline' in sys.modules or 'readline' in sys.modules: - rl_type = RlType.GNU + # We don't support libedit + if 'libedit' not in readline.__doc__: + rl_type = RlType.GNU - # We need wcswidth to calculate display width of tab completions - from wcwidth import wcswidth + # We need wcswidth to calculate display width of tab completions + from wcwidth import wcswidth - # Load the readline lib so we can make changes to it - import ctypes - readline_lib = ctypes.CDLL(readline.__file__) + # Load the readline lib so we can make changes to it + import ctypes + readline_lib = ctypes.CDLL(readline.__file__) +if rl_type == RlType.NONE: + rl_err_msg = "Tab completion has been disabled since no supported version of readline was found\n" + rl_err_msg += "To resolve this, install pyreadline on Windows or gnureadline on Mac\n" + sys.stderr.write(self.colorize(rl_err_msg, 'yellow')) # BrokenPipeError and FileNotFoundError exist only in Python 3. Use IOError for Python 2. if six.PY3: @@ -690,6 +696,9 @@ def strip_ansi(text): def _pop_readline_history(clear_history=True): """Returns a copy of readline's history and optionally clears it (default)""" # noinspection PyArgumentList + if rl_type == RlType.NONE: + return [] + history = [ readline.get_history_item(i) for i in range(1, 1 + readline.get_current_history_length()) @@ -701,10 +710,11 @@ def _pop_readline_history(clear_history=True): def _push_readline_history(history, clear_history=True): """Restores readline's history and optionally clears it first (default)""" - if clear_history: - readline.clear_history() - for line in history: - readline.add_history(line) + if rl_type != RlType.NONE: + if clear_history: + readline.clear_history() + for line in history: + readline.add_history(line) def _complete_from_cmd(cmd_obj, text, line, begidx, endidx): @@ -838,7 +848,7 @@ class AddSubmenu(object): original_attributes = self._get_original_attributes() history = _pop_readline_history() - if self.persistent_history_file: + if self.persistent_history_file and rl_type != RlType.NONE: try: readline.read_history_file(self.persistent_history_file) except FILE_NOT_FOUND_ERROR: @@ -871,7 +881,7 @@ class AddSubmenu(object): self._copy_out_shared_attrs(parent_cmd, original_attributes) # write submenu history - if self.persistent_history_file: + if self.persistent_history_file and rl_type != RlType.NONE: readline.write_history_file(self.persistent_history_file) # reset main app history before exit _push_readline_history(history) @@ -1030,7 +1040,7 @@ class Cmd(cmd.Cmd): pass # If persistent readline history is enabled, then read history from file and register to write to file at exit - if persistent_history_file: + if persistent_history_file and rl_type != RlType.NONE: persistent_history_file = os.path.expanduser(persistent_history_file) try: readline.read_history_file(persistent_history_file) @@ -1972,7 +1982,7 @@ class Cmd(cmd.Cmd): :param text: str - the current word that user is typing :param state: int - non-negative integer """ - if state == 0: + if state == 0 and rl_type != RlType.NONE: unclosed_quote = '' self.set_completion_defaults() @@ -3061,9 +3071,12 @@ Usage: Usage: unalias [-a] name [name ...] self.poutput(' %2d. %s\n' % (idx + 1, text)) while True: response = sm.input(prompt) - hlen = readline.get_current_history_length() - if hlen >= 1 and response != '': - readline.remove_history_item(hlen - 1) + + if rl_type != RlType.NONE: + hlen = readline.get_current_history_length() + if hlen >= 1 and response != '': + readline.remove_history_item(hlen - 1) + try: response = int(response) result = fulloptions[response - 1][0] -- cgit v1.2.1 From cbf1a7c0afbf6bc580436341c4f6bfcdf9b0e902 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Mon, 7 May 2018 15:16:03 -0400 Subject: Removed color --- cmd2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd2.py b/cmd2.py index 98907b67..5c5f0792 100755 --- a/cmd2.py +++ b/cmd2.py @@ -169,7 +169,7 @@ elif 'gnureadline' in sys.modules or 'readline' in sys.modules: if rl_type == RlType.NONE: rl_err_msg = "Tab completion has been disabled since no supported version of readline was found\n" rl_err_msg += "To resolve this, install pyreadline on Windows or gnureadline on Mac\n" - sys.stderr.write(self.colorize(rl_err_msg, 'yellow')) + sys.stderr.write(rl_err_msg) # BrokenPipeError and FileNotFoundError exist only in Python 3. Use IOError for Python 2. if six.PY3: -- cgit v1.2.1