From 6466e4915d783190a9766ad19dd6324820dc7abb Mon Sep 17 00:00:00 2001 From: vdbewout Date: Fri, 1 Nov 2024 11:05:39 +0000 Subject: [PATCH] feat(window): `Menu:select_next` negative count When using a negative count for `Menu:select_next` don't go into negative indices instead rollover to the last entry. --- lua/care/init.lua | 2 +- lua/care/menu/init.lua | 13 ++++--------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/lua/care/init.lua b/lua/care/init.lua index 3ba3bb5..7e72c3d 100644 --- a/lua/care/init.lua +++ b/lua/care/init.lua @@ -25,7 +25,7 @@ care.api = { care.core.menu:close() end, select_prev = function(count) - care.core.menu:select_prev(count) + care.core.menu:select_next(-(count or 1)) end, select_next = function(count) care.core.menu:select_next(count) diff --git a/lua/care/menu/init.lua b/lua/care/menu/init.lua index 26624ba..1293035 100644 --- a/lua/care/menu/init.lua +++ b/lua/care/menu/init.lua @@ -233,20 +233,15 @@ end function Menu:select_next(count) count = count or 1 - self.index = self.index + count - if self.index > #self.entries then - self.index = self.index - #self.entries - 1 - end + self.index = (self.index + count) % #self.entries + self:select(1) end function Menu:select_prev(count) count = count or 1 - self.index = self.index - count - if self.index < 0 then - self.index = #self.entries + self.index + 1 - end - self:select(-1) + + self:select_next(-count) end function Menu:open(entries, offset)