diff --git a/libs/ui/CountersSelector.lua b/libs/ui/CountersSelector.lua index e88d77d..e3b016f 100644 --- a/libs/ui/CountersSelector.lua +++ b/libs/ui/CountersSelector.lua @@ -79,6 +79,17 @@ local function takeN(t, n) return result end +local function resetAllCounters(countersMap) + local result = {} + for k, counterPayload in pairs(countersMap) do + result[k] = { + name = counterPayload.name, + count = 0 + } + end + return result +end + local function renderCountersMap(win, countersMap, selectedCounter) win.clear() win.setCursorPos(1, 1) @@ -86,15 +97,6 @@ local function renderCountersMap(win, countersMap, selectedCounter) -- local nbCounters = utils.sizeof(countersMap) -- local totalCount = getTotalCount(countersMap) - -- if titleFn ~= noTitleFn then - -- withColor(win, colors.white, colors.green, function() - -- win.clearLine() - -- win.write(titleFn(countersMap, selectedCounter)) - -- end) - - -- -- topMargin = TITLE_MARGIN - -- end - local selectedPage = math.floor((selectedCounter - 1) / height) + 1 -- local totalPages = (nbCounters % height) + 1 @@ -129,6 +131,36 @@ local function renderTitle(win, countersMap, selectedCounter, titleFn) end) end +local function incrementCounter(countersMap, selectedCounter) + local counterPayload = countersMap[selectedCounter] + + if counterPayload and counterPayload.count then + countersMap[selectedCounter] = { + count = counterPayload.count + 1, + name = counterPayload.name + } + + return true + end + + return false +end + +local function decrementCounter(countersMap, selectedCounter) + local counterPayload = countersMap[selectedCounter] + + if counterPayload and counterPayload.count and counterPayload.count > 0 then + countersMap[selectedCounter] = { + count = counterPayload.count - 1, + name = counterPayload.name + } + + return true + end + + return false +end + local function CountersSelector(initialCountersMap, config) local countersMap = utils.shallowClone(initialCountersMap) local counterMax = config.counterMax @@ -169,25 +201,19 @@ local function CountersSelector(initialCountersMap, config) elseif keyPressed == keys.down then selectedCounter = math.min(nbCounters, selectedCounter + 1) elseif keyPressed == keys.left and globalCounter > 0 then - local counterPayload = countersMap[selectedCounter] - if counterPayload and counterPayload.count and counterPayload.count > 0 then - countersMap[selectedCounter] = { - count = counterPayload.count - 1, - name = counterPayload.name - } + if decrementCounter(countersMap, selectedCounter) then globalCounter = globalCounter - 1 end elseif keyPressed == keys.right and globalCounter < counterMax then - local counterPayload = countersMap[selectedCounter] - if counterPayload and counterPayload.count then - countersMap[selectedCounter] = { - count = counterPayload.count + 1, - name = counterPayload.name - } + if incrementCounter(countersMap, selectedCounter) then globalCounter = globalCounter + 1 end + elseif keyPressed == keys.r then + countersMap = resetAllCounters(countersMap) elseif keyPressed == keys.enter then shouldContinue = false + elseif keypRessed == keys.q then + return nil end end