Added simple_select() to wk.std.Menu()
* Allows user to select one entry from available entries
This commit is contained in:
parent
68000272ea
commit
f1a1a158ba
1 changed files with 42 additions and 0 deletions
|
|
@ -162,6 +162,30 @@ class Menu():
|
||||||
# Done
|
# Done
|
||||||
return valid_answers
|
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):
|
def _update(self, single_selection=True):
|
||||||
"""Update menu items in preparation for printing to screen."""
|
"""Update menu items in preparation for printing to screen."""
|
||||||
index = 0
|
index = 0
|
||||||
|
|
@ -215,6 +239,24 @@ class Menu():
|
||||||
details['Enabled'] = details.get('Enabled', False)
|
details['Enabled'] = details.get('Enabled', False)
|
||||||
self.toggles[name] = details
|
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
|
# Functions
|
||||||
def abort(prompt='Aborted.', show_prompt=True, return_code=1):
|
def abort(prompt='Aborted.', show_prompt=True, return_code=1):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue