diff options
author | kotfu <kotfu@kotfu.net> | 2019-05-24 23:03:22 -0600 |
---|---|---|
committer | kotfu <kotfu@kotfu.net> | 2019-05-24 23:03:22 -0600 |
commit | c13ff0f48b1e4652b6145a5b69ea2f439629afdf (patch) | |
tree | cc7709211f9c6900e99f76efb1cc8f56e4a1be06 /cmd2/cmd2.py | |
parent | 69308f455193d5b5f89a6edf7c7d3d052bdcd5ce (diff) | |
download | cmd2-git-c13ff0f48b1e4652b6145a5b69ea2f439629afdf.tar.gz |
Populate readline history from unpickled history
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r-- | cmd2/cmd2.py | 45 |
1 files changed, 23 insertions, 22 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index bfc59f3e..1cb48a0a 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -404,9 +404,8 @@ class Cmd(cmd.Cmd): # Commands to exclude from the history command # initialize history - self.persistent_history_file = persistent_history_file self.persistent_history_length = persistent_history_length - self._initialize_history() + self._initialize_history(persistent_history_file) self.exclude_from_history = '''history edit eof eos'''.split() # Command aliases and macros @@ -3448,7 +3447,7 @@ class Cmd(cmd.Cmd): for hi in history: self.poutput(hi.pr(script=args.script, expanded=args.expanded, verbose=args.verbose)) - def _initialize_history(self): + def _initialize_history(self, hist_file): """Initialize history using history related attributes This function can determine whether history is saved in the prior text-based @@ -3462,43 +3461,46 @@ class Cmd(cmd.Cmd): """ self.history = History() # with no persistent history, nothing else in this method is relevant - if not self.persistent_history_file: + if not hist_file: + self.persistent_history_file = hist_file return - self.persistent_history_file = os.path.expanduser(self.persistent_history_file) + hist_file = os.path.expanduser(hist_file) # first we try and unpickle the history file history = History() try: - with open(self.persistent_history_file, 'rb') as fobj: + with open(hist_file, 'rb') as fobj: history = pickle.load(fobj) except (FileNotFoundError, KeyError, EOFError): pass except IsADirectoryError: msg = "persistent history file '{}' is a directory" - self.perror(msg.format(self.persistent_history_file)) + self.perror(msg.format(hist_file)) + return except OSError as ex: msg = "can not read persistent history file '{}': {}" - self.perror(msg.format(self.persistent_history_file, ex), traceback_war=False) + self.perror(msg.format(hist_file, ex), traceback_war=False) + return self.history = history - - # trim history to length and ensure it's writable - # history.truncate(maxlen) - # try: - # # open with append so it doesn't truncate the file - # with open(histfile, 'ab') as fobj: - # self.persistent_history_file = histfile - # except OSError as ex: - # msg = "can not write persistent history file '{}': {}" - # self.perror(msg.format(histfile, ex), traceback_war=False) + self.persistent_history_file = hist_file + + # populate readline history + if rl_type != RlType.NONE: + last = None + for item in history: + # readline only adds a single entry for multiple sequential identical commands + # so we emulate that behavior here + if item.statement.raw != last: + readline.add_history(item.statement.raw) + last = item.statement.raw # register a function to write history at save # if the history file is in plain text format from 0.9.12 or lower # this will fail, and the history in the plain text file will be lost - - #import atexit - #atexit.register(self._persist_history_on_exit) + import atexit + atexit.register(self._persist_history_on_exit) def _persist_history_on_exit(self): """write history out to the history file""" @@ -3509,7 +3511,6 @@ class Cmd(cmd.Cmd): try: with open(self.persistent_history_file, 'wb') as fobj: pickle.dump(self.history, fobj) - except OSError as ex: msg = "can not write persistent history file '{}': {}" self.perror(msg.format(self.persistent_history_file, ex), traceback_war=False) |