summaryrefslogtreecommitdiff
path: root/cmd2.py
diff options
context:
space:
mode:
authorAlberto Sartori <alberto.sartori.as@gmail.com>2018-03-13 16:36:08 +0100
committerAlberto Sartori <alberto.sartori.as@gmail.com>2018-03-14 22:29:58 +0100
commitff1cad30e526725ed674975b8c53eca69ff1a3e9 (patch)
tree6757d9ba91adc6314266d1b91d8c3431c14a9575 /cmd2.py
parent9068d5f7b32a3c628861a60ca3b19fa1304f37c9 (diff)
downloadcmd2-git-ff1cad30e526725ed674975b8c53eca69ff1a3e9.tar.gz
allow submenu history preservation
Diffstat (limited to 'cmd2.py')
-rwxr-xr-xcmd2.py23
1 files changed, 20 insertions, 3 deletions
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_<submenu> and will perform the complete commands of the submenu.