diff --git a/lua/CC Tweaked/tutel/tutel-host.lua b/lua/CC Tweaked/tutel/tutel-host.lua index 24d6a7d..9864a47 100644 --- a/lua/CC Tweaked/tutel/tutel-host.lua +++ b/lua/CC Tweaked/tutel/tutel-host.lua @@ -24,30 +24,51 @@ modem.open(HOST_CHANNEL) helper functions ]] function wrapText(text, maxWidth) + if not text then return {} end + if maxWidth <= 0 then return {text} end + local lines = {} local currentLine = "" - for word in text:gmatch("%S+") do - if #currentLine + #word + 1 <= maxWidth then - currentLine = currentLine .. (currentLine == "" and "" or " ") .. word - else + + for word, newline in text:gmatch("([^%s\n]*)(%s*\n?)") do + if newline:find("\n") then + if currentLine ~= "" then + table.insert(lines, currentLine) + currentLine = "" + end + if word ~= "" then + table.insert(lines, word) + else + table.insert(lines, "") + end + elseif #currentLine + #word <= maxWidth 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 table.insert(lines, word:sub(1, maxWidth)) word = word:sub(maxWidth + 1) end currentLine = word - else - table.insert(lines, currentLine) - currentLine = word end end end + if currentLine ~= "" then table.insert(lines, currentLine) end + return lines end + --[[ logging ]] @@ -233,29 +254,22 @@ 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 - + local entry = log.buffer[i] local format = logTypes[entry.type] - - term.setCursorPos(1, y1 - drawnLines) - - term.setBackgroundColor(format["BG"]) - term.setTextColor(format["FG"]) - - local prefix = ("[%s]"):format(entry.type) - local prefixLength = #prefix + 1 - local maxMessageWidth = w - prefixLength - - if maxMessageWidth <= 0 then maxMessageWidth = 1 end - - local wrappedMessage = wrapText(entry.message, maxMessageWidth) - + + local prevColourBg = term.getBackgroundColour() + local prevColourFg = term.getTextColour() + + local prefix = ("[%s] "):format(entry.type) + local prefixLength = #prefix + local maxMessageWidth = math.max(1, w - prefixLength) + + local wrappedMessage = wrapText(tostring(entry.message), maxMessageWidth) + local messageBG, messageFG if format.AffectContent then messageBG = format.BG @@ -264,42 +278,36 @@ function drawLogs(x0, y0, x1, y1) messageBG = prevColourBg messageFG = prevColourFg end - - for lineIdx = #wrappedMessage, 1, -1 do + + for lineIdx, line in ipairs(wrappedMessage) do if drawnLines > h then break end - - local line = wrappedMessage[i] - - local currentY = y1 - drawnLines - term.setCursorPos(1, currentY) - - term.setBackgroundColor(messageBG) - term.clearLine() - + + term.setCursorPos(1, y1 - drawnLines) + if lineIdx == 1 then term.setBackgroundColor(format.BG) term.setTextColor(format.FG) term.write(prefix) - - - term.setBackgroundColor(messageBG) - term.setTextColor(messageFG) - - term.write(" ") - term.write(line) else term.setBackgroundColor(messageBG) - term.setTextColor(messageFG) term.write((" "):rep(prefixLength)) - term.write(line) end - + + term.setBackgroundColor(messageBG) + term.setTextColor(messageFG) + term.write(line) + + local remaining = w - (prefixLength + #line) + if remaining > 0 then + term.write((" "):rep(remaining)) + end + drawnLines = drawnLines + 1 end + + term.setBackgroundColor(prevColourBg) + term.setTextColor(prevColourFg) end - - term.setBackgroundColor(prevColourBg) - term.setTextColor(prevColourFg) end function drawStatusBar(y)