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)


13
14
15
# File 'lib/util.rb', line 13

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()



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/util.rb', line 19

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

.parse_line(line) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/util.rb', line 3

def self.parse_line(line)
  idx = line.index('=')
  if (idx)
    return line[0...idx], line[idx+1..-1]
  else
    line
  end
end