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

[LUA] Printing in center of monitor screen

Started by remiX, 20 October 2012 - 11:14 PM
remiX #1
Posted 21 October 2012 - 01:14 AM
Hey guys,

Can someone give me a code to write text in the exact middle of any screen size of monitors.
Espen #2
Posted 21 October 2012 - 01:37 AM

local function writeCentered( text )
  local x, y = term.getSize()
  local centerYPos = y / 2
  local centerXStartingPos = ( x - string.len(text) ) / 2
  term.setCursorPos( centerXStartingPos, centerYPos )
  write( text )
end

writeCentered("This should appear in the center.")

Although this would force every line into the middle Y position.
It might be better to limit the centering to the X axis only, and then decide on a per-use case on which Y coordinate the X-centered text should go.

EDIT: Another example where you can choose the Y-Axis:

local function writeCentered( text, y )
  local x = term.getSize()
  local centerXPos = ( x - string.len(text) ) / 2
  term.setCursorPos( centerXPos, y )
  write( text )
end

writeCentered("This should appear in the center of line 3.", 3)
Edited on 20 October 2012 - 11:44 PM
jag #3
Posted 21 October 2012 - 01:38 AM
Just play around with monitor.getSize() (requires peripheral) and string.len()!

Info from the interwebz:
Peripheral - Monitor (CC wiki)
String libary - string.len() (Lua-users wiki)

EDIT: Or just do as Espen says :/
remiX #4
Posted 21 October 2012 - 01:58 AM

local function writeCentered( text )
  local x, y = term.getSize()
  local centerYPos = y / 2
  local centerXStartingPos = ( x - string.len(text) ) / 2
  term.setCursorPos( centerXStartingPos, centerYPos )
  write( text )
end

writeCentered("This should appear in the center.")

Although this would force every line into the middle Y position.
It might be better to limit the centering to the X axis only, and then decide on a per-use case on which Y coordinate the X-centered text should go.

EDIT: Another example where you can choose the Y-Axis:

local function writeCentered( text, y )
  local x = term.getSize()
  local centerXPos = ( x - string.len(text) ) / 2
  term.setCursorPos( centerXPos, y )
  write( text )
end

writeCentered("This should appear in the center of line 3.", 3)

Sweet thanks :)/>/> I knew it had to do with term.getSize and the stringlength but I didn't know what to do :)/>/>

Just play around with monitor.getSize() (requires peripheral) and string.len()!

Info from the interwebz:
Peripheral - Monitor (CC wiki)
String libary - string.len() (Lua-users wiki)

EDIT: Or just do as Espen says :/

Haha, ninja'd :)/>/>

Another question: I'm trying to get ASCII art text to be printed on the monitor. Works fine - but it doesn't print backslashes [ ]? Why >_<
Orwell #5
Posted 21 October 2012 - 02:15 AM

local function writeCentered( text )
  local x, y = term.getSize()
  local centerYPos = y / 2
  local centerXStartingPos = ( x - string.len(text) ) / 2
  term.setCursorPos( centerXStartingPos, centerYPos )
  write( text )
end

writeCentered("This should appear in the center.")

Although this would force every line into the middle Y position.
It might be better to limit the centering to the X axis only, and then decide on a per-use case on which Y coordinate the X-centered text should go.

EDIT: Another example where you can choose the Y-Axis:

local function writeCentered( text, y )
  local x = term.getSize()
  local centerXPos = ( x - string.len(text) ) / 2
  term.setCursorPos( centerXPos, y )
  write( text )
end

writeCentered("This should appear in the center of line 3.", 3)

Sweet thanks :)/>/> I knew it had to do with term.getSize and the stringlength but I didn't know what to do :)/>/>

Just play around with monitor.getSize() (requires peripheral) and string.len()!

Info from the interwebz:
Peripheral - Monitor (CC wiki)
String libary - string.len() (Lua-users wiki)

EDIT: Or just do as Espen says :/

Haha, ninja'd :)/>/>

Another question: I'm trying to get ASCII art text to be printed on the monitor. Works fine - but it doesn't print backslashes [ ]? Why >_<

You need to escape them, this means that you'd use '' instead of ''. Or, you could use [[ ]] to denote a string instead of " ". (This is very practical for ascii-art, cause it's a multiline string)

local str = [[
( /)
(o.O)
(, ,)
]]
print(str)
remiX #6
Posted 21 October 2012 - 05:20 PM

local str = [[
( /)
(o.O)
(, ,)
]]
print(str)

I was trying to make mutliline strings starting with ([[.. fuuu. But thanks xD