module Kitcat::TerminalWidthCalculator

Public Class Methods

calculate() click to toggle source
# File lib/kitcat/terminal_width_calculator.rb, line 4
def calculate
  default_width = 80

  term_width = calculate_term_width

  term_width > 0 ? term_width : default_width
end

Private Class Methods

calculate_term_width() click to toggle source
# File lib/kitcat/terminal_width_calculator.rb, line 14
def calculate_term_width
  if ENV['COLUMNS'] =~ /^\d+$/
    ENV['COLUMNS'].to_i
  elsif tput_case?
    `tput cols`.to_i
  elsif stty_case?
    `stty size`.scan(/\d+/).map(&:to_i)[1]
  end
rescue
  0
end
shell_command_exists?(command) click to toggle source
# File lib/kitcat/terminal_width_calculator.rb, line 34
def shell_command_exists?(command)
  ENV['PATH'].split(File::PATH_SEPARATOR).any? { |d| File.exist? File.join(d, command) }
end
stty_case?() click to toggle source
# File lib/kitcat/terminal_width_calculator.rb, line 30
def stty_case?
  STDIN.tty? && shell_command_exists?('stty')
end
tput_case?() click to toggle source
# File lib/kitcat/terminal_width_calculator.rb, line 26
def tput_case?
  (RUBY_PLATFORM =~ /java/ || !STDIN.tty? && ENV['TERM']) && shell_command_exists?('tput')
end