From f1a1a158ba2aeff278bef047219f96854a6f7855 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Fri, 13 Sep 2019 16:16:33 -0700 Subject: [PATCH] Added simple_select() to wk.std.Menu() * Allows user to select one entry from available entries --- scripts/wk/std.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/scripts/wk/std.py b/scripts/wk/std.py index 09fadb28..7494c3e5 100644 --- a/scripts/wk/std.py +++ b/scripts/wk/std.py @@ -162,6 +162,30 @@ class Menu(): # Done return valid_answers + def _resolve_selection(self, selection): + """Get menu item based on user selection, returns tuple.""" + resolved_selection = None + if selection.isnumeric(): + # Enumerate over numbered entries + entries = [ + *self.sets.items(), + *self.toggles.items(), + *self.options.items(), + ] + for _i, details in enumerate(entries): + if str(_i+1) == selection: + resolved_selection = (details) + break + else: + # Just check actions + for action, details in self.actions.items(): + if action.lower().startswith(selection.lower()): + resolved_selection = (action, details) + break + + # Done + return resolved_selection + def _update(self, single_selection=True): """Update menu items in preparation for printing to screen.""" index = 0 @@ -215,6 +239,24 @@ class Menu(): details['Enabled'] = details.get('Enabled', False) self.toggles[name] = details + def simple_select(self, prompt='Please make a selection...'): + """Display menu and make a single selection, returns str.""" + self._update() + menu_text = self._generate_menu_text() + valid_answers = self._get_valid_answers() + + # Loop until valid answer is given + while True: + clear_screen() + print(menu_text) + sleep(0.01) + answer = input_text(prompt).strip() + if answer.upper() in valid_answers: + break + + # Done + return self._resolve_selection(answer) + # Functions def abort(prompt='Aborted.', show_prompt=True, return_code=1):