summaryrefslogtreecommitdiff
path: root/alot/ui.py
diff options
context:
space:
mode:
authorLucas Hoffmann <l-m-h@web.de>2016-07-15 00:25:59 +0200
committerLucas Hoffmann <l-m-h@web.de>2016-12-14 08:46:01 +0100
commit4b926b31f8d2d2b58dc1332442a14822d6518106 (patch)
treedd6a5a310244ff69b8f5c6f3409b53ecddfe0422 /alot/ui.py
parent809220c94c941e4b6910d4e75e83a899b311bd2e (diff)
Save and load command history across invocations
Initialize the command history with lines from ${XDG_CACHE_HOME:-~/.cache}/alot at startup. Write the current history to the file again during shutdown.
Diffstat (limited to 'alot/ui.py')
-rw-r--r--alot/ui.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/alot/ui.py b/alot/ui.py
index aa9d89c8..84871c18 100644
--- a/alot/ui.py
+++ b/alot/ui.py
@@ -2,7 +2,9 @@
# This file is released under the GNU GPL, version 3 or a later revision.
# For further details see the COPYING file
import logging
+import os
import signal
+
from twisted.internet import reactor, defer
import urwid
@@ -73,6 +75,12 @@ class UI(object):
signal.signal(signal.SIGINT, self.handle_signal)
signal.signal(signal.SIGUSR1, self.handle_signal)
+ # load command history
+ self._cache = os.path.join(
+ os.environ.get('XDG_CACHE_HOME', os.path.expanduser('~/.cache')),
+ 'alot', 'commandhistory')
+ self.commandprompthistory = self._load_history_from_file( self._cache)
+
# set up main loop
self.mainloop = urwid.MainLoop(self.root_widget,
handle_mouse=False,
@@ -664,3 +672,37 @@ class UI(object):
if isinstance(self.current_buffer, SearchBuffer):
self.current_buffer.rebuild()
self.update()
+
+ def cleanup(self):
+ """Do the final clean up before shutting down."""
+ self._save_history_to_file(self.commandprompthistory, self._cache)
+
+ @staticmethod
+ def _load_history_from_file(path):
+ """Load a history list from a file and split it into lines.
+
+ :param path: the path to the file that should be loaded
+ :type path: str
+ :returns: a list of history items (the lines of the file)
+ :rtype: list(str)
+
+ """
+ if os.path.exists(path):
+ return [line.rstrip('\n') for line in open(path).readlines()]
+
+ @staticmethod
+ def _save_history_to_file(history, path):
+ """Save a history list to a file for later loading (possibly in another
+ session).
+
+ :param history: the history list to save
+ :type history: list(str)
+ :param path: the path to the file where to save the history
+ :type path: str
+ :returns: None
+
+ """
+ directory = os.path.dirname(path)
+ if not os.path.exists(directory):
+ os.makedirs(directory)
+ open(path, 'w').write('\n'.join(history))