Jump to content




[REQUEST] Print To The Center API


11 replies to this topic

#1 djblocksaway

    Epic Coderz

  • New Members
  • 397 posts
  • LocationAustralia

Posted 10 April 2012 - 02: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/>

#2 thesbros

  • Members
  • 484 posts
  • LocationMy house. Where else?

Posted 10 April 2012 - 04: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.

#3 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 10 April 2012 - 05:04 PM

View Postthesbros, on 10 April 2012 - 04:39 PM, said:

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.

#4 djblocksaway

    Epic Coderz

  • New Members
  • 397 posts
  • LocationAustralia

Posted 11 April 2012 - 12:26 AM

View PostMysticT, on 10 April 2012 - 05:04 PM, said:

View Postthesbros, on 10 April 2012 - 04:39 PM, said:

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


#5 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 11 April 2012 - 03:16 AM

You can already create this yourself, so it is moot implementing it in an API.

#6 djblocksaway

    Epic Coderz

  • New Members
  • 397 posts
  • LocationAustralia

Posted 11 April 2012 - 09: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


#7 AKman1984

  • Members
  • 33 posts

Posted 11 August 2012 - 12:38 AM

View PostMysticT, on 10 April 2012 - 05:04 PM, said:

View Postthesbros, on 10 April 2012 - 04:39 PM, said:

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.

#8 AKman1984

  • Members
  • 33 posts

Posted 11 August 2012 - 05:30 PM

View PostAKman1984, on 11 August 2012 - 12:38 AM, said:

View PostMysticT, on 10 April 2012 - 05:04 PM, said:

View Postthesbros, on 10 April 2012 - 04:39 PM, said:

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

#9 dimitriye98

  • Members
  • 72 posts

Posted 12 August 2012 - 05:10 AM

Mods, now that this is solved, shouldn't it be moved to general or programs?

#10 snoble2022

  • Members
  • 100 posts
  • LocationSomewhere near the local computer store?

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


#11 immibis

    Lua God

  • Members
  • 1,017 posts
  • LocationWellington, New Zealand

Posted 29 December 2012 - 12:23 PM

View Postsnoble2022, on 29 December 2012 - 12:21 PM, said:

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.

#12 Lyqyd

    Lua Liquidator

  • Administrators
  • 6,398 posts

Posted 29 December 2012 - 02:28 PM

Locked.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users