mirror of
https://github.com/Ale32bit/Capy64.git
synced 2025-12-15 02:25:44 +00:00
79 lines
No EOL
2.1 KiB
Lua
79 lines
No EOL
2.1 KiB
Lua
local term = require("term")
|
|
local expect = require("expect").expect
|
|
|
|
function write(sText)
|
|
expect(1, sText, "string", "number")
|
|
|
|
local w, h = term.getSize()
|
|
local x, y = term.getPos()
|
|
|
|
local nLinesPrinted = 0
|
|
local function newLine()
|
|
if y + 1 <= h then
|
|
term.setPos(1, y + 1)
|
|
else
|
|
term.setPos(1, h)
|
|
term.scroll(1)
|
|
end
|
|
x, y = term.getPos()
|
|
nLinesPrinted = nLinesPrinted + 1
|
|
end
|
|
|
|
-- Print the line with proper word wrapping
|
|
sText = tostring(sText)
|
|
while #sText > 0 do
|
|
local whitespace = string.match(sText, "^[ \t]+")
|
|
if whitespace then
|
|
-- Print whitespace
|
|
term.write(whitespace)
|
|
x, y = term.getPos()
|
|
sText = string.sub(sText, #whitespace + 1)
|
|
end
|
|
|
|
local newline = string.match(sText, "^\n")
|
|
if newline then
|
|
-- Print newlines
|
|
newLine()
|
|
sText = string.sub(sText, 2)
|
|
end
|
|
|
|
local text = string.match(sText, "^[^ \t\n]+")
|
|
if text then
|
|
sText = string.sub(sText, #text + 1)
|
|
if #text > w then
|
|
-- Print a multiline word
|
|
while #text > 0 do
|
|
if x > w then
|
|
newLine()
|
|
end
|
|
term.write(text)
|
|
text = string.sub(text, w - x + 2)
|
|
x, y = term.getPos()
|
|
end
|
|
else
|
|
-- Print a word normally
|
|
if x + #text - 1 > w then
|
|
newLine()
|
|
end
|
|
term.write(text)
|
|
x, y = term.getPos()
|
|
end
|
|
end
|
|
end
|
|
|
|
return nLinesPrinted
|
|
end
|
|
|
|
function print(...)
|
|
local nLinesPrinted = 0
|
|
local nLimit = select("#", ...)
|
|
for n = 1, nLimit do
|
|
local s = tostring(select(n, ...))
|
|
if n < nLimit then
|
|
s = s .. "\t"
|
|
end
|
|
nLinesPrinted = nLinesPrinted + write(s)
|
|
end
|
|
nLinesPrinted = nLinesPrinted + write("\n")
|
|
return nLinesPrinted
|
|
end |