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

Code to center text

Started by Matthewaj, 25 May 2012 - 04:47 PM
Matthewaj #1
Posted 25 May 2012 - 06:47 PM
What would the code be to center the text when you do print("")?
ComputerCraftFan11 #2
Posted 25 May 2012 - 07:36 PM

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

cPrint("Hello World!")

Original code from Casper
Andale #3
Posted 15 January 2013 - 06:52 PM
I combined this with code from rondouglas to get centered monitor text withOUT running a program onto the monitor.



function startMonitor()
   monitor = peripheral.wrap("[color=#ff0000]location[/color]")
   wm, hm = monitor.getSize()
end
startMonitor()

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

cwrite("Hello World!")  -- centers printed text to monitor

be SURE it says monitor.write(text) and not just write(text)

Must be used in that order. Print calls do not work on the monitor so it must be Write. (Far as I know)
remiX #4
Posted 16 January 2013 - 02:28 AM
Uhm, Why are you reviving this nearly 1 year old thread? (K, more like 7 months but still)
Andale #5
Posted 16 January 2013 - 12:45 PM
Cuz i haven't found one on how to center print to monitor and its relevant. You can either complain that people don't search enough or that they use old threads, not both. I choose to complain about people not searching and this was what iI found for centering text on terminals.
Orwell #6
Posted 16 January 2013 - 12:58 PM
Cuz i haven't found one on how to center print to monitor and its relevant. You can either complain that people don't search enough or that they use old threads, not both. I choose to complain about people not searching and this was what iI found for centering text on terminals.
This is about centering using print(). So it's less relevant to centering text on monitors than a couple of dozen of other snippets that do exactly this and should also show up on a search. Just saying.
remiX #7
Posted 16 January 2013 - 05:38 PM
Cuz i haven't found one on how to center print to monitor and its relevant. You can either complain that people don't search enough or that they use old threads, not both. I choose to complain about people not searching and this was what iI found for centering text on terminals.

Then search for this, make a new thread and quote a post from this?
Mvf314 #8
Posted 30 January 2013 - 05:47 AM
You can use this:


x,y = term.getSize()
randomYPos = math.random(1,y) --Not neccecary, I use this to define a random y position
halfX = x / 2

function centralWrite(text)
  length = string.len(text)
  halflength = length / 2
  term.setCursorPos(halfX - halflength)
  print(text)
end
ikke009 #9
Posted 30 January 2013 - 05:49 AM
yeah why don't we re-revive this thread for lolz…