From 37585452259b2c5ae32c3e50e97aa0aca545f52e Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Fri, 30 Jan 2026 14:17:03 -0800 Subject: update sort and refresh options logic --- state.lua | 57 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 11 deletions(-) (limited to 'state.lua') diff --git a/state.lua b/state.lua index a88fe41..326fa28 100644 --- a/state.lua +++ b/state.lua @@ -100,6 +100,8 @@ function menu_mt:set_cursor(pos, lines, opts) top = pos - math.floor((lines - 1) / 2) elseif opts and opts.keep_offset then top = top + pos - self.cursor + elseif opts and opts.view_top then + top = opts.view_top elseif margin > 0 then margin = math.max(0, math.min( margin, @@ -120,25 +122,58 @@ function menu_mt:set_cursor(pos, lines, opts) return true end -function menu_mt:set_sort(bool, f) - if not self.sorted == not bool then - return - end +function menu_mt:options_key() + return self.type == 'search' and 'search_options' or 'options' +end - local key = self.type == 'search' and 'search_options' or 'options' - if bool then - self['orig_' .. key] = self[key] - self[key] = util.copy_table(self[key]) +function menu_mt:set_sort(f) + local key = self:options_key() + local orig_key = 'orig_' .. key + + if f then + if not self[orig_key] then + self[orig_key] = self[key] + end + self[key] = util.copy_table(self[orig_key]) f(self[key]) + elseif self.sort_f then + self[key] = self[orig_key] + self[orig_key] = nil + end + + self.sort_f = f + + if self.type == 'search' then + self:update_search_matches() + end +end + +function menu_mt:is_sorted() + return self.sort_f ~= nil +end + +function menu_mt:master_options() + local key = self:options_key() + return self.sort_f and self['orig_' .. key] or self[key] +end + +function menu_mt:set_options(options) + local key = self:options_key() + if self.sort_f then + self['orig_' .. key] = options + self[key] = util.copy_table(options) + self.sort_f(self[key]) else - self[key] = self['orig_' .. key] - self['orig_' .. key] = nil + self[key] = options end if self.type == 'search' then self:update_search_matches() end - self.sorted = bool + + -- previous position could have become invalid. reset to top and let + -- caller deal with remembering position if it is needed. + self:set_cursor(1) end function menu_mt:set_search_cursor(pos) -- cgit v1.2.3-70-g09d2