2088 posts
                
                    
                        Location
                        South Africa
                    
                
             
            
                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.
                
             
         
        
        
            
            
                
                    
                
                715 posts
                
             
            
                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
                
             
         
        
        
            
            
                
                    
                
                521 posts
                
                    
                        Location
                        Stockholm, Sweden
                    
                
             
            
                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 :/
 
                
             
         
        
        
            
            
                
                    
                
                2088 posts
                
                    
                        Location
                        South Africa
                    
                
             
            
                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 >_<
 
                
             
         
        
        
            
            
                
                    
                
                1054 posts
                
             
            
                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)
 
                
             
         
        
        
            
            
                
                    
                
                2088 posts
                
                    
                        Location
                        South Africa
                    
                
             
            
                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