class ProgressBar::Calculators::Length

Attributes

current_length[RW]
length_override[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/ruby-progressbar/calculators/length.rb, line 7
def initialize(options = {})
  self.length_override = options[:length]
  self.current_length  = nil
end

Public Instance Methods

calculate_length() click to toggle source
# File lib/ruby-progressbar/calculators/length.rb, line 23
def calculate_length
  length_override || terminal_width || 80
end
length() click to toggle source
# File lib/ruby-progressbar/calculators/length.rb, line 12
def length
  current_length || reset_length
end
length_changed?() click to toggle source
# File lib/ruby-progressbar/calculators/length.rb, line 16
def length_changed?
  previous_length     = current_length
  self.current_length = calculate_length

  previous_length != current_length
end
length_override=(other) click to toggle source
# File lib/ruby-progressbar/calculators/length.rb, line 31
def length_override=(other)
  @length_override ||= ENV['RUBY_PROGRESS_BAR_LENGTH'] || other
  @length_override = @length_override.to_i if @length_override
end
reset_length() click to toggle source
# File lib/ruby-progressbar/calculators/length.rb, line 27
def reset_length
  self.current_length = calculate_length
end

Private Instance Methods

dynamic_width() click to toggle source
# File lib/ruby-progressbar/calculators/length.rb, line 53
def dynamic_width
  if IO.console
    dynamic_width_via_io_object
  else
    dynamic_width_via_system_calls
  end
end
dynamic_width_stty() click to toggle source
# File lib/ruby-progressbar/calculators/length.rb, line 75
def dynamic_width_stty
  %x`stty size 2>/dev/null`.split[1].to_i
end
dynamic_width_tput() click to toggle source
# File lib/ruby-progressbar/calculators/length.rb, line 79
def dynamic_width_tput
  %x`tput cols 2>/dev/null`.to_i
end
dynamic_width_via_io_object() click to toggle source
# File lib/ruby-progressbar/calculators/length.rb, line 66
def dynamic_width_via_io_object
  _rows, columns = IO.console.winsize
  columns
end
dynamic_width_via_system_calls() click to toggle source
# File lib/ruby-progressbar/calculators/length.rb, line 71
def dynamic_width_via_system_calls
  dynamic_width_stty.nonzero? || dynamic_width_tput
end
terminal_width() click to toggle source

This code was copied and modified from Rake, available under MIT-LICENSE Copyright © 2003, 2004 Jim Weirich

# File lib/ruby-progressbar/calculators/length.rb, line 40
def terminal_width
  return 80 unless unix?

  result = dynamic_width
  (result < 20) ? 80 : result
rescue
  80
end
unix?() click to toggle source
# File lib/ruby-progressbar/calculators/length.rb, line 83
def unix?
  RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i
end