class GLI::Terminal

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 ::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")

Constants

SIZE_DETERMINERS

Public Class Methods

command_exists?(command) click to toggle source

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() click to toggle source

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) click to toggle source

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() click to toggle source

Provide access to the shared instance.

# File lib/gli/terminal.rb, line 34
def self.instance; @@instance ||= Terminal.new; end

Private Class Methods

jruby?() click to toggle source

True if we are JRuby; exposed to allow for testing

# File lib/gli/terminal.rb, line 99
def self.jruby?; RUBY_PLATFORM =~ /java/; end
run_command(command) click to toggle source

Runs a command using backticks. Extracted to allow for testing

# File lib/gli/terminal.rb, line 94
def self.run_command(command)
  `#{command}`
end
solaris?() click to toggle source

True if this is running under Solaris Sparc

# File lib/gli/terminal.rb, line 102
def self.solaris?; RUBY_PLATFORM =~ /solaris/; end

Public Instance Methods

command_exists?(command) click to toggle source
# File lib/gli/terminal.rb, line 49
def command_exists?(command)
  self.class.command_exists?(command)
end
size() click to toggle source

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