From: Michael Tremer Date: Sat, 8 May 2021 16:41:02 +0000 (+0000) Subject: tui: Refactor progress window X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=81d9567a4880f3360eaca89c01834839d40f7d61;p=people%2Fms%2Fbricklayer.git tui: Refactor progress window Signed-off-by: Michael Tremer --- diff --git a/src/python/tui.py b/src/python/tui.py index 0ac99ac..9a43f05 100644 --- a/src/python/tui.py +++ b/src/python/tui.py @@ -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()