Files
t-deckard/deck/menu.py
T

55 lines
1.2 KiB
Python

# A simple menu system for T-DeckARD
# Created by Luxferre in 2025, released into public domain
from deck.input import input
_idx_map = {
'1': 0, 'q': 0,
'2': 1, 'w': 1,
'3': 2, 'e': 2,
'4': 3, 'r': 3,
'5': 4, 't': 4,
'6': 5, 'y': 5,
'7': 6, 'u': 6,
'8': 7, 'i': 7,
'9': 8, 'o': 8,
'0': 9, 'p': 9
}
def menu(choices, max_size = 10):
"""
Presents a menu of choices and prompts for numeric or
alphabetical input
Accepts a list of (id, label) tuples (up to 10 items)
Returns the associated id of the selected menu items
"""
cnum = 1
for (cid, clbl) in choices:
try:
cletter = 'qwertyuiop'[cnum - 1]
print(f'[{cletter}] {cnum % 10}. {clbl}')
except:
print(f'{cnum % 10} {clbl}')
cnum += 1
if cnum > max_size:
break
sel = ''
while len(sel) < 1:
sel = input('Your choice (c to cancel): ').strip().lower()
sel = str(sel[0])
if sel == 'c':
print('Selection canceled')
return '' # empty label denotes canceled operation
elif sel in _idx_map:
idx = _idx_map[sel]
if idx < len(choices):
return choices[idx][0]
else:
print('Invalid choice!')
return menu(choices)
else:
print('Invalid choice!')
return menu(choices)