This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
How to center text?
Started by ComputerCraftFan11, 05 March 2012 - 12:14 AMPosted 05 March 2012 - 01:14 AM
How can I put text in the center of the screen?
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
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
Posted 05 March 2012 - 01:20 AM
that returns the number 16 when i try it on a turtlelocal 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:/>/>
Posted 05 March 2012 - 01:22 AM
I derped a bit and edited my post, try again now.
Posted 05 March 2012 - 01:24 AM
same
Posted 05 March 2012 - 01:27 AM
See thats what I get for using some java with lua
That works for me…
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…
Posted 05 March 2012 - 01:28 AM
thanks, it worksSee thats what I get for using some java with lualocal 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…
Posted 05 March 2012 - 01:31 AM
how can i add more text but move it down?
(edit: nvm i figured it out)
(edit: nvm i figured it out)
Posted 08 July 2012 - 11:37 AM
how to add more lines of textthanks, it worksSee thats what I get for using some java with lualocal 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…
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)
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
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)
Posted 11 January 2013 - 08:31 PM
[EDIT]
Nevermind I found out why.
Nevermind I found out why.
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.
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.
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.