Compare commits

..

2 Commits

Author SHA1 Message Date
neru e29b4e9190 refactor: rewrite text wrapping 2025-05-08 00:57:44 -03:00
neru a00403549a fix: properly restore color 2025-05-08 00:55:39 -03:00
+47 -52
View File
@@ -23,42 +23,28 @@ modem.open(HOST_CHANNEL)
--[[ --[[
helper functions helper functions
]] ]]
function wrapText(text, width, prefixWidth) function wrapText(text, maxWidth)
local lines = {} local lines = {}
local isFirstLine = true
for originalLine in text:gmatch("[^\n]+") do
local currentLine = "" local currentLine = ""
for word in text:gmatch("%S+") do
local currentLength = 0 if #currentLine + #word + 1 <= maxWidth then
currentLine = currentLine .. (currentLine == "" and "" or " ") .. word
if isFirstLine then else
isFirstLine = false if currentLine == "" then
currentLength = prefixWidth while #word > maxWidth do
table.insert(lines, word:sub(1, maxWidth))
word = word:sub(maxWidth + 1)
end end
currentLine = word
for word in originalLine:gmatch("%S+") do else
if currentLength + #word > width and currentLength > 0 then
table.insert(lines, currentLine) table.insert(lines, currentLine)
currentLine = word currentLine = word
currentLength = #word
else
if currentLength > 0 then
currentLine = currentLine .. " " .. word
currentLength = currentLength + 1 + #word
else
currentLine = word
currentLength = #word
end end
end end
end end
if currentLine ~= "" then
if currentLength > 0 then
table.insert(lines, currentLine) table.insert(lines, currentLine)
end end
end
return lines return lines
end end
@@ -247,6 +233,9 @@ function drawLogs(x0, y0, x1, y1)
local w = x1 - x0 local w = x1 - x0
local h = y1 - y0 local h = y1 - y0
local prevColourBg = term.getBackgroundColour()
local prevColourFg = term.getTextColour()
local drawnLines = 0 local drawnLines = 0
for i = #log.buffer - logOffset, 1, -1 do for i = #log.buffer - logOffset, 1, -1 do
if drawnLines > h then break end if drawnLines > h then break end
@@ -256,54 +245,60 @@ function drawLogs(x0, y0, x1, y1)
term.setCursorPos(1, y1 - drawnLines) term.setCursorPos(1, y1 - drawnLines)
local prevColourBg = term.getBackgroundColour()
local prevColourFg = term.getTextColour()
term.setBackgroundColor(format["BG"]) term.setBackgroundColor(format["BG"])
term.setTextColor(format["FG"]) term.setTextColor(format["FG"])
local prefix = ("[%s] "):format(entry.type) local prefix = ("[%s] "):format(entry.type)
term.write(prefix) local prefixLength = #prefix
local maxMessageWidth = w - prefixLength
if maxMessageWidth <= 0 then maxMessageWidth = 1 end
local lines = wrapText(entry.message, w, prefix:len()) local wrappedMessage = wrapText(entry.message, maxMessageWidth)
if not format["AffectContent"] then local messageBG, messageFG
term.setBackgroundColor(prevColourBg) if format.AffectContent then
term.setTextColor(prevColourFg) messageBG = format.BG
messageFG = format.FG
else
messageBG = prevColourBg
messageFG = prevColourFg
end end
local isFirstLine = false for lineIdx, line in ipairs(wrappedMessage) do
for _, line in pairs(lines) do
if drawnLines > h then break end if drawnLines > h then break end
term.setCursorPos(1, y1 - drawnLines) local currentY = y1 - drawnLines
term.setCursorPos(1, currentY)
if isFirstLine then if lineIdx == 1 then
term.setCursorPos(prefix:len(), y1 - drawnLines) term.setBackgroundColor(format.BG)
isFirstLine = false term.setTextColor(format.FG)
term.write(prefix)
term.setBackgroundColor(messageBG)
term.setTextColor(messageFG)
term.write(line)
else
term.setBackgroundColor(messageBG)
term.setTextColor(messageFG)
term.write((" "):rep(prefixLength))
term.write(line)
end end
term.write(line) term.setBackgroundColor(messageBG)
term.setCursorPos(1 + prefixLength + #line, currentY)
term.clearLine()
drawnLines = drawnLines + 1 drawnLines = drawnLines + 1
end end
end
if format["AffectContent"] then
-- term.write((" %s\n"):format(entry.message))
term.setBackgroundColor(prevColourBg) term.setBackgroundColor(prevColourBg)
term.setTextColor(prevColourFg) term.setTextColor(prevColourFg)
end end
-- else
-- term.write((" %s\n"):format(entry.message))
-- end
drawnLines = drawnLines + 1
end
end
function drawStatusBar(y) function drawStatusBar(y)
term.setCursorPos(1, y) term.setCursorPos(1, y)