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

Centering

Started by shipmaster4000, 11 August 2012 - 11:17 PM
shipmaster4000 #1
Posted 12 August 2012 - 01:17 AM
Hello, i was writing a count down. I wanted the number to be in the middle of the screen. I was using term.setCursorPos(1,1) and i was wondering what arguments would make this print out at the exact middle of the screen.
Cranium #2
Posted 12 August 2012 - 01:35 AM
You can get the terminal size by using this:

x, y = term.getSize()
Where it would list x and y as the maximum size of the screen.
It's easy to get the middle of the y value, by just doing term.setCursorPos(1,y/2). That will be centered top/down, but will be all the way to the left.
Then you can use math like string.len() to get the length of your string, which you can use to determine the center of the x value.
Use b = string.len("type here")/2
Then this: x = (x/2)-b
Then you can put both values into your x and y coordinates to place it exact center. You would just put term.setCursorPos(x,y/2) after all that math, and you're set!
KaoS #3
Posted 12 August 2012 - 01:34 PM
to summarise

function center(input)
term.setCursorPos({term.getSize()}[1]/2-#input/2,{term.getSize()}[2]/2)
print(input)
end