fix(ui): better rendering result for CountersSelector

This commit is contained in:
Guillaume ARM 2024-05-23 20:58:01 +02:00
parent b8c83bc959
commit b2beb2fdbb

View File

@ -4,17 +4,32 @@ local function noTitleFn()
return "" return ""
end end
local savedTextColor = nil local function createWriteWithColor(textColor, backgroundColor, givenWin)
local savedBackgroundColor = nil local win = givenWin or term
local originalTextColor = nil
local originalBackgroundColor = nil
local function saveTermConfig() if textColor then
savedTextColor = term.getTextColor() originalTextColor = win.getTextColor()
savedBackgroundColor = term.getBackgroundColor() win.setTextColor(textColor)
end end
local function restoreTermConfig() if backgroundColor then
term.setTextColor(savedTextColor) originalBackgroundColor = win.getBackgroundColor()
term.setBackgroundColor(savedBackgroundColor) win.setBackgroundColor(backgroundColor)
end
return function(str)
win.write(str)
if originalTextColor then
win.setTextColor(originalTextColor)
end
if originalBackgroundColor then
win.setBackgroundColor(originalBackgroundColor)
end
end
end end
local function getTotalCount(countersMap) local function getTotalCount(countersMap)
@ -58,6 +73,9 @@ end
local TITLE_MARGIN = 1 local TITLE_MARGIN = 1
local function renderCountersMap(win, countersMap, selectedCounter, titleFn) 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)
@ -67,7 +85,7 @@ local function renderCountersMap(win, countersMap, selectedCounter, titleFn)
local bottomMargin = 0 local bottomMargin = 0
if titleFn ~= noTitleFn then if titleFn ~= noTitleFn then
win.write('custom title: ' .. titleFn(countersMap, selectedCounter)) writeRegular('custom title: ' .. titleFn(countersMap, selectedCounter))
topMargin = TITLE_MARGIN topMargin = TITLE_MARGIN
end end
@ -83,15 +101,14 @@ local function renderCountersMap(win, countersMap, selectedCounter, titleFn)
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)
if k == selectedCounter then if k == selectedCounter then
win.setBackgroundColor(colors.white) writeSelected(line)
win.setTextColor(colors.black)
else else
win.setBackgroundColor(colors.black) writeRegular(line)
win.setTextColor(colors.white)
end end
win.write(tostring(v.name) .. ' ' .. tostring(v.count))
cursorYIndex = cursorYIndex + 1 cursorYIndex = cursorYIndex + 1
end end
end end
@ -109,8 +126,6 @@ local function CountersSelector(initialCountersMap, config)
error('empty countersMap provided') error('empty countersMap provided')
end end
-- saveTermConfig()
local selectedCounter = 1 local selectedCounter = 1
local nbCounters = utils.sizeof(countersMap) local nbCounters = utils.sizeof(countersMap)
local globalCounter = getTotalCount(countersMap) local globalCounter = getTotalCount(countersMap)
@ -125,13 +140,14 @@ local function CountersSelector(initialCountersMap, config)
end end
term.clear() term.clear()
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 shouldContinue = true local shouldContinue = true
while shouldContinue do while shouldContinue do
mainWin.clear() mainWin.clear()
renderCountersMap(mainWin, countersMap, selectedCounter, titleFn) renderCountersMap(mainWin, countersMap, selectedCounter, titleFn)
local _, keyPressed, isHeld = os.pullEvent('key') local _, keyPressed, _ = os.pullEvent('key')
if keyPressed == keys.up then if keyPressed == keys.up then
selectedCounter = math.max(1, selectedCounter - 1) selectedCounter = math.max(1, selectedCounter - 1)
@ -160,8 +176,6 @@ local function CountersSelector(initialCountersMap, config)
end end
end end
-- restoreTermConfig()
return countersMap, selectedCounter return countersMap, selectedCounter
end end