feat(CountersSelector): add 'r' and 'q' + refactor
This commit is contained in:
parent
4903f0a96c
commit
56558fad7a
@ -79,6 +79,17 @@ local function takeN(t, n)
|
|||||||
return result
|
return result
|
||||||
end
|
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)
|
local function renderCountersMap(win, countersMap, selectedCounter)
|
||||||
win.clear()
|
win.clear()
|
||||||
win.setCursorPos(1, 1)
|
win.setCursorPos(1, 1)
|
||||||
@ -86,15 +97,6 @@ local function renderCountersMap(win, countersMap, selectedCounter)
|
|||||||
-- local nbCounters = utils.sizeof(countersMap)
|
-- local nbCounters = utils.sizeof(countersMap)
|
||||||
-- local totalCount = getTotalCount(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 selectedPage = math.floor((selectedCounter - 1) / height) + 1
|
||||||
-- local totalPages = (nbCounters % height) + 1
|
-- local totalPages = (nbCounters % height) + 1
|
||||||
|
|
||||||
@ -129,6 +131,36 @@ local function renderTitle(win, countersMap, selectedCounter, titleFn)
|
|||||||
end)
|
end)
|
||||||
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 function CountersSelector(initialCountersMap, config)
|
||||||
local countersMap = utils.shallowClone(initialCountersMap)
|
local countersMap = utils.shallowClone(initialCountersMap)
|
||||||
local counterMax = config.counterMax
|
local counterMax = config.counterMax
|
||||||
@ -169,25 +201,19 @@ local function CountersSelector(initialCountersMap, config)
|
|||||||
elseif keyPressed == keys.down then
|
elseif keyPressed == keys.down then
|
||||||
selectedCounter = math.min(nbCounters, selectedCounter + 1)
|
selectedCounter = math.min(nbCounters, selectedCounter + 1)
|
||||||
elseif keyPressed == keys.left and globalCounter > 0 then
|
elseif keyPressed == keys.left and globalCounter > 0 then
|
||||||
local counterPayload = countersMap[selectedCounter]
|
if decrementCounter(countersMap, selectedCounter) then
|
||||||
if counterPayload and counterPayload.count and counterPayload.count > 0 then
|
|
||||||
countersMap[selectedCounter] = {
|
|
||||||
count = counterPayload.count - 1,
|
|
||||||
name = counterPayload.name
|
|
||||||
}
|
|
||||||
globalCounter = globalCounter - 1
|
globalCounter = globalCounter - 1
|
||||||
end
|
end
|
||||||
elseif keyPressed == keys.right and globalCounter < counterMax then
|
elseif keyPressed == keys.right and globalCounter < counterMax then
|
||||||
local counterPayload = countersMap[selectedCounter]
|
if incrementCounter(countersMap, selectedCounter) then
|
||||||
if counterPayload and counterPayload.count then
|
|
||||||
countersMap[selectedCounter] = {
|
|
||||||
count = counterPayload.count + 1,
|
|
||||||
name = counterPayload.name
|
|
||||||
}
|
|
||||||
globalCounter = globalCounter + 1
|
globalCounter = globalCounter + 1
|
||||||
end
|
end
|
||||||
|
elseif keyPressed == keys.r then
|
||||||
|
countersMap = resetAllCounters(countersMap)
|
||||||
elseif keyPressed == keys.enter then
|
elseif keyPressed == keys.enter then
|
||||||
shouldContinue = false
|
shouldContinue = false
|
||||||
|
elseif keypRessed == keys.q then
|
||||||
|
return nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user