From e29b4e91903f75a8c1ae9deeec45eefd1d90b833 Mon Sep 17 00:00:00 2001 From: neru Date: Thu, 8 May 2025 00:57:44 -0300 Subject: [PATCH] refactor: rewrite text wrapping --- lua/CC Tweaked/tutel/tutel-host.lua | 100 ++++++++++++++-------------- 1 file changed, 49 insertions(+), 51 deletions(-) diff --git a/lua/CC Tweaked/tutel/tutel-host.lua b/lua/CC Tweaked/tutel/tutel-host.lua index cba70fe..23d754a 100644 --- a/lua/CC Tweaked/tutel/tutel-host.lua +++ b/lua/CC Tweaked/tutel/tutel-host.lua @@ -23,42 +23,28 @@ modem.open(HOST_CHANNEL) --[[ helper functions ]] -function wrapText(text, width, prefixWidth) +function wrapText(text, maxWidth) 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 + local currentLine = "" + for word in text:gmatch("%S+") do + if #currentLine + #word + 1 <= maxWidth then + currentLine = currentLine .. (currentLine == "" and "" or " ") .. word + else + if currentLine == "" then + 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 - 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 - + if currentLine ~= "" then + table.insert(lines, currentLine) + end return lines end @@ -262,39 +248,51 @@ function drawLogs(x0, y0, x1, y1) term.setBackgroundColor(format["BG"]) term.setTextColor(format["FG"]) - local prefix = ("[%s]"):format(entry.type) - term.write(prefix) + local prefix = ("[%s] "):format(entry.type) + local prefixLength = #prefix + local maxMessageWidth = w - prefixLength + if maxMessageWidth <= 0 then maxMessageWidth = 1 end - local lines = wrapText(entry.message, w, prefix:len()) + local wrappedMessage = wrapText(entry.message, maxMessageWidth) - if not format["AffectContent"] then - term.setBackgroundColor(prevColourBg) - term.setTextColor(prevColourFg) + local messageBG, messageFG + if format.AffectContent then + messageBG = format.BG + messageFG = format.FG + else + messageBG = prevColourBg + messageFG = prevColourFg end - local isFirstLine = false - for _, line in pairs(lines) do + for lineIdx, line in ipairs(wrappedMessage) do if drawnLines > h then break end - term.setCursorPos(1, y1 - drawnLines) + local currentY = y1 - drawnLines + term.setCursorPos(1, currentY) - if isFirstLine then - term.setCursorPos(prefix:len() + 1, y1 - drawnLines) - isFirstLine = false + if lineIdx == 1 then + term.setBackgroundColor(format.BG) + term.setTextColor(format.FG) + term.write(prefix) + + term.setBackgroundColor(messageBG) + term.setTextColor(messageFG) + term.write(line) + else + + term.setBackgroundColor(messageBG) + term.setTextColor(messageFG) + term.write((" "):rep(prefixLength)) + term.write(line) end - term.write(line) - + term.setBackgroundColor(messageBG) + term.setCursorPos(1 + prefixLength + #line, currentY) + term.clearLine() + drawnLines = drawnLines + 1 end - - if format["AffectContent"] then - term.setBackgroundColor(prevColourBg) - term.setTextColor(prevColourFg) - end - - drawnLines = drawnLines + 1 end term.setBackgroundColor(prevColourBg)