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
]]
function wrapText(text, width, prefixWidth)
function wrapText(text, maxWidth)
local lines = {}
local isFirstLine = true
for originalLine in text:gmatch("[^\n]+") do
local currentLine = ""
local currentLength = 0
if isFirstLine then
isFirstLine = false
currentLength = prefixWidth
for word in text:gmatch("%S+") do
if #currentLine + #word + 1 <= maxWidth then
currentLine = currentLine .. (currentLine == "" and "" or " ") .. word
else
if currentLine == "" then
while #word > maxWidth do
table.insert(lines, word:sub(1, maxWidth))
word = word:sub(maxWidth + 1)
end
for word in originalLine:gmatch("%S+") do
if currentLength + #word > width and currentLength > 0 then
currentLine = word
else
table.insert(lines, currentLine)
currentLine = word
currentLength = #word
else
if currentLength > 0 then
currentLine = currentLine .. " " .. word
currentLength = currentLength + 1 + #word
else
currentLine = word
currentLength = #word
end
end
end
if currentLength > 0 then
if currentLine ~= "" then
table.insert(lines, currentLine)
end
end
return lines
end
@@ -247,6 +233,9 @@ function drawLogs(x0, y0, x1, y1)
local w = x1 - x0
local h = y1 - y0
local prevColourBg = term.getBackgroundColour()
local prevColourFg = term.getTextColour()
local drawnLines = 0
for i = #log.buffer - logOffset, 1, -1 do
if drawnLines > h then break end
@@ -256,54 +245,60 @@ function drawLogs(x0, y0, x1, y1)
term.setCursorPos(1, y1 - drawnLines)
local prevColourBg = term.getBackgroundColour()
local prevColourFg = term.getTextColour()
term.setBackgroundColor(format["BG"])
term.setTextColor(format["FG"])
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
term.setBackgroundColor(prevColourBg)
term.setTextColor(prevColourFg)
local messageBG, messageFG
if format.AffectContent then
messageBG = format.BG
messageFG = format.FG
else
messageBG = prevColourBg
messageFG = prevColourFg
end
local isFirstLine = false
for _, line in pairs(lines) do
for lineIdx, line in ipairs(wrappedMessage) do
if drawnLines > h then break end
term.setCursorPos(1, y1 - drawnLines)
local currentY = y1 - drawnLines
term.setCursorPos(1, currentY)
if isFirstLine then
term.setCursorPos(prefix:len(), y1 - drawnLines)
isFirstLine = false
if lineIdx == 1 then
term.setBackgroundColor(format.BG)
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
term.write(line)
term.setBackgroundColor(messageBG)
term.setCursorPos(1 + prefixLength + #line, currentY)
term.clearLine()
drawnLines = drawnLines + 1
end
end
if format["AffectContent"] then
-- term.write((" %s\n"):format(entry.message))
term.setBackgroundColor(prevColourBg)
term.setTextColor(prevColourFg)
end
-- else
-- term.write((" %s\n"):format(entry.message))
-- end
drawnLines = drawnLines + 1
end
end
function drawStatusBar(y)
term.setCursorPos(1, y)