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