implemented short menu system too

This commit is contained in:
Luxferre
2026-01-04 14:41:54 +02:00
parent d2009dd65c
commit 7eb45a4f0b
3 changed files with 32 additions and 3 deletions
+6 -2
View File
@@ -56,9 +56,13 @@ Exported functions:
### `deck.menu` ### `deck.menu`
A library for easy menu systems for T-DeckARD programs. Allows displaying up to 10 menu items with the ability to select them with a single digit or a key on the `QWERTYUIOP` keyboard row. A library for easy menu systems for T-DeckARD programs.
Exports the only function, `menu(choices)`, which accepts a list of **tuples**, each in the format `(id, label)`. The `label` field is what's displayed on the screen, the `id` field is the string that gets returned upon selection. If the user selects `c`, the operation is canceled and an empty `id` string is returned. Exported functions:
- `confirm(msg, yes='y', no='n', alt_prompt=None)`: prompt for yes/no confirmation, using a standard prompt in `msg` and the `yes`/`no` message in the corresponding parameters, or a completely alternative prompt in `alt_prompt`.Returns True or False as the result.
- `shortmenu(choices, suffix=': ')`: display several menu items from the `choices` list, automatically labeling the first fitting letter from the item as the selection shortcut. Returns a tuple (`result`, `label`), or (``,``) in case the selection doesn't match.
- `menu(choices)`: display up to 10 menu items with the ability to select them with a single digit or a key on the `QWERTYUIOP` keyboard row. Accepts a list of **tuples**, each in the format `(id, label)`. The `label` field is what's displayed on the screen, the `id` field is the string that gets returned upon selection. If the user selects `c`, the operation is canceled and an empty `id` string is returned.
### `deck.time` ### `deck.time`
+25
View File
@@ -64,3 +64,28 @@ def menu(choices, max_size = 10):
else: else:
print('Invalid choice!') print('Invalid choice!')
return menu(choices) return menu(choices)
def shortmenu(choices, suffix=': '):
letters = {}
cmap = []
for c in choices:
l = '?'
cl = len(c)
i = 0
while i < cl:
l = c[i].lower()
if l not in letters:
break
i += 1
if i == 0:
letters[l] = (c, l + ')' + c[1:])
cmap.append(letters[l][1])
elif i < cl:
letters[l] = (c, c[0:i] + '(' + l + ')' + c[i+1:])
cmap.append(letters[l][1])
prompt = ' '.join(cmap) + suffix
res = input(prompt).strip()[0].lower()
if res in letters:
return (res, letters[res][0])
else:
return ('', '')
+1 -1
View File
@@ -7,7 +7,7 @@ from deck.fs import unlock_rootfs, lock_rootfs, mount_sd, umount_sd, env, rnd
from deck.fs import du, df, sep, pwd, ls, md, cd, rd, rm, mv, cp, sync from deck.fs import du, df, sep, pwd, ls, md, cd, rd, rm, mv, cp, sync
from deck.input import input, input_multi from deck.input import input, input_multi
from deck.pager import print_paged, term_size from deck.pager import print_paged, term_size
from deck.menu import menu from deck.menu import menu, shortmenu, confirm
from deck.time import * from deck.time import *
from deck.hwinfo import board_id, battery_v, cpu_f, REAL_HW from deck.hwinfo import board_id, battery_v, cpu_f, REAL_HW
from deck.http import wget, wput, http_fetch, http_set_ua from deck.http import wget, wput, http_fetch, http_set_ua