Skip to content

Commit 5e6a929

Browse files
authored
Merge pull request #394 from python-cmd2/readline_warning
Added warning if tab completion will be disabled. Not allowing libedit
2 parents bfdb482 + 176c06d commit 5e6a929

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

cmd2/cmd2.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import cmd
2828
import codecs
2929
import collections
30+
from colorama import Fore
3031
import copy
3132
import datetime
3233
import functools
@@ -52,7 +53,14 @@
5253
from . import utils
5354

5455
# Set up readline
55-
from .rl_utils import rl_force_redisplay, readline, rl_type, RlType
56+
from .rl_utils import rl_type, RlType
57+
if rl_type == RlType.NONE:
58+
rl_err_msg = "Tab completion has been disabled since no supported version of readline was found\n"
59+
rl_err_msg += "To resolve this, install pyreadline on Windows or gnureadline on Mac\n"
60+
sys.stderr.write(Fore.LIGHTYELLOW_EX + rl_err_msg + Fore.RESET)
61+
else:
62+
from .rl_utils import rl_force_redisplay, readline
63+
5664
from .argparse_completer import AutoCompleter, ACArgumentParser
5765

5866
from cmd2.parsing import StatementParser, Statement
@@ -321,6 +329,9 @@ class EmptyStatement(Exception):
321329
def _pop_readline_history(clear_history: bool=True) -> List[str]:
322330
"""Returns a copy of readline's history and optionally clears it (default)"""
323331
# noinspection PyArgumentList
332+
if rl_type == RlType.NONE:
333+
return []
334+
324335
history = [
325336
readline.get_history_item(i)
326337
for i in range(1, 1 + readline.get_current_history_length())
@@ -332,10 +343,11 @@ def _pop_readline_history(clear_history: bool=True) -> List[str]:
332343

333344
def _push_readline_history(history, clear_history=True):
334345
"""Restores readline's history and optionally clears it first (default)"""
335-
if clear_history:
336-
readline.clear_history()
337-
for line in history:
338-
readline.add_history(line)
346+
if rl_type != RlType.NONE:
347+
if clear_history:
348+
readline.clear_history()
349+
for line in history:
350+
readline.add_history(line)
339351

340352

341353
def _complete_from_cmd(cmd_obj, text, line, begidx, endidx):
@@ -469,7 +481,7 @@ def enter_submenu(parent_cmd, statement):
469481
original_attributes = self._get_original_attributes()
470482
history = _pop_readline_history()
471483

472-
if self.persistent_history_file:
484+
if self.persistent_history_file and rl_type != RlType.NONE:
473485
try:
474486
readline.read_history_file(self.persistent_history_file)
475487
except FileNotFoundError:
@@ -499,7 +511,7 @@ def enter_submenu(parent_cmd, statement):
499511
self._copy_out_shared_attrs(parent_cmd, original_attributes)
500512

501513
# write submenu history
502-
if self.persistent_history_file:
514+
if self.persistent_history_file and rl_type != RlType.NONE:
503515
readline.write_history_file(self.persistent_history_file)
504516
# reset main app history before exit
505517
_push_readline_history(history)
@@ -654,7 +666,7 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, persistent_histor
654666
pass
655667

656668
# If persistent readline history is enabled, then read history from file and register to write to file at exit
657-
if persistent_history_file:
669+
if persistent_history_file and rl_type != RlType.NONE:
658670
persistent_history_file = os.path.expanduser(persistent_history_file)
659671
try:
660672
readline.read_history_file(persistent_history_file)
@@ -1544,7 +1556,7 @@ def complete(self, text, state):
15441556
:param text: str - the current word that user is typing
15451557
:param state: int - non-negative integer
15461558
"""
1547-
if state == 0:
1559+
if state == 0 and rl_type != RlType.NONE:
15481560
unclosed_quote = ''
15491561
self.set_completion_defaults()
15501562

@@ -2616,9 +2628,12 @@ def select(self, opts, prompt='Your choice? '):
26162628
self.poutput(' %2d. %s\n' % (idx + 1, text))
26172629
while True:
26182630
response = input(prompt)
2619-
hlen = readline.get_current_history_length()
2620-
if hlen >= 1 and response != '':
2621-
readline.remove_history_item(hlen - 1)
2631+
2632+
if rl_type != RlType.NONE:
2633+
hlen = readline.get_current_history_length()
2634+
if hlen >= 1 and response != '':
2635+
readline.remove_history_item(hlen - 1)
2636+
26222637
try:
26232638
response = int(response)
26242639
result = fulloptions[response - 1][0]

cmd2/rl_utils.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ class RlType(Enum):
3434
rl_type = RlType.PYREADLINE
3535

3636
elif 'gnureadline' in sys.modules or 'readline' in sys.modules:
37-
rl_type = RlType.GNU
37+
# We don't support libedit
38+
if 'libedit' not in readline.__doc__:
39+
rl_type = RlType.GNU
3840

39-
# Load the readline lib so we can access members of it
40-
import ctypes
41-
readline_lib = ctypes.CDLL(readline.__file__)
41+
# Load the readline lib so we can access members of it
42+
import ctypes
43+
readline_lib = ctypes.CDLL(readline.__file__)
4244

4345

4446
def rl_force_redisplay() -> None:

0 commit comments

Comments
 (0)