summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xcmd2.py47
1 files changed, 30 insertions, 17 deletions
diff --git a/cmd2.py b/cmd2.py
index 82f79ce2..5c5f0792 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(rl_err_msg)
# 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]