fix: misc changes
This commit is contained in:
@@ -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,9 +254,6 @@ 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
|
||||
@@ -243,18 +261,14 @@ function drawLogs(x0, y0, x1, y1)
|
||||
local entry = log.buffer[i]
|
||||
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 prefixLength = #prefix
|
||||
local maxMessageWidth = math.max(1, w - prefixLength)
|
||||
|
||||
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 wrappedMessage = wrapText(tostring(entry.message), maxMessageWidth)
|
||||
|
||||
local messageBG, messageFG
|
||||
if format.AffectContent then
|
||||
@@ -265,41 +279,35 @@ function drawLogs(x0, y0, x1, y1)
|
||||
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
|
||||
end
|
||||
|
||||
term.setBackgroundColor(prevColourBg)
|
||||
term.setTextColor(prevColourFg)
|
||||
term.setBackgroundColor(prevColourBg)
|
||||
term.setTextColor(prevColourFg)
|
||||
end
|
||||
end
|
||||
|
||||
function drawStatusBar(y)
|
||||
|
||||
Reference in New Issue
Block a user