feat: wrap lines on logs

This commit is contained in:
2025-05-08 00:29:02 -03:00
parent 84637cb3dd
commit 8236be1fff
+73 -6
View File
@@ -20,6 +20,48 @@ local MSG_HEADER = 0x747574656C
local modem = peripheral.find("modem") or error("No modems found", 0) local modem = peripheral.find("modem") or error("No modems found", 0)
modem.open(HOST_CHANNEL) modem.open(HOST_CHANNEL)
--[[
helper functions
]]
function wrapText(text, width, prefixWidth)
local lines = {}
local isFirstLine = true
for originalLine in text:gmatch("[^\n]+") do
local currentLine = ""
local currentLength = 0
if isFirstLine then
isFirstLine = false
currentLength = prefixWidth
end
for word in originalLine:gmatch("%S+") do
if currentLength + #word > width and currentLength > 0 then
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
table.insert(lines, currentLine)
end
end
return table.concat(lines, "\n")
end
--[[ --[[
logging logging
]] ]]
@@ -219,18 +261,43 @@ function drawLogs(x0, y0, x1, y1)
term.setBackgroundColor(format["BG"]) term.setBackgroundColor(format["BG"])
term.setTextColor(format["FG"]) term.setTextColor(format["FG"])
term.write(("[%s]"):format(entry.type))
local prefix = ("[%s]"):format(entry.type)
term.write(prefix)
local lines = wrapText(entry.message, w, prefix:len())
if not format["AffectContent"] then
term.setBackgroundColor(prevColourBg)
term.setTextColor(prevColourFg)
end
local isFirstLine = false
for _, line in pairs(lines) do
if drawnLines > h then break end
term.setCursorPos(1, y1 - drawnLines)
if isFirstLine then
term.setCursorPos(prefix:len(), y1 - drawnLines)
isFirstLine = false
end
drawnLines = drawnLines + 1
end
if format["AffectContent"] then if format["AffectContent"] then
term.write((" %s\n"):format(entry.message)) -- term.write((" %s\n"):format(entry.message))
term.setBackgroundColor(prevColourBg) term.setBackgroundColor(prevColourBg)
term.setTextColor(prevColourFg) term.setTextColor(prevColourFg)
else
term.setBackgroundColor(prevColourBg)
term.setTextColor(prevColourFg)
term.write((" %s\n"):format(entry.message))
end end
-- else
-- term.write((" %s\n"):format(entry.message))
-- end
drawnLines = drawnLines + 1 drawnLines = drawnLines + 1
end end
end end