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

Is there a way to set where a new line starts?

Started by Spexiono, 21 February 2014 - 03:10 AM
Spexiono #1
Posted 21 February 2014 - 04:10 AM
Hey,

Is there a way to set where the new line starts whenever i use the print() command?
At the moment i have this:

term.setCursorPos(17,7)
print("+------------------+")
term.setCursorPos(17,8)
print("|				  |")
term.setCursorPos(17,9)
print("+------------------+")
to produce a box like this:


(Edit: Formatting didn't work so i made a screen shot, How do you force a post to show multiple spaces?)

Is there a better way to set where the new line starts so i don't have to essentially repeat term.setCursorPos(17,x)

I can't do this:

print("				 +------------------+")
print("				 |                  |")
print("				 +------------------+")

Because this replaces any text to the left of the box with spaces which overwrites other parts of my program.
Edited on 21 February 2014 - 03:19 AM
MKlegoman357 #2
Posted 21 February 2014 - 04:44 AM
You can write your own print function but you should use term.write instead of print because you don't really need line wrapping. There's no other way of doing it, you'll have to set the cursor every time you write. What you could do is put your GUI into a table and then use a for loop to draw each row stored in a table.
Bomb Bloke #3
Posted 21 February 2014 - 05:38 AM
A (basic) example of such a function:

local function indentPrint(text)
	local curx,cury = term.getCursorPos()
	term.write(text)
	term.setCursorPos(curx,cury+1)
end

With this in place, you'd then re-write the rest of your script to just:

term.setCursorPos(17,7)
indentPrint("+------------------+")
indentPrint("|                  |")
indentPrint("+------------------+")
Spexiono #4
Posted 22 February 2014 - 06:12 PM
local function indentPrint(text)
	local curx,cury = term.getCursorPos()
	term.write(text)
	term.setCursorPos(curx,cury+1)
end
Thanks, this has worked perfectly, :)/>