feat(CountersSelector): refactor + better title bar
This commit is contained in:
parent
ab17e80fa5
commit
7f7d3e801e
@ -4,10 +4,7 @@ local function noTitleFn()
|
|||||||
return ""
|
return ""
|
||||||
end
|
end
|
||||||
|
|
||||||
local function createWriteWithColor(textColor, backgroundColor, givenWin)
|
local function withColor(win, textColor, backgroundColor, callbackFn)
|
||||||
local win = givenWin or term
|
|
||||||
|
|
||||||
return function(str)
|
|
||||||
local originalTextColor = nil
|
local originalTextColor = nil
|
||||||
local originalBackgroundColor = nil
|
local originalBackgroundColor = nil
|
||||||
|
|
||||||
@ -21,7 +18,7 @@ local function createWriteWithColor(textColor, backgroundColor, givenWin)
|
|||||||
win.setBackgroundColor(backgroundColor)
|
win.setBackgroundColor(backgroundColor)
|
||||||
end
|
end
|
||||||
|
|
||||||
win.write(str)
|
local result = table.pack(callbackFn(win))
|
||||||
|
|
||||||
if originalTextColor then
|
if originalTextColor then
|
||||||
win.setTextColor(originalTextColor)
|
win.setTextColor(originalTextColor)
|
||||||
@ -30,6 +27,17 @@ local function createWriteWithColor(textColor, backgroundColor, givenWin)
|
|||||||
if originalBackgroundColor then
|
if originalBackgroundColor then
|
||||||
win.setBackgroundColor(originalBackgroundColor)
|
win.setBackgroundColor(originalBackgroundColor)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return table.unpack(result)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function createWriteWithColor(textColor, backgroundColor, givenWin)
|
||||||
|
local win = givenWin or term
|
||||||
|
|
||||||
|
return function(str)
|
||||||
|
return withColor(win, textColor, backgroundColor, function()
|
||||||
|
return win.write(str)
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -71,49 +79,54 @@ local function takeN(t, n)
|
|||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
local TITLE_MARGIN = 1
|
local function renderCountersMap(win, countersMap, selectedCounter)
|
||||||
|
win.clear()
|
||||||
local function renderCountersMap(win, countersMap, selectedCounter, titleFn)
|
|
||||||
local writeRegular = createWriteWithColor(colors.white, colors.black, win)
|
|
||||||
local writeSelected = createWriteWithColor(colors.black, colors.white, win)
|
|
||||||
|
|
||||||
win.setCursorPos(1, 1)
|
win.setCursorPos(1, 1)
|
||||||
local _, height = win.getSize()
|
local _, height = win.getSize()
|
||||||
-- local nbCounters = utils.sizeof(countersMap)
|
-- local nbCounters = utils.sizeof(countersMap)
|
||||||
-- local totalCount = getTotalCount(countersMap)
|
-- local totalCount = getTotalCount(countersMap)
|
||||||
|
|
||||||
local topMargin = 0
|
-- if titleFn ~= noTitleFn then
|
||||||
local bottomMargin = 0
|
-- withColor(win, colors.white, colors.green, function()
|
||||||
|
-- win.clearLine()
|
||||||
|
-- win.write(titleFn(countersMap, selectedCounter))
|
||||||
|
-- end)
|
||||||
|
|
||||||
if titleFn ~= noTitleFn then
|
-- -- topMargin = TITLE_MARGIN
|
||||||
writeRegular('custom title: ' .. titleFn(countersMap, selectedCounter))
|
-- end
|
||||||
topMargin = TITLE_MARGIN
|
|
||||||
end
|
|
||||||
|
|
||||||
local availableHeight = height - topMargin - bottomMargin
|
local selectedPage = math.floor((selectedCounter - 1) / height) + 1
|
||||||
|
-- local totalPages = (nbCounters % height) + 1
|
||||||
|
|
||||||
local selectedPage = math.floor((selectedCounter - 1) / availableHeight) + 1
|
local nbElementsToOmit = (selectedPage - 1) * height
|
||||||
-- local totalPages = (nbCounters % availableHeight) + 1
|
local displayedCounters = takeN(dropN(countersMap, nbElementsToOmit), height)
|
||||||
|
|
||||||
local nbElementsToOmit = (selectedPage - 1) * availableHeight
|
local cursorYIndex = 1
|
||||||
local displayedCounters = takeN(dropN(countersMap, nbElementsToOmit), availableHeight)
|
|
||||||
|
|
||||||
local cursorYIndex = 1 + topMargin
|
|
||||||
for k,v in pairs(displayedCounters) do
|
for k,v in pairs(displayedCounters) do
|
||||||
win.setCursorPos(1, cursorYIndex)
|
win.setCursorPos(1, cursorYIndex)
|
||||||
|
|
||||||
local line = tostring(v.name) .. ' ' .. tostring(v.count)
|
local writeLine = function() return tostring(v.name) .. ' ' .. tostring(v.count) end
|
||||||
|
|
||||||
if k == selectedCounter then
|
if k == selectedCounter then
|
||||||
writeSelected(line)
|
withColor(win, colors.black, colors.white, writeLine)
|
||||||
else
|
else
|
||||||
writeRegular(line)
|
withColor(win, colors.white, colors.black, writeLine)
|
||||||
end
|
end
|
||||||
|
|
||||||
cursorYIndex = cursorYIndex + 1
|
cursorYIndex = cursorYIndex + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function renderTitle(win, countersMap, selectedCounter, titleFn)
|
||||||
|
win.clear()
|
||||||
|
win.setCursorPos(1, 1)
|
||||||
|
|
||||||
|
withColor(win, colors.white, colors.green, function()
|
||||||
|
win.clearLine()
|
||||||
|
win.write(titleFn(countersMap, selectedCounter))
|
||||||
|
end)
|
||||||
|
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
|
||||||
@ -135,19 +148,18 @@ local function CountersSelector(initialCountersMap, config)
|
|||||||
error('counter cannot be greater than the counterMax')
|
error('counter cannot be greater than the counterMax')
|
||||||
end
|
end
|
||||||
|
|
||||||
local topHeight = 0
|
local topHeight = 1
|
||||||
if titleFn ~= noTitleFn then
|
|
||||||
topHeight = 1
|
|
||||||
end
|
|
||||||
|
|
||||||
term.clear()
|
term.clear()
|
||||||
local width, height = term.getSize()
|
local width, height = term.getSize()
|
||||||
local mainWin = window.create(term.current(), 1, 1 + topHeight, width, height - topHeight)
|
local mainWin = window.create(term.current(), 1, 1 + topHeight, width, height - topHeight)
|
||||||
|
local titleWin = window.create(term.current(), 1, 1, width, 1 + topHeight)
|
||||||
|
|
||||||
local shouldContinue = true
|
local shouldContinue = true
|
||||||
while shouldContinue do
|
while shouldContinue do
|
||||||
mainWin.clear()
|
renderTitle(titleWin, countersMap, selectedCounter, titleFn)
|
||||||
renderCountersMap(mainWin, countersMap, selectedCounter, titleFn)
|
renderCountersMap(mainWin, countersMap, selectedCounter)
|
||||||
|
|
||||||
local _, keyPressed, _ = os.pullEvent('key')
|
local _, keyPressed, _ = os.pullEvent('key')
|
||||||
|
|
||||||
if keyPressed == keys.up then
|
if keyPressed == keys.up then
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user