From b2beb2fdbbc55deaa083effbd7ebbc2a8471c2aa Mon Sep 17 00:00:00 2001 From: Guillaume ARM Date: Thu, 23 May 2024 20:58:01 +0200 Subject: [PATCH] fix(ui): better rendering result for CountersSelector --- libs/ui/CountersSelector.lua | 54 +++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/libs/ui/CountersSelector.lua b/libs/ui/CountersSelector.lua index 9d71122..d9e77ca 100644 --- a/libs/ui/CountersSelector.lua +++ b/libs/ui/CountersSelector.lua @@ -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() -end + 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