Class: Util

Inherits:
Object
  • Object
show all
Defined in:
lib/util.rb

Class Method Summary collapse

Class Method Details

.command_exists?(command) ⇒ Boolean

Determines if a shell command exists by searching for it in ENV.

Returns:

  • (Boolean)


3
4
5
# File 'lib/util.rb', line 3

def self.command_exists?(command)
  ENV['PATH'].split(File::PATH_SEPARATOR).any? {|d| File.exists? File.join(d, command) }
end

.detect_terminal_size ⇒ Object

Returns [width, height] of terminal when detected, nil if not detected. Think of this as a simpler version of Highline's Highline::SystemExtensions.terminal_size()



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/util.rb', line 9

def self.detect_terminal_size
  if (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/)
    [ENV['COLUMNS'].to_i, ENV['LINES'].to_i]
  elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && command_exists?('tput')
    [`tput cols`.to_i, `tput lines`.to_i]
  elsif STDIN.tty? && command_exists?('stty')
    `stty size`.scan(/\d+/).map { |s| s.to_i }.reverse
  else
    nil
  end
rescue
  nil
end