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

Centering Text

Started by flaminsnowman99, 24 July 2012 - 01:12 AM
flaminsnowman99 #1
Posted 24 July 2012 - 03:12 AM
How do i center text on a screen? I've done this before i just cant remember how i did it :)/>/>
Noodle #2
Posted 24 July 2012 - 03:24 AM
Hmm.. Exact center would be

text = "Blah"
x, y = term.getSize()
term.setCursorPos(x/2/string.len(text), y/2)
print(text)
DireEpidemic #3
Posted 25 July 2012 - 11:52 AM
Would you have to retype that for each word or would a sentance work?
Cranium #4
Posted 25 July 2012 - 07:40 PM
Since "blah" is defining a string, anything inside that "" should be printed, I think. I'm not a pro, and would love to know as well, but that's just my guess.
sjonky #5
Posted 25 July 2012 - 07:55 PM
This is a pretty nice function which does the job :)/>/>
I did not make it.


Made by Nitrogenfingers

 
local w,h = term.getSize()
 
function printCentered(msg, height)
term.setCursorPos(w/2 - #msg/2, height)
term.write(msg)
end
Noodle #6
Posted 26 July 2012 - 06:45 PM
@sjonky
Height won't work, you placed it at the Y coord which means it should be at that position.
x, y = term.getSize()
function cPrint(msg)
  term.setCursorPos(x/2/string.len(msg), y/2)
  write(msg)
end
That should be correct to do exact center.
Cranium #7
Posted 26 July 2012 - 08:52 PM
What is the string.len() command? I never heard of that before now….
Noodle #8
Posted 26 July 2012 - 09:58 PM
Well string.len() returns the length of a string (string.len("String")).
Its within the Lua string library.
LINK
Cranium #9
Posted 27 July 2012 - 01:00 AM
oh, I was looking in the CC wiki, that's why I never saw that. I have been getting accustomed to CC specific code, but lua code is still lightyears out of my full grasp