Class: Wrong::Terminal

Inherits:
Object show all
Defined in:
lib/wrong/terminal.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)


37
38
39
# File 'lib/wrong/terminal.rb', line 37

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

.sizeObject

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() Lifted from github.com/cldwalker/hirb/blob/master/lib/hirb/util.rb#L59

See also stackoverflow.com/questions/2068859/how-to-get-the-width-of-terminal-window-in-ruby

https://github.com/genki/ruby-terminfo/blob/master/lib/terminfo.rb
http://www.mkssoftware.com/docs/man1/stty.1.asp


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/wrong/terminal.rb', line 12

def self.size
  @@terminal_size ||= begin
    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
end

.widthObject



28
29
30
# File 'lib/wrong/terminal.rb', line 28

def self.width
  (@terminal_width ||= nil) || (size && size.first) || 80
end

.width=(forced_with) ⇒ Object



32
33
34
# File 'lib/wrong/terminal.rb', line 32

def self.width= forced_with
  @terminal_width = forced_with
end