diff --git a/libs/ui/CountersSelector.lua b/libs/ui/CountersSelector.lua index bf43615..ca0d292 100644 --- a/libs/ui/CountersSelector.lua +++ b/libs/ui/CountersSelector.lua @@ -25,7 +25,7 @@ local function getTotalCount(countersMap) return total end -local function omitN(t, n) +local function dropN(t, n) local result = {} for k,v in pairs(t) do @@ -39,7 +39,7 @@ local function omitN(t, n) return result end -local function pickN(t, n) +local function takeN(t, n) local result = {} for k,v in pairs(t) do @@ -59,7 +59,6 @@ local function renderCountersMap(countersMap, selectedCounter, titleFn) term.setCursorPos(1, 1) local _, height = term.getSize() -- local nbCounters = utils.sizeof(countersMap) - local selectedCounterKey = countersMap[selectedCounter] -- local totalCount = getTotalCount(countersMap) local topMargin = 0 @@ -77,14 +76,14 @@ local function renderCountersMap(countersMap, selectedCounter, titleFn) -- local totalPages = (nbCounters % availableHeight) + 1 local nbElementsToOmit = (selectedPage - 1) * availableHeight - local displayedCounters = pickN(omitN(countersMap, nbElementsToOmit), availableHeight) + local displayedCounters = takeN(dropN(countersMap, nbElementsToOmit), availableHeight) local cursorYIndex = 1 + topMargin for k,v in pairs(displayedCounters) do - term.clearLine() term.setCursorPos(1, cursorYIndex) + term.clearLine() - if k == selectedCounterKey then + if k == selectedCounter then term.setBackgroundColor(colors.white) term.setTextColor(colors.black) else @@ -92,7 +91,7 @@ local function renderCountersMap(countersMap, selectedCounter, titleFn) term.setTextColor(colors.white) end - term.write(tostring(k) .. ' ' .. tostring(v)) + term.write(tostring(v.name) .. ' ' .. tostring(v.count)) cursorYIndex = cursorYIndex + 1 end end @@ -124,7 +123,6 @@ local function CountersSelector(initialCountersMap, config) local shouldContinue = true while shouldContinue do renderCountersMap(countersMap, selectedCounter, titleFn) - local _, keyPressed, isHeld = os.pullEvent('key') if keyPressed == keys.up then @@ -132,15 +130,21 @@ 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 currentCount = countersMap[selectedCounter] - if currentCount and currentCount > 0 then - countersMap[selectedCounter] = currentCount - 1 + local counterPayload = countersMap[selectedCounter] + if counterPayload and counterPayload.count and counterPayload.count > 0 then + countersMap[selectedCounter] = { + count = counterPayload.count - 1, + name = counterPayload.name + } globalCounter = globalCounter - 1 end elseif keyPressed == keys.right and globalCounter < counterMax then - local currentCount = countersMap[selectedCounter] - if currentCount then - countersMap[selectedCounter] = currentCount + 1 + local counterPayload = countersMap[selectedCounter] + if counterPayload and counterPayload.count then + countersMap[selectedCounter] = { + count = counterPayload.count + 1, + name = counterPayload.name + } globalCounter = globalCounter + 1 end elseif keyPressed == keys.enter then