class CutePrint::TermWidth::Detected

A terminal width that tries to determine the terminal width automatically. @api private

Constants

FALLBACK_WIDTH

Attributes

width[R]

Public Class Methods

new() click to toggle source
# File lib/cute_print/term_width/detected.rb, line 11
def initialize
  @width = detect_width
end

Public Instance Methods

visible() click to toggle source
# File lib/cute_print/term_width/detected.rb, line 15
def visible
  :detect
end

Private Instance Methods

detect_terminal_size() click to toggle source

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

# File lib/cute_print/term_width/detected.rb, line 51
def 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
detect_width() click to toggle source
# File lib/cute_print/term_width/detected.rb, line 23
def detect_width
  return width_override_for_tests if width_override_for_tests
  width, _height = detect_terminal_size
  # Hirb returns nil if it can't determine the terminal width.
  # Program run in Emacs have an apparent terminal width of "0".
  width = FALLBACK_WIDTH if width.nil? || width == 0
  width
end
width_override_for_tests() click to toggle source
# File lib/cute_print/term_width/detected.rb, line 32
def width_override_for_tests
  n = ENV['CUTE_PRINT_TERM_WIDTH']
  n && Integer(n)
end