From ff1cad30e526725ed674975b8c53eca69ff1a3e9 Mon Sep 17 00:00:00 2001 From: Alberto Sartori Date: Tue, 13 Mar 2018 16:36:08 +0100 Subject: allow submenu history preservation --- cmd2.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'cmd2.py') diff --git a/cmd2.py b/cmd2.py index 023db442..4cf8cf01 100755 --- a/cmd2.py +++ b/cmd2.py @@ -832,8 +832,9 @@ class AddSubmenu(object): reformat_prompt="{super_prompt}>> {sub_prompt}", shared_attributes=None, require_predefined_shares=True, + create_subclass=False, preserve_shares=False, - create_subclass=False + persistent_history_file=None ): """Set up the class decorator @@ -863,6 +864,10 @@ class AddSubmenu(object): self.submenu = submenu self.command = command self.aliases = aliases + if persistent_history_file: + self.persistent_history_file = os.path.expanduser(persistent_history_file) + else: + self.persistent_history_file = None if reformat_prompt is not None and not isinstance(reformat_prompt, str): raise ValueError("reformat_prompt should be either a format string or None") @@ -909,6 +914,13 @@ class AddSubmenu(object): """ submenu = self.submenu original_attributes = self._get_original_attributes() + history = _pop_readline_history() + + if self.persistent_history_file: + try: + readline.read_history_file(self.persistent_history_file) + except FILE_NOT_FOUND_ERROR: + pass try: # copy over any shared attributes @@ -921,7 +933,6 @@ class AddSubmenu(object): ret = submenu.onecmd(line) submenu.postcmd(ret, line) else: - history = _pop_readline_history() if self.reformat_prompt is not None: prompt = submenu.prompt submenu.prompt = self.reformat_prompt.format( @@ -933,11 +944,17 @@ class AddSubmenu(object): if self.reformat_prompt is not None: # noinspection PyUnboundLocalVariable self.submenu.prompt = prompt - _push_readline_history(history) finally: # copy back original attributes self._copy_out_shared_attrs(parent_cmd, original_attributes) + # write submenu history + if self.persistent_history_file: + readline.write_history_file(self.persistent_history_file) + # reset main app history before exit + _push_readline_history(history) + + def complete_submenu(_self, text, line, begidx, endidx): """ This function will be bound to complete_ and will perform the complete commands of the submenu. -- cgit v1.2.1