This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KevinW1998's profile picture

automatical line breaks

Started by KevinW1998, 08 July 2012 - 12:24 PM
KevinW1998 #1
Posted 08 July 2012 - 02:24 PM
I have special area with information but I want to have auto line breaks like print()
So does anyone know how to do this?

Note: the text is form the user himself!
1lann #2
Posted 08 July 2012 - 02:43 PM
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
*whoops, edited*
MysticT #3
Posted 08 July 2012 - 02:57 PM
Why don't you use print?
If it's for a monitor, redirect the terminal to the monitor before printing, using term.redirect, and then use print.
KevinW1998 #4
Posted 08 July 2012 - 02:58 PM
I mean the user has input information and I want it to display it in a box e.g. 15x15
but I don't know how to :)/>/>
ardera #5
Posted 08 July 2012 - 06:48 PM
code:

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…
KevinW1998 #6
Posted 08 July 2012 - 07:18 PM
Nope didn't work :)/>/>
cant_delete_account #7
Posted 08 July 2012 - 07:39 PM
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
n*
KevinW1998 #8
Posted 08 July 2012 - 07:56 PM
Well I know that!
I only want to split the string at a space so it fits in the box:

e.g.:

+------------------+
| This is a text   |
| that fits in the |
| box!		   |
|		   |
|		   |
|		   |
|		   |
|		   |
|		   |
|		   |
|		   |
+------------------+
MysticT #9
Posted 08 July 2012 - 10:08 PM
And why don't you copy the write code and change the size to the one you want?
write and print functions (from bios.lua):

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.
KevinW1998 #10
Posted 09 July 2012 - 08:01 AM
Thanks MysticT!
I thought that print is a "main" method that doesn't need to get rewritten in bios.lua

:)/>/>
Edit: not finished yet B)/>/>
I want to use special x-coor but it always prints at the beginning of the screen B)/>/>
KevinW1998 #11
Posted 09 July 2012 - 09:29 PM
Does anyone know how to fix it? :)/>/>