]> git.ipfire.org Git - people/ms/bricklayer.git/commitdiff
tui: Refactor progress window
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 May 2021 16:41:02 +0000 (16:41 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 May 2021 16:41:02 +0000 (16:41 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/tui.py

index 0ac99ac59d9903ab20a5b8f04920605ca4a3ee22..9a43f051829fb770163e88df99480ed234a43614 100644 (file)
@@ -144,8 +144,9 @@ class Tui(object):
 
                return self.message(title, text, buttons=buttons, width=width)
 
-       def progress(self, *args, **kwargs):
-               return ProgressWindow(self, *args, **kwargs)
+       def progress(self, title, text, max_value=None, height=None, width=60, help=None):
+               return ProgressWindow(self, title, text, max_value=max_value,
+                       height=height, width=width, help=help)
 
        def select(self, title, text, items, default=None, buttons=None, height=None,
                        width=40, help=None):
@@ -308,29 +309,37 @@ class ListboxChoiceWindow(ButtonsWindow):
                return self.listbox.current()
 
 
-class ProgressWindow(object):
-       def __init__(self, tui, title, text, max_value=0, width=60, help=None):
-               self.tui = tui
+class ProgressWindow(Window):
+       """
+               Shows a notification (optionally with a progress bar) about what is going on
+       """
+       def __init__(self, tui, title, text, max_value=0, height=None, width=None, help=None):
+               Window.__init__(self, tui, title, text, height=height, width=width, help=help)
 
-               # Compose the window
-               textbox = snack.TextboxReflowed(width, text)
+               self.max_value = max_value
 
-               # Add the progressbar
-               if max_value:
-                       scale = snack.Scale(width, total=max_value)
-                       self.update_callback = scale.set
-               else:
-                       scale = None
+               # Make progressbar
+               self.scale = snack.Scale(self.width, self.max_value)
 
+       def _make_window(self):
                # Create the grid
-               self.grid = snack.GridFormHelp(tui.screen, title, help, 1, 3)
-               self.grid.add(textbox, 0, 0, padding=(0, 0, 0, 1))
-               if scale:
-                       self.grid.add(scale, 0, 1, growx=1)
+               grid = snack.GridFormHelp(self.tui.screen, self.title, self.help, 1, 2)
+
+               # Add the textbox
+               textbox = snack.TextboxReflowed(self.width, self.text)
+               grid.add(textbox, 0, 0)
+
+               # Optionally add the progress bar
+               if self.max_value:
+                       grid.add(self.scale, 0, 1, padding=(0, 1, 0, 0))
+
+               return grid
 
        def __enter__(self):
                # Render the window
-               self.grid.draw()
+               window = self._make_window()
+               window.draw()
+
                self.tui.refresh()
 
        def __exit__(self, type, value, traceback):
@@ -340,5 +349,6 @@ class ProgressWindow(object):
                """
                        Updates the progressbar value
                """
-               self.update_callback(value)
-               self.tui.refresh()
+               if self.max_value:
+                       self.scale.set(value)
+                       self.tui.refresh()