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

[REQUEST] Print To The Center API

Started by djblocksaway, 10 April 2012 - 01:01 PM
djblocksaway #1
Posted 10 April 2012 - 03:01 PM
can someone please make an api so when you use

PrintCenter "BLAH"


it will print to the center of the screen i have tried to make an api that does this but didnt succeed so if someone successfully makes this pleease tell me :P/>/>
cant_delete_account #2
Posted 10 April 2012 - 05:39 PM
Just mess around with:

local w,h = term.getSize()
local text = "SomeRandomString"
term.setCursorPos(w-20,h-10)
print(text)
term.setCursorPos(1,1)
Change how much it's taking off of the width and height until you get it centered.
MysticT #3
Posted 10 April 2012 - 06:04 PM
Just mess around with:

local w,h = term.getSize()
local text = "SomeRandomString"
term.setCursorPos(w-20,h-10)
print(text)
term.setCursorPos(1,1)
Change how much it's taking off of the width and height until you get it centered.
It's easier to take the lenght of the screen to make it always center, so you don't have to try and change the value until it gets centered.
Try something like this:

function PrintCentered(sText)
    local w, h = term.getSize()
    local x, y = term.getCursorPos()
    x = math.max(math.floor((w / 2) - (#sText / 2)), 0)
    term.setCursorPos(x, y)
    print(sText)
end
This prints the text centered, but only the first line. So if you give it a string longer than the screen width, it prints like the normal print.
djblocksaway #4
Posted 11 April 2012 - 01:26 AM
Just mess around with:

local w,h = term.getSize()
local text = "SomeRandomString"
term.setCursorPos(w-20,h-10)
print(text)
term.setCursorPos(1,1)
Change how much it's taking off of the width and height until you get it centered.
It's easier to take the lenght of the screen to make it always center, so you don't have to try and change the value until it gets centered.
Try something like this:

function PrintCentered(sText)
	local w, h = term.getSize()
	local x, y = term.getCursorPos()
	x = math.max(math.floor((w / 2) - (#sText / 2)), 0)
	term.setCursorPos(x, y)
	print(sText)
end
This prints the text centered, but only the first line. So if you give it a string longer than the screen width, it prints like the normal print.
here is what i have so far


local function PrintCenter(msg)
local msgLen = string.len(msg)
local screenWidth,_ = term.getSize()
local xCoords = tonumber(math.ceil((screenWidth / 2) - (msgLen / 2)))
local _,termY = term.getCursorPos()
term.setCursorPos(xCoords,termY)
print(msg)
return xCoords
end
Advert #5
Posted 11 April 2012 - 04:16 AM
You can already create this yourself, so it is moot implementing it in an API.
djblocksaway #6
Posted 11 April 2012 - 10:24 AM
dont worry i made the API :P/>/>
Heres the api file
(Just add this to a txt document named center and than remove the .txt and place in .minecraft/mods/computercraft/lua/apis and on computers use center.PrintCenter " " In your code)


CODE

function PrintCenter(msg)
msgLen = string.len(msg)
screenWidth,_ = term.getSize()
xCoords = tonumber(math.ceil((screenWidth / 2) - (msgLen / 2)))
_,termY = term.getCursorPos()
term.setCursorPos(xCoords,termY)
print(msg)
return xCoords
end
AKman1984 #7
Posted 11 August 2012 - 01:38 AM
Just mess around with:
 local w,h = term.getSize() local text = "SomeRandomString" term.setCursorPos(w-20,h-10) print(text) term.setCursorPos(1,1) 
Change how much it's taking off of the width and height until you get it centered.
It's easier to take the lenght of the screen to make it always center, so you don't have to try and change the value until it gets centered. Try something like this:
 function PrintCentered(sText) local w, h = term.getSize() local x, y = term.getCursorPos() x = math.max(math.floor((w / 2) - (#sText / 2)), 0) term.setCursorPos(x, y) print(sText) end 
This prints the text centered, but only the first line. So if you give it a string longer than the screen width, it prints like the normal print.

Awsome code works great. Two questions how can have the print out move a bit to the left or right. Also how can I get this to show off a monitor try some things did not work.
AKman1984 #8
Posted 11 August 2012 - 06:30 PM
Just mess around with:
 local w,h = term.getSize() local text = "SomeRandomString" term.setCursorPos(w-20,h-10) print(text) term.setCursorPos(1,1) 
Change how much it's taking off of the width and height until you get it centered.
It's easier to take the lenght of the screen to make it always center, so you don't have to try and change the value until it gets centered. Try something like this:
 function PrintCentered(sText) local w, h = term.getSize() local x, y = term.getCursorPos() x = math.max(math.floor((w / 2) - (#sText / 2)), 0) term.setCursorPos(x, y) print(sText) end 
This prints the text centered, but only the first line. So if you give it a string longer than the screen width, it prints like the normal print.

Awsome code works great. Two questions how can have the print out move a bit to the left or right. Also how can I get this to show off a monitor try some things did not work.

Never mind I got it.


function PrintCentered(sText)
local w, h = mon.getSize()
local x, y = mon.getCursorPos()
x = math.max(math.floor((w / 2) - (#sText / 2)), 0)
mon.setCursorPos(x, y)
mon.write(sText)
end
dimitriye98 #9
Posted 12 August 2012 - 06:10 AM
Mods, now that this is solved, shouldn't it be moved to general or programs?
snoble2022 #10
Posted 29 December 2012 - 12:21 PM
Or you could just do this


function printCen(msg)
  w, h = term.getSize()
  term.setCursorPos(w/2, h/2)
  write(msg)
end

Always prints in the center of the screen
If you want to print in the center of a line then

function printCen(msg, ycord)
  w, h = term.getSize()
  term.setCursorPos(w/2, ycord)
  write(msg)
end
immibis #11
Posted 29 December 2012 - 12:23 PM
Or you could just do this


function printCen(msg)
  w, h = term.getSize()
  term.setCursorPos(w/2, h/2)
  write(msg)
end

Always prints in the center of the screen
If you want to print in the center of a line then

function printCen(msg, ycord)
  w, h = term.getSize()
  term.setCursorPos(w/2, ycord)
  write(msg)
end
If you'd actually tried either of those, you'd know they don't work properly as they always print the message to the right of the centre.
You should also have seen that this thread is 4 months old.
Lyqyd #12
Posted 29 December 2012 - 02:28 PM
Locked.