fix: misc changes

This commit is contained in:
2025-05-08 01:25:34 -03:00
parent 2f22573319
commit 4638994190
+43 -35
View File
@@ -24,30 +24,51 @@ modem.open(HOST_CHANNEL)
helper functions helper functions
]] ]]
function wrapText(text, maxWidth) function wrapText(text, maxWidth)
if not text then return {} end
if maxWidth <= 0 then return {text} end
local lines = {} local lines = {}
local currentLine = "" local currentLine = ""
for word in text:gmatch("%S+") do
if #currentLine + #word + 1 <= maxWidth then for word, newline in text:gmatch("([^%s\n]*)(%s*\n?)") do
currentLine = currentLine .. (currentLine == "" and "" or " ") .. word if newline:find("\n") then
if currentLine ~= "" then
table.insert(lines, currentLine)
currentLine = ""
end
if word ~= "" then
table.insert(lines, word)
else else
table.insert(lines, "")
end
elseif #currentLine + #word <= maxWidth then
if currentLine == "" then if currentLine == "" then
currentLine = word
else
currentLine = currentLine .. " " .. word
end
else
if currentLine ~= "" then
table.insert(lines, currentLine)
currentLine = word
else
while #word > maxWidth do while #word > maxWidth do
table.insert(lines, word:sub(1, maxWidth)) table.insert(lines, word:sub(1, maxWidth))
word = word:sub(maxWidth + 1) word = word:sub(maxWidth + 1)
end end
currentLine = word currentLine = word
else
table.insert(lines, currentLine)
currentLine = word
end end
end end
end end
if currentLine ~= "" then if currentLine ~= "" then
table.insert(lines, currentLine) table.insert(lines, currentLine)
end end
return lines return lines
end end
--[[ --[[
logging logging
]] ]]
@@ -233,9 +254,6 @@ 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
@@ -243,18 +261,14 @@ function drawLogs(x0, y0, x1, y1)
local entry = log.buffer[i] local entry = log.buffer[i]
local format = logTypes[entry.type] local format = logTypes[entry.type]
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) local prefix = ("[%s] "):format(entry.type)
local prefixLength = #prefix + 1 local prefixLength = #prefix
local maxMessageWidth = w - prefixLength local maxMessageWidth = math.max(1, w - prefixLength)
if maxMessageWidth <= 0 then maxMessageWidth = 1 end local wrappedMessage = wrapText(tostring(entry.message), maxMessageWidth)
local wrappedMessage = wrapText(entry.message, maxMessageWidth)
local messageBG, messageFG local messageBG, messageFG
if format.AffectContent then if format.AffectContent then
@@ -265,42 +279,36 @@ function drawLogs(x0, y0, x1, y1)
messageFG = prevColourFg messageFG = prevColourFg
end end
for lineIdx = #wrappedMessage, 1, -1 do for lineIdx, line in ipairs(wrappedMessage) do
if drawnLines > h then break end if drawnLines > h then break end
local line = wrappedMessage[i] term.setCursorPos(1, y1 - drawnLines)
local currentY = y1 - drawnLines
term.setCursorPos(1, currentY)
term.setBackgroundColor(messageBG)
term.clearLine()
if lineIdx == 1 then if lineIdx == 1 then
term.setBackgroundColor(format.BG) term.setBackgroundColor(format.BG)
term.setTextColor(format.FG) term.setTextColor(format.FG)
term.write(prefix) term.write(prefix)
term.setBackgroundColor(messageBG)
term.setTextColor(messageFG)
term.write(" ")
term.write(line)
else else
term.setBackgroundColor(messageBG) term.setBackgroundColor(messageBG)
term.setTextColor(messageFG)
term.write((" "):rep(prefixLength)) term.write((" "):rep(prefixLength))
end
term.setBackgroundColor(messageBG)
term.setTextColor(messageFG)
term.write(line) term.write(line)
local remaining = w - (prefixLength + #line)
if remaining > 0 then
term.write((" "):rep(remaining))
end end
drawnLines = drawnLines + 1 drawnLines = drawnLines + 1
end end
end
term.setBackgroundColor(prevColourBg) term.setBackgroundColor(prevColourBg)
term.setTextColor(prevColourFg) term.setTextColor(prevColourFg)
end end
end
function drawStatusBar(y) function drawStatusBar(y)
term.setCursorPos(1, y) term.setCursorPos(1, y)