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