Sha256: 5305da132b8f13dd19abfbe979498c373c3bc519ea91ed638fc00b6876c1af8d

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module Collimator
  module ProgressBar
    @display_width = 100
    @max = 100
    @min = 0
    @method = :percent
    @step_size = 5
    @current = 0

    def self.set_parameters(params)
      @display_width = params[:display_width] if params.has_key?(:display_width)
      @max = params[:max] if params.has_key?(:max)
      @min = params[:min] if params.has_key?(:min)
      @method = params[:method] if params.has_key?(:method)
      @step_size = params[:step_size] if params.has_key?(:step_size)
    end

    def self.start(params = {})
      set_parameters params
      @current = @min
      put_current_progress
    end

    def self.increment
      @current += @step_size
      put_current_progress unless @current > @max
    end

    def self.complete
      clear
      puts " "*(@display_width + 2)
    end

    def self.clear
      print "\b"*(@display_width + 2)
    end

    private

    def self.put_current_progress
      clear
      STDOUT.flush
      print build_bar_string
      #puts build_bar_string
    end

    def self.build_bar_string
      units = @max - @min
      unit_width = @display_width/units
      percent_complete = @current / (units * 1.0)

      s = '-'*(percent_complete * @display_width) + ' '*((100-percent_complete) * @display_width)
      s1 = s[0..(@display_width/2 - 4)]
      s2 = s[(@display_width/2 + 3)..(@display_width - 1)]

      formatted_percent = "%0.1f" % (percent_complete * 100)

      s = '|' + s1 + "#{formatted_percent}%".center(6) + s2 + '|'
      #s = "#{units} - #{unit_width} - #{percent_complete} - #{@current}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
collimator-0.0.3 lib/collimator/progress_bar.rb