From 36bcd889defcb61797a3b3f79d43509b44169a13 Mon Sep 17 00:00:00 2001 From: vdbewout Date: Fri, 1 Nov 2024 11:05:39 +0000 Subject: [PATCH] feat(menu): simplify calculations for selection When using a negative count for `Menu:select_next` don't go into negative indices instead rollover to the last entry. Co-authored-by: max397574 <81827001+max397574@users.noreply.github.com> --- lua/care/menu/init.lua | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/lua/care/menu/init.lua b/lua/care/menu/init.lua index 26624ba..2ca44c7 100644 --- a/lua/care/menu/init.lua +++ b/lua/care/menu/init.lua @@ -233,19 +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 + 1) + 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.index = (self.index - count) % (#self.entries + 1) + self:select(-1) end