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

How to center text?

Started by ComputerCraftFan11, 05 March 2012 - 12:14 AM
ComputerCraftFan11 #1
Posted 05 March 2012 - 01:14 AM
How can I put text in the center of the screen?
Casper7526 #2
Posted 05 March 2012 - 01:18 AM
local function centerText(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.round((x / 2) - (text:len() / 2)), y2)
write(text)
end

centerText("Hello World")

Wrote off the top of my head, but you get the idea :unsure:/>/>

Edit: derp
ComputerCraftFan11 #3
Posted 05 March 2012 - 01:20 AM
local function centerText(text)
local x,y = term.getSize()
term.setCursorPos(math.round((x / 2) - (text:len() / 2)), y)
term.write(text)
end

centerText("Hello World")

Wrote off the top of my head, but you get the idea :unsure:/>/>
that returns the number 16 when i try it on a turtle
Casper7526 #4
Posted 05 March 2012 - 01:22 AM
I derped a bit and edited my post, try again now.
ComputerCraftFan11 #5
Posted 05 March 2012 - 01:24 AM
same
Casper7526 #6
Posted 05 March 2012 - 01:27 AM
See thats what I get for using some java with lua



local function centerText(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
write(text)
end

centerText("Hello World")

That works for me…
ComputerCraftFan11 #7
Posted 05 March 2012 - 01:28 AM
See thats what I get for using some java with lua



local function centerText(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
write(text)
end

centerText("Hello World")

That works for me…
thanks, it works
ComputerCraftFan11 #8
Posted 05 March 2012 - 01:31 AM
how can i add more text but move it down?

(edit: nvm i figured it out)
haskell911 #9
Posted 08 July 2012 - 11:37 AM
See thats what I get for using some java with lua



local function centerText(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
write(text)
end

centerText("Hello World")

That works for me…
thanks, it works
how to add more lines of text
nateracecar5 #10
Posted 08 January 2013 - 03:52 AM
I was wondering how to center text to the MIDDLE of the screen if you don't mind. (When I say middle I mean the middle of both x and y)
Mads #11
Posted 08 January 2013 - 04:08 AM

local function centerTextXY(text)
    local w, h = term.getSize()
    term.setCursorPos(math.floor(w / 2 - text:len() / 2 + .5), math.floor(h / 2 + .5))
    io.write(text)
end
remiX #12
Posted 08 January 2013 - 06:33 AM
Why is everyone using text:len() why not just #text? :P/>


w, h = term.getSize()
term.setCursorPos(math.floor(w - #text)/2, yPos)
ti07shadow #13
Posted 11 January 2013 - 08:31 PM
[EDIT]
Nevermind I found out why.
hron84 #14
Posted 05 November 2013 - 09:41 PM
How do you solve if you have to update text what is centerized? term.clear() is not a best solution because sometimes screen start blinking if called APIs returns too slow.
Bomb Bloke #15
Posted 06 November 2013 - 01:53 AM
That's getting away from the original question a bit - the best way to avoid blinking varies depending on what you want to update, whether the length varies per update, and what else you've got on the screen around it. Whether you're centering the text is really one of the least important factors.

I'd recommend posting a new thread that explains exactly what it is you're trying to do, along with the code you're currently using to do it.