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)


31
32
33
# File 'lib/util.rb', line 31

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



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/util.rb', line 37

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

.match_key(abbrev, struct) ⇒ Object

let user lowercase and abbreviate key; abbreviation must match one key



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/util.rb', line 13

def self.match_key(abbrev, struct)
  abbrev = abbrev.downcase
  key = nil
  struct.keys.each { |fullkey|
    testkey = fullkey.downcase
    return fullkey if abbrev == testkey
    if fullkey.downcase.start_with?(abbrev)
      if (! key)
        key = fullkey
      else
        return nil
      end
    end
  }
  key
end

.parse_line(line, char = '=') ⇒ Object



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

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