From 752a283e0057aa32233a1de47a4f4faabb389065 Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Tue, 6 Feb 2018 15:13:08 +0100 Subject: [PATCH] kete: persistent history We write down the history immediatelly after receiving a new command, just in case something bad happens. Fixes #54 --- tools/tuhi_kete.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tools/tuhi_kete.py b/tools/tuhi_kete.py index b294d68..5030134 100755 --- a/tools/tuhi_kete.py +++ b/tools/tuhi_kete.py @@ -24,6 +24,10 @@ import readline import threading import time import svgwrite +import xdg.BaseDirectory + + +CONFIG_PATH = os.path.join(xdg.BaseDirectory.xdg_data_home, 'tuhi-kete') class ColorFormatter(logging.Formatter): @@ -562,6 +566,20 @@ class TuhiKeteShell(cmd.Cmd): # patching get_names to hide some functions we do not want in the help self.get_names = self._filtered_get_names + try: + os.mkdir(CONFIG_PATH) + except FileExistsError: + pass + + self._history_file = os.path.join(CONFIG_PATH, 'history') + + try: + readline.read_history_file(self._history_file) + except FileNotFoundError: + readline.write_history_file(self._history_file) + + readline.set_history_length(100) + Gio.bus_watch_name(Gio.BusType.SESSION, TUHI_DBUS_NAME, Gio.BusNameWatcherFlags.NONE, @@ -622,6 +640,8 @@ class TuhiKeteShell(cmd.Cmd): if self._manager is None and line not in ['EOF', 'exit', 'help']: print('Not connected to the Tuhi daemon') return '' + + readline.write_history_file(self._history_file) return line def postcmd(self, stop, line):