Skip to content

Commit

Permalink
feat(menu): simplify calculations for selection
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
vdbe and max397574 committed Nov 1, 2024
1 parent afeffde commit 36bcd88
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions lua/care/menu/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 36bcd88

Please sign in to comment.