feat: wrap lines on logs
This commit is contained in:
@@ -20,6 +20,48 @@ local MSG_HEADER = 0x747574656C
|
||||
local modem = peripheral.find("modem") or error("No modems found", 0)
|
||||
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
|
||||
]]
|
||||
@@ -219,18 +261,43 @@ function drawLogs(x0, y0, x1, y1)
|
||||
|
||||
term.setBackgroundColor(format["BG"])
|
||||
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
|
||||
term.write((" %s\n"):format(entry.message))
|
||||
-- term.write((" %s\n"):format(entry.message))
|
||||
term.setBackgroundColor(prevColourBg)
|
||||
term.setTextColor(prevColourFg)
|
||||
else
|
||||
term.setBackgroundColor(prevColourBg)
|
||||
term.setTextColor(prevColourFg)
|
||||
term.write((" %s\n"):format(entry.message))
|
||||
end
|
||||
|
||||
|
||||
-- else
|
||||
-- term.write((" %s\n"):format(entry.message))
|
||||
-- end
|
||||
|
||||
drawnLines = drawnLines + 1
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user