Added simple_select() to wk.std.Menu()

* Allows user to select one entry from available entries
This commit is contained in:
2Shirt 2019-09-13 16:16:33 -07:00
parent 68000272ea
commit f1a1a158ba
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -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):