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

[Mackan90096's tutorials] How to print text in the middle of computer

Started by Mackan90096, 15 May 2013 - 04:38 AM
Mackan90096 #1
Posted 15 May 2013 - 06:38 AM
Have you ever wanted text in the middle of the screen?

Well. Here's how you do it:


local w, h = term.getSize()
term.setCursorPos(math.floor(w-string.len("ComputerCraft"))/2, 2)
print("ComputerCraft")

Explanation:


local w, h = term.getSize() -- Declaring the width and height of the screen


term.setCursorPos(math.floor(w-string.len("ComputerCraft"))/2, 2) -- Setting the cursors position. string.len will get the lenght of the string, in this case ComputerCraft.


print("ComputerCraft") -- Printing the text.
theoriginalbit #2
Posted 15 May 2013 - 07:01 AM
I think you need to move the comments out of the code and onto the line above it. It will make it read nicer… also I feel that this is a better method of doing it…
Spoiler

local function centerPrint(msg, y)
  local w, h = term.getSize()
  term.setCursorPos(math.floor((w-#msg)/2) + (#msg % 2 == 0 and 1 or 0), y or h/2)
  print(msg)
end
this function does the following.
  • accept a message and a y position
  • get the terminal size
  • set the cursor position
  • — X position is half of the screen width minus the text length, plus 1 if the message is even length or 0 if it is odd length (this is because even length strings look like they are centered when 1 pixel to the right) — Y position is value supplied or half screen height when not provided
  • draw the text
H4X0RZ #3
Posted 15 May 2013 - 08:23 AM
I think you need to move the comments out of the code and onto the line above it. It will make it read nicer… also I feel that this is a better method of doing it…
Spoiler

local function centerPrint(msg, y)
  local w, h = term.getSize()
  term.setCursorPos(math.floor((w-#msg)/2) + (#msg % 2 == 0 and 1 or 0), y or sh/2)
  print(msg)
end
this function does the following.
  • accept a message and a y position
  • get the terminal size
  • set the cursor position
  • — X position is half of the screen width minus the text length, plus 1 if the message is even length or 0 if it is odd length (this is because even length strings look like they are centered when 1 pixel to the right) — Y position is value supplied or half screen height when not provided
  • draw the text

Where is sh defined?
theoriginalbit #4
Posted 15 May 2013 - 10:43 AM
Where is sh defined?
Whoops, I must have bumped s, its meant to be just h. fixed it. thanks.
Dave-ee Jones #5
Posted 16 May 2013 - 04:55 AM
I think you need to move the comments out of the code and onto the line above it. It will make it read nicer… also I feel that this is a better method of doing it…
Spoiler

local function centerPrint(msg, y)
  local w, h = term.getSize()
  term.setCursorPos(math.floor((w-#msg)/2) + (#msg % 2 == 0 and 1 or 0), y or h/2)
  print(msg)
end
this function does the following.
  • accept a message and a y position
  • get the terminal size
  • set the cursor position
  • — X position is half of the screen width minus the text length, plus 1 if the message is even length or 0 if it is odd length (this is because even length strings look like they are centered when 1 pixel to the right) — Y position is value supplied or half screen height when not provided
  • draw the text

Or you could just forget all that and just do:

function printCentered(text)
w, h = term.getSize()
x, y = term.getCursorPos()
x = math.max(math.floor((w / 2) - (#text / 2)), y)
term.setCursorPos(x,y)
print(text)
end
And you wouldn't have to do the 'y' argument.
theoriginalbit #6
Posted 16 May 2013 - 05:16 AM
Or you could just forget all that and just do:

function printCentered(text)
w, h = term.getSize()
x, y = term.getCursorPos()
x = math.max(math.floor((w / 2) - (#text / 2)), y)
term.setCursorPos(x,y)
print(text)
end
And you wouldn't have to do the 'y' argument.
You're missing the most important part that I was posting about, the centre for the x that takes into account for the even length strings, so that they print in the visual centre.
andyspam #7
Posted 18 May 2013 - 02:53 AM
thanks.
Espen #8
Posted 18 May 2013 - 03:41 AM
And you wouldn't have to do the 'y' argument.

You're missing the most important part that I was posting about, the centre for the x that takes into account for the even length strings, so that they print in the visual centre.

@AutoLocK:
Also, he didn't add y as a parameter because he had to, but because he wanted to provide a choice.
With his code you don't have to provide a y coordinate, you can.
If you look closely he sets the coordinate as such:
y or h/2

That means you can print the text centered at any y coordinate you like
or you don't provide a y coordinate and then the code will print it in the vertical center (h/2).
So what you did was not fixing a bug, but removing a feature. And he did explain that in the description of his code as well. ^_^/>