Sha256: 3f22b7b0b58807e7a8365fb57ac68b82686f384f5fcfac9b1bc3a525a6a49a73

Contents?: true

Size: 1.73 KB

Versions: 4

Compression:

Stored size: 1.73 KB

Contents

class   ProgressBar
module  Calculators
class   Length
  attr_reader   :length_override
  attr_accessor :current_length

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

  def length
    current_length || reset_length
  end

  def length_changed?
    previous_length     = current_length
    self.current_length = calculate_length

    previous_length != current_length
  end

  def calculate_length
    length_override || terminal_width || 80
  end

  def reset_length
    self.current_length = calculate_length
  end

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

  private

  # This code was copied and modified from Rake, available under MIT-LICENSE
  # Copyright (c) 2003, 2004 Jim Weirich
  def terminal_width
    return 80 unless unix?

    result = dynamic_width
    (result < 20) ? 80 : result
  rescue
    80
  end

  # rubocop:disable Lint/DuplicateMethods
  begin
    require 'io/console'

    def dynamic_width
      if IO.console
        dynamic_width_via_io_object
      else
        dynamic_width_via_system_calls
      end
    end
  rescue LoadError
    def dynamic_width
      dynamic_width_via_system_calls
    end
  end

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

  def dynamic_width_via_system_calls
    dynamic_width_stty.nonzero? || dynamic_width_tput
  end

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

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

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

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
ruby-progressbar-1.8.3 lib/ruby-progressbar/calculators/length.rb
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/ruby-progressbar-1.8.1/lib/ruby-progressbar/calculators/length.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/ruby-progressbar-1.8.1/lib/ruby-progressbar/calculators/length.rb
ruby-progressbar-1.8.1 lib/ruby-progressbar/calculators/length.rb