Skip to content

Commit 35da4bc

Browse files
committed
fw/apps/prf/mfg_menu: enumerate entries
Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
1 parent f9e71e1 commit 35da4bc

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/fw/apps/prf_apps/mfg_menu_app.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,19 @@ static void prv_extras_window_load(Window *window) {
235235
SimpleMenuItem *menu_items = app_malloc(sizeof(extras_menu_items));
236236
memcpy(menu_items, extras_menu_items, sizeof(extras_menu_items));
237237

238+
size_t num_items = ARRAY_LENGTH(extras_menu_items);
239+
240+
// Add index numbers to each menu entry
241+
for (size_t i = 0; i < num_items; i++) {
242+
const char *original_title = menu_items[i].title;
243+
size_t new_title_len = snprintf(NULL, 0, "%zu. %s", i + 1, original_title) + 1;
244+
char *new_title = app_malloc(new_title_len);
245+
snprintf(new_title, new_title_len, "%zu. %s", i + 1, original_title);
246+
menu_items[i].title = new_title;
247+
}
248+
238249
data->menu_section = (SimpleMenuSection) {
239-
.num_items = ARRAY_LENGTH(extras_menu_items),
250+
.num_items = num_items,
240251
.items = menu_items
241252
};
242253

@@ -367,6 +378,8 @@ static size_t prv_create_menu_items(SimpleMenuItem** out_menu_items) {
367378
*out_menu_items = app_malloc(sizeof(s_menu_items));
368379
memcpy(*out_menu_items, s_menu_items, sizeof(s_menu_items));
369380

381+
size_t num_items = ARRAY_LENGTH(s_menu_items);
382+
370383
// Now we're going to modify the first two elements in the menu to include data available only
371384
// at runtime. If it was available at compile time we could have just shoved it in the
372385
// s_menu_items array but it's not. Note that we allocate a few buffers here that we never
@@ -387,9 +400,20 @@ static size_t prv_create_menu_items(SimpleMenuItem** out_menu_items) {
387400

388401
(*out_menu_items)[1].subtitle = device_serial;
389402

403+
// Add index numbers to each menu entry
404+
for (size_t i = 0; i < num_items; i++) {
405+
const char *original_title = (*out_menu_items)[i].title;
406+
// Allocate buffer for "N. " + original title + null terminator
407+
// Max index is 99 (2 digits), so we need 4 chars for "99. " + strlen + 1
408+
size_t new_title_len = snprintf(NULL, 0, "%zu. %s", i + 1, original_title) + 1;
409+
char *new_title = app_malloc(new_title_len);
410+
snprintf(new_title, new_title_len, "%zu. %s", i + 1, original_title);
411+
(*out_menu_items)[i].title = new_title;
412+
}
413+
390414
// We've now populated out_menu_items with the correct data. Return the number of items by
391415
// looking at the original list of menu items.
392-
return ARRAY_LENGTH(s_menu_items);
416+
return num_items;
393417
}
394418

395419
static void prv_window_load(Window *window) {

0 commit comments

Comments
 (0)