From 925aa695b437325d48abc484f2769c6787ebf3f5 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Sat, 31 Jan 2026 02:40:00 -0800 Subject: use menu metatable for saving/restoring search state --- rt.lua | 16 ++++------------ state.lua | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/rt.lua b/rt.lua index 9193314..502cf83 100644 --- a/rt.lua +++ b/rt.lua @@ -167,8 +167,7 @@ local function save_favourites() end local function set_cursor(pos, opts) - local moved = state:menu():set_cursor(pos, osd:menu_lines(state), opts) - if moved then + if state:menu():set_cursor(pos, osd:menu_lines(state), opts) then osd:dirty() end end @@ -609,11 +608,7 @@ function rt.start_search() ' (/)' if menu.type == 'search' then - -- resuming search, save previous state - menu.prev_search_text = menu.search_text - menu.prev_cursor = menu.cursor - menu.prev_view_top = menu.view_top - + menu:save_checkpoint() menu.title = title menu.search_active = true menu:set_search_cursor(#menu.search_text + 1) @@ -643,11 +638,8 @@ end function rt.cancel_search() local menu = state:menu() - -- cancelling resumed search restores previous state - if menu.prev_search_text then - menu:set_search_text(menu.prev_search_text) - menu.cursor = menu.prev_cursor - menu.view_top = menu.prev_view_top + if menu.checkpoint then + menu:restore_checkpoint() rt.end_search() return end diff --git a/state.lua b/state.lua index 326fa28..3abb892 100644 --- a/state.lua +++ b/state.lua @@ -82,6 +82,21 @@ function mt:insert_favourite_before_next_in_menu(id) self:add_favourite(id) end +function menu_mt:save_checkpoint() + self.checkpoint = { + search_text = self.search_text, + cursor = self.cursor, + view_top = self.view_top, + } +end + +function menu_mt:restore_checkpoint() + local t = self.checkpoint + self:set_search_text(t.search_text) + self.cursor = t.cursor + self.view_top = t.view_top +end + function menu_mt:set_cursor(pos, lines, opts) local pos = math.max(1, math.min(pos, #self.options)) local top = self.view_top -- cgit v1.2.3-70-g09d2