From 48f3b534ad804e8c954be0b2360f70dc03cb2a84 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Tue, 6 May 2025 22:42:03 -0700 Subject: store search state in menu stack --- main.lua | 82 ++++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 49 insertions(+), 33 deletions(-) diff --git a/main.lua b/main.lua index e844ddf..e768539 100644 --- a/main.lua +++ b/main.lua @@ -24,9 +24,6 @@ local streams = {} local depth = 0 local menus = {} -local search_text -local search_text_cursor -local search_active = false local key_bindings = {} local function load_json_file(path) @@ -64,7 +61,7 @@ local function update_osd() if depth > 1 then for i = 2, depth do local col = '999999' - if search_active and i == depth then + if menus[i].search_active then col = 'FF00' end out[#out+1] = '{\\c&H' .. col .. '&} ยป [' .. @@ -83,7 +80,7 @@ local function update_osd() str = '[' .. str .. ']' end - if i == menu.cursor_pos and not search_active then + if i == menu.cursor_pos and not menu.search_active then str = '{\\c&HFF00&}* ' .. str .. '{\\c}' elseif opt.type == 'category' then str = '{\\c&H99DDFF&}' .. str .. '{\\c}' @@ -222,8 +219,11 @@ local function unbind_keys() end local function split_search_text() - return search_text:sub(1, math.max(0, search_text_cursor - 1)), - search_text:sub(search_text_cursor) + local menu = menus[depth] + + return menu.search_text:sub(1, + math.max(0, menu.search_text_cursor_pos - 1)), + menu.search_text:sub(menu.search_text_cursor_pos) end local function search_update_osd() @@ -232,13 +232,15 @@ local function search_update_osd() update_osd() end -local function search_candidates(options, arr) +local function search_menu_options(options, arr) + local menu = menus[depth] + local arr = arr or {} for _, v in ipairs(options) do arr[#arr+1] = v if v.type == 'category' then - search_candidates( + search_menu_options( category_menu_options(v.category_id), arr) end end @@ -246,64 +248,76 @@ local function search_candidates(options, arr) end local function search_update() + local menu = menus[depth] + local matches = {} - for _, v in ipairs(menus[depth].candidates) do - if string.find(v.name, search_text, 0, true) then + for _, v in ipairs(menu.search_options) do + if string.find(v.name, menu.search_text, 0, true) then matches[#matches+1] = v end end - menus[depth].options = matches + menu.options = matches search_update_osd() end local function search_input(event) + local menu = menus[depth] + if event.event ~= 'down' and event.event ~= 'repeat' then return end local bc, ac = split_search_text() - search_text = bc .. event.key_text .. ac - search_text_cursor = search_text_cursor + 1 + menu.search_text = bc .. event.key_text .. ac + menu.search_text_cursor_pos = menu.search_text_cursor_pos + 1 search_update() end local function search_backspace() - if search_text_cursor == 1 then + local menu = menus[depth] + + if menu.search_text_cursor_pos == 1 then return end local bc, ac = split_search_text() - search_text = bc:sub(1, -2) .. ac - search_text_cursor = search_text_cursor - 1 + menu.search_text = bc:sub(1, -2) .. ac + menu.search_text_cursor_pos = menu.search_text_cursor_pos - 1 search_update() end local function search_delete() - if search_text_cursor == #search_text + 1 then + local menu = menus[depth] + + if menu.search_text_cursor_pos == #menu.search_text + 1 then return end local bc, ac = split_search_text() - search_text = bc .. ac:sub(2) + menu.search_text = bc .. ac:sub(2) search_update() end local function search_cursor_left() - if search_text_cursor == 1 then + local menu = menus[depth] + + if menu.search_text_cursor_pos == 1 then return end - search_text_cursor = search_text_cursor - 1 + menu.search_text_cursor_pos = menu.search_text_cursor_pos - 1 search_update_osd() end local function search_cursor_right() - if search_text_cursor == #search_text + 1 then + local menu = menus[depth] + + if menu.search_text_cursor_pos == #menu.search_text + 1 then return end - search_text_cursor = search_text_cursor + 1 + menu.search_text_cursor_pos = menu.search_text_cursor_pos + 1 search_update_osd() end @@ -311,20 +325,20 @@ local bind_search_keys local bind_menu_keys local function search_start() - search_active = true - local menu = menus[depth] if menu.type == 'search' then - search_text_cursor = #search_text + 1 + menu.search_active = true + menu.search_text_cursor_pos = #menu.search_text + 1 menu.cursor_pos = 1 menu.page_pos = 1 search_update_osd() else - search_text = '' - search_text_cursor = 1 push_menu({ type='search', - candidates=search_candidates(menu.options), + search_active=true, + search_options=search_menu_options(menu.options), + search_text='', + search_text_cursor_pos=1, }) search_update() end @@ -333,14 +347,16 @@ local function search_start() end local function search_finish() - search_active = false - menus[depth].title = 'Search results: ' .. search_text + local menu = menus[depth] + + menu.search_active = false + menu.title = 'Search results: ' .. menu.search_text update_osd() bind_menu_keys() end local function search_cancel() - search_active = false + menus[depth].search_active = false depth = depth - 1 update_osd() bind_menu_keys() @@ -382,7 +398,7 @@ local function toggle_menu() if osd.hidden then unbind_keys() - elseif search_active then + elseif menus[depth].search_active then bind_search_keys() else bind_menu_keys() -- cgit v1.2.3-70-g09d2