diff options
author | kmvanbrunt <kmvanbrunt@gmail.com> | 2018-05-26 01:17:48 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-26 01:17:48 -0400 |
commit | 6b6a36c41bd4d1ce3c7b8fbe46d935e599ccf744 (patch) | |
tree | bf57a15570248a2e3fbfb058a817d6c743cc9167 /cmd2/rl_utils.py | |
parent | cad21a60fa92ebe4a7c177142d273f9f7497967b (diff) | |
parent | 4c67d3372db924e87be920d89aadecb946584b31 (diff) | |
download | cmd2-git-6b6a36c41bd4d1ce3c7b8fbe46d935e599ccf744.tar.gz |
Merge pull request #416 from python-cmd2/pyshell_readline
Pyshell readline
Diffstat (limited to 'cmd2/rl_utils.py')
-rw-r--r-- | cmd2/rl_utils.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/cmd2/rl_utils.py b/cmd2/rl_utils.py index 8ef65d28..55ca4a12 100644 --- a/cmd2/rl_utils.py +++ b/cmd2/rl_utils.py @@ -33,6 +33,38 @@ 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. + ############################################################################################################ + # readline.redisplay() + try: + getattr(readline, 'redisplay') + except AttributeError: + # noinspection PyProtectedMember + readline.redisplay = readline.rl.mode._update_line + + # readline.remove_history_item() + 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__: |