]> git.ipfire.org Git - people/ms/bricklayer.git/commitdiff
tui: Setup screen with some helptext
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 5 May 2021 15:29:30 +0000 (15:29 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 5 May 2021 15:29:30 +0000 (15:29 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/tui/__init__.py

index 20ead6cfc07b85fd0e9bdcefa0b908c334a3dd01..6926a4a325295cd9b12c3ca8e63e81589d128461 100644 (file)
@@ -19,6 +19,9 @@
 ###############################################################################
 
 import logging
+import snack
+
+from ..i18n import _
 
 # Setup logging
 log = logging.getLogger("bricklayer.tui")
@@ -27,10 +30,63 @@ class Tui(object):
        def __init__(self, bricklayer):
                self.bricklayer = bricklayer
 
+               # Placeholder for screen
+               self.screen = None
+
        # Make this class usable as context
 
        def __enter__(self):
+               log.debug("Entering TUI context")
+
+               # Setup the screen
+               self._setup_screen()
+
                return self
 
        def __exit__(self, type, value, traceback):
-               pass
+               log.debug("Leaving TUI context")
+
+               # Wipe the screen
+               self._finish_screen()
+
+       def refresh(self):
+               """
+                       Refreshes what is written on the screen
+               """
+               if self.screen:
+                       self.screen.refresh()
+
+       def _setup_screen(self):
+               """
+                       Sets up the screen
+               """
+               self.screen = snack.SnackScreen()
+
+               # Setup helpline
+               self.push_helpline(
+                       _("<Tab>/<Alt-Tab> between elements | <Space> selects | <F12> next screen")
+               )
+
+               # Refresh the screen
+               self.refresh()
+
+       def _finish_screen(self):
+               """
+                       Cleanup screen
+               """
+               if self.screen:
+                       self.screen.finish()
+                       self.screen = None
+
+       def push_helpline(self, helpline):
+               """
+                       Sets the helpline, but centers it first
+               """
+               if not self.screen:
+                       raise RuntimeError()
+
+               # Center the string
+               if self.screen.width:
+                       helpline = helpline.center(self.screen.width)
+
+               self.screen.pushHelpLine(helpline)