So does anyone know how to do this?
Note: the text is form the user himself!
n
*whoops, edited*
function boxPrint(str)
for n=1, math.floor(string.len(str) / 15) do
write(string.sub(str, n*15,(n+1)*15).."n")
end
end
boxPrint("This text will print in 15*15")
I didnt test it, but i think it will work…n*Hmmm, you'll need to be more clear on what you want to do. I cannot understand what you mean by "Auto line breaks" Though I'm assuming you want to know the regex for a new line in lua? well it's/n
+------------------+
| This is a text |
| that fits in the |
| box! |
| |
| |
| |
| |
| |
| |
| |
| |
+------------------+
function write( sText )
local w,h = term.getSize() -- change the size here (eg: 15, 10)
local x,y = term.getCursorPos()
local nLinesPrinted = 0
local function newLine()
if y + 1 <= h then
term.setCursorPos(1, y + 1)
else
term.scroll(1)
term.setCursorPos(1, h)
end
x, y = term.getCursorPos()
nLinesPrinted = nLinesPrinted + 1
end
-- Print the line with proper word wrapping
while string.len(sText) > 0 do
local whitespace = string.match( sText, "^[ t]+" )
if whitespace then
-- Print whitespace
term.write( whitespace )
x,y = term.getCursorPos()
sText = string.sub( sText, string.len(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, "^[^ tn]+" )
if text then
sText = string.sub( sText, string.len(text) + 1 )
if string.len(text) > w then
-- Print a multiline word
while string.len( text ) > 0 do
if x > w then
newLine()
end
term.write( text )
text = string.sub( text, (w-x) + 2 )
x,y = term.getCursorPos()
end
else
-- Print a word normally
if x + string.len(text) > w then
newLine()
end
term.write( text )
x,y = term.getCursorPos()
end
end
end
return nLinesPrinted
end
function print( ... )
local nLinesPrinted = 0
for n,v in ipairs( { ... } ) do
nLinesPrinted = nLinesPrinted + write( tostring( v ) )
end
nLinesPrinted = nLinesPrinted + write( "n" )
return nLinesPrinted
end
You can simply copy that to your program, change the function names and the line that sets the size.