From 8236be1ffffc09216a445498cf8402b828716ad4 Mon Sep 17 00:00:00 2001 From: neru Date: Thu, 8 May 2025 00:29:02 -0300 Subject: [PATCH] feat: wrap lines on logs --- lua/CC Tweaked/tutel/tutel-host.lua | 81 ++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 7 deletions(-) diff --git a/lua/CC Tweaked/tutel/tutel-host.lua b/lua/CC Tweaked/tutel/tutel-host.lua index 068372b..0d312e1 100644 --- a/lua/CC Tweaked/tutel/tutel-host.lua +++ b/lua/CC Tweaked/tutel/tutel-host.lua @@ -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,25 +261,50 @@ 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 function drawStatusBar(y) term.setCursorPos(1, y) - + term.setBackgroundColor(colors.green) term.setTextColor(colors.white) term.clearLine()