class ProgressBar::Calculators::Length

def calculate_length

def calculate_length
  length_override || terminal_width || 80
end

def dynamic_width

def dynamic_width
  if output && output.tty? && output.respond_to?(:winsize)
    dynamic_width_via_output_stream_object
  elsif IO.console
    dynamic_width_via_io_object
  else
    dynamic_width_via_system_calls
  end
end

def dynamic_width

def dynamic_width
  dynamic_width_via_system_calls
end

def dynamic_width_stty

def dynamic_width_stty
  `stty size 2>/dev/null`.split[1].to_i
end

def dynamic_width_tput

def dynamic_width_tput
  `tput cols 2>/dev/null`.to_i
end

def dynamic_width_via_io_object

def dynamic_width_via_io_object
  _rows, columns = IO.console.winsize
  columns
end

def dynamic_width_via_output_stream_object

def dynamic_width_via_output_stream_object
  _rows, columns = output.winsize
  columns
end

def dynamic_width_via_system_calls

def dynamic_width_via_system_calls
  dynamic_width_stty.nonzero? || dynamic_width_tput
end

def initialize(options = {})

def initialize(options = {})
  self.length_override = options[:length]
  self.output          = options[:output]
  self.current_length  = nil
end

def length

def length
  current_length || reset_length
end

def length_changed?

def length_changed?
  previous_length     = current_length
  self.current_length = calculate_length
  previous_length != current_length
end

def length_override=(other)

def length_override=(other)
  @length_override ||= ENV['RUBY_PROGRESS_BAR_LENGTH'] || other
  @length_override = @length_override.to_i if @length_override
end

def reset_length

def reset_length
  self.current_length = calculate_length
end

def terminal_width

rubocop:disable Style/RescueStandardError
Copyright (c) 2003, 2004 Jim Weirich
This code was copied and modified from Rake, available under MIT-LICENSE
def terminal_width
  return 80 unless unix?
  result = dynamic_width
  (result < 20) ? 80 : result
rescue
  80
end

def unix?

def unix?
  RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i
end