summaryrefslogtreecommitdiff
path: root/alot/ui.py
diff options
context:
space:
mode:
authorThomas Nixon <tom@tomn.co.uk>2018-02-19 22:31:38 +0000
committerThomas Nixon <tom@tomn.co.uk>2018-02-19 23:15:29 +0000
commit01016563fccf31ddee6e5b8b4b13c065149dd814 (patch)
treeb40d9ec08e364ea7b21185b2379efa42e1b6a161 /alot/ui.py
parent021735bdcba6763a5ed42cfce305e0ca33714db4 (diff)
Move screen stop/start logic into context manager.
As well as reducing duplication and adding screen size detection to :pipeto and :pyshell, this ensures that the screen is always restarted, resulting in cleaner error handling if an error occurs while the screen is stopped.
Diffstat (limited to 'alot/ui.py')
-rw-r--r--alot/ui.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/alot/ui.py b/alot/ui.py
index 52dc3656..a8da57d2 100644
--- a/alot/ui.py
+++ b/alot/ui.py
@@ -7,6 +7,7 @@ import logging
import os
import signal
import codecs
+import contextlib
from twisted.internet import reactor, defer, task
import urwid
@@ -363,6 +364,24 @@ class UI(object):
exit_msg = 'Could not stop reactor: {}.'.format(e)
logging.error('%s\nShutting down anyway..', exit_msg)
+ @contextlib.contextmanager
+ def paused(self):
+ """
+ context manager that pauses the UI to allow running external commands.
+
+ If an exception occurs, the UI will be started before the exception is
+ re-raised.
+ """
+ self.mainloop.stop()
+ try:
+ yield
+ finally:
+ self.mainloop.start()
+
+ # make sure urwid renders its canvas at the correct size
+ self.mainloop.screen_size = None
+ self.mainloop.draw_screen()
+
def buffer_open(self, buf):
"""register and focus new :class:`~alot.buffers.Buffer`."""