Jump to content




How to center text?


14 replies to this topic

#1 ComputerCraftFan11

  • Members
  • 771 posts
  • LocationHawaii

Posted 05 March 2012 - 01:14 AM

How can I put text in the center of the screen?

#2 Casper7526

    OG of CC

  • Members
  • 362 posts

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

#3 ComputerCraftFan11

  • Members
  • 771 posts
  • LocationHawaii

Posted 05 March 2012 - 01:20 AM

View PostCasper7526, on 05 March 2012 - 01:18 AM, said:

local 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:/>
that returns the number 16 when i try it on a turtle

#4 Casper7526

    OG of CC

  • Members
  • 362 posts

Posted 05 March 2012 - 01:22 AM

I derped a bit and edited my post, try again now.

#5 ComputerCraftFan11

  • Members
  • 771 posts
  • LocationHawaii

Posted 05 March 2012 - 01:24 AM

same

#6 Casper7526

    OG of CC

  • Members
  • 362 posts

Posted 05 March 2012 - 01:27 AM

See thats what I get for using some java with lua


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...

#7 ComputerCraftFan11

  • Members
  • 771 posts
  • LocationHawaii

Posted 05 March 2012 - 01:28 AM

View PostCasper7526, on 05 March 2012 - 01:27 AM, said:

See thats what I get for using some java with lua


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...
thanks, it works

#8 ComputerCraftFan11

  • Members
  • 771 posts
  • LocationHawaii

Posted 05 March 2012 - 01:31 AM

how can i add more text but move it down?

(edit: nvm i figured it out)

#9 haskell911

  • New Members
  • 8 posts

Posted 08 July 2012 - 10:37 AM

View PostComputerCraftFan11, on 05 March 2012 - 01:28 AM, said:

View PostCasper7526, on 05 March 2012 - 01:27 AM, said:

See thats what I get for using some java with lua


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...
thanks, it works
how to add more lines of text

#10 nateracecar5

  • Members
  • 94 posts

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)

#11 Mads

  • Members
  • 604 posts
  • LocationCopenhagen, Denmark

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


#12 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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)


#13 ti07shadow

  • Members
  • 4 posts

Posted 11 January 2013 - 08:31 PM

[EDIT]
Nevermind I found out why.

#14 hron84

  • Members
  • 7 posts

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.

#15 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users