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

[Question] How to center something...

Started by dextermb, 12 December 2012 - 02:12 AM
dextermb #1
Posted 12 December 2012 - 03:12 AM
How would you go about centering something in a computer eg:


  print ("center these words")

How would you center the words in the middle of the computer screen?
ScruffyRules #2
Posted 12 December 2012 - 03:23 AM
i think theres something in textutils API that can help you with this!
dextermb #3
Posted 12 December 2012 - 03:27 AM
i think theres something in textutils API that can help you with this!

I am working on a script/API myself :/
I was just wondering if there is some standard lua code that i can use rather than installing a secondary API.
ScruffyRules #4
Posted 12 December 2012 - 03:45 AM
textutils is in LUA.
RyanISan #5
Posted 12 December 2012 - 05:07 AM
Come on, this is some pretty simple maths ;)/>

OK, here's a function that figures out what X coordinate to print a string at so that it's centred.


function findCentre(str)
    local centreX = term.getSize()
    centreX = (centreX - #str) / 2
    if centreX > 0 then
        return centreX+1 -- This +1 is not necessary, it's personal preference.
    else
        return 1
    end
end

Can you figure out how to display your string AT that X coordinate? HINT: http://computercraft...rm.setCursorPos

Note: A string with an even number of characters can't really be centred properly…this MAY set off your OCD as it did mine.
FUCKCOMPUTERCRAFT!"£ #6
Posted 12 December 2012 - 12:04 PM
Take a look at my some GUI functions I made.

http://pastebin.com/hLLjTfjz

If you don't understand what the functions do just pm me or ask here :)/>

With my funtions just do..

shell.run("GUI")
cPrint ("center these words")

Note the shell cmd may vary…
Cranium #7
Posted 12 December 2012 - 12:51 PM

local function writeC(string)
	local tX, tY = term.getSize()
	local b = string.len(string) / 2
	local x = (tX / 2) - b
	local y = tY / 2
	term.setCursorPos(x, y)
	write(string)
end

This function prints any string in the center of the screen.