Class to encapsulate stuff about the terminal. This is useful to application developers as a canonical means to get information about the user's current terminal configuraiton. GLI uses this to determine the number of columns to use when printing to the screen.

To access it, use Terminal.instance. This is a singleton mostly to facilitate testing, but it seems reasonable enough, since there's only one terminal in effect

Example:

Terminal.instance.size[0] # => columns in the terminal
Terminal.default_size = [128,24] # => change default when we can't figure it out
raise "no ls?!?!?" unless Terminal.instance.command_exists?("ls")
Methods
C
D
I
S
Constants
SIZE_DETERMINERS = [ [ lambda { (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/) }, lambda { [ENV['COLUMNS'].to_i, ENV['LINES'].to_i] } ], [ lambda { (jruby? || (!STDIN.tty? && ENV['TERM'])) && command_exists?('tput') }, lambda { [run_command('tput cols').to_i, run_command('tput lines').to_i] } ], [ lambda { (solaris? && STDIN.tty? && command_exists?('stty')) }, lambda { run_command('stty').split("\n")[1].scan(/\d+/)[0..1].map { |size_element| size_element.to_i }.reverse } ], [ lambda { STDIN.tty? && command_exists?('stty') }, lambda { run_command('stty size').scan(/\d+/).map { |size_element| size_element.to_i }.reverse } ], [ lambda { true }, lambda { Terminal.default_size }, ], ]
 
Class Public methods
command_exists?(command)

Returns true if the given command exists on this system

command

The command, as a String, to check for, without any path information.

# File lib/gli/terminal.rb, line 45
def self.command_exists?(command)
  ENV['PATH'].split(File::PATH_SEPARATOR).any? {|dir| File.exist? File.join(dir, command) }
end
default_size()

Get the default size of the terminal when we can't figure it out

Returns an array of int [cols,rows]

# File lib/gli/terminal.rb, line 22
def self.default_size
  @@default_size
end
default_size=(size)

Set the default size of the terminal to use when we can't figure it out

size

array of two int [cols,rows]

# File lib/gli/terminal.rb, line 29
def self.default_size=(size)
  @@default_size = size
end
instance()

Provide access to the shared instance.

# File lib/gli/terminal.rb, line 34
def self.instance; @@instance ||= Terminal.new; end
Instance Public methods
command_exists?(command)
# File lib/gli/terminal.rb, line 49
def command_exists?(command)
  self.class.command_exists?(command)
end
size()

Get the size of the current terminal. Ripped from hirb

Returns an Array of size two Ints representing the terminal width and height

# File lib/gli/terminal.rb, line 80
def size
  SIZE_DETERMINERS.each do |predicate, get_size|
    next unless predicate.call
    size = get_size.call
    return size unless size == [0, 0]
  end
rescue Exception => ex
  raise ex if @unsafe
  Terminal.default_size
end