From 4ce81a71de2aeb184f64d360d1774b8698d16ba3 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Wed, 23 May 2018 03:07:23 -0400 Subject: Stopped sharing history between cmd2 and python console --- cmd2/rl_utils.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'cmd2/rl_utils.py') diff --git a/cmd2/rl_utils.py b/cmd2/rl_utils.py index 8ef65d28..5d278a0b 100644 --- a/cmd2/rl_utils.py +++ b/cmd2/rl_utils.py @@ -33,6 +33,29 @@ rl_type = RlType.NONE if 'pyreadline' in sys.modules: rl_type = RlType.PYREADLINE + # pyreadline is incomplete in terms of the Python readline API + # Add the missing functions we need + try: + getattr(readline, 'remove_history_item') + except AttributeError: + # noinspection PyProtectedMember + def pyreadline_remove_history_item(pos: int) -> None: + """ + An implementation of remove_history_item() for pyreadline + :param pos: The 0-based position in history to remove + """ + # Save of the current location of the history cursor + saved_cursor = readline.rl.mode._history.history_cursor + + # Delete the history item + del(readline.rl.mode._history.history[pos]) + + # Update the cursor if needed + if saved_cursor > pos: + readline.rl.mode._history.history_cursor -= 1 + + readline.remove_history_item = pyreadline_remove_history_item + elif 'gnureadline' in sys.modules or 'readline' in sys.modules: # We don't support libedit if 'libedit' not in readline.__doc__: -- cgit v1.2.1