cmd2 icon indicating copy to clipboard operation
cmd2 copied to clipboard

Persistent history file not saved when application is killed

Open shimomut opened this issue 1 year ago • 2 comments

When killing the Cmd2 application (e.g. by closing the terminal window), persistent history file is not saved.

By using persistent_history_file argument for Cmd.init(), and hitting quit command, I can see the persistent history command is updated as expected.

Environment : Ubuntu 22 on WLS2

shimomut avatar Feb 13 '24 19:02 shimomut

I checked the behavior of bash and ipython. They save history persistently even when I close terminal window.

shimomut avatar Mar 12 '24 03:03 shimomut

As a hacky solution, I was able to use _persist_history() private method in postcmd_hook to save command history immediately after all command executions.

    def __init__(self):
            :
        self.register_postcmd_hook(self.on_command_executed)
            :


    def on_command_executed(self, data: cmd2.plugin.PostcommandData) -> cmd2.plugin.PostcommandData:
        
        # Save command history immediately
        self._persist_history()

        return data

shimomut avatar May 15 '24 02:05 shimomut