2727import cmd
2828import codecs
2929import collections
30+ from colorama import Fore
3031import copy
3132import datetime
3233import functools
5253from . 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+
5664from .argparse_completer import AutoCompleter , ACArgumentParser
5765
5866from cmd2 .parsing import StatementParser , Statement
@@ -321,6 +329,9 @@ class EmptyStatement(Exception):
321329def _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
333344def _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
341353def _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
@@ -2617,9 +2629,12 @@ def select(self, opts, prompt='Your choice? '):
26172629 self .poutput (' %2d. %s\n ' % (idx + 1 , text ))
26182630 while True :
26192631 response = input (prompt )
2620- hlen = readline .get_current_history_length ()
2621- if hlen >= 1 and response != '' :
2622- readline .remove_history_item (hlen - 1 )
2632+
2633+ if rl_type != RlType .NONE :
2634+ hlen = readline .get_current_history_length ()
2635+ if hlen >= 1 and response != '' :
2636+ readline .remove_history_item (hlen - 1 )
2637+
26232638 try :
26242639 response = int (response )
26252640 result = fulloptions [response - 1 ][0 ]
0 commit comments