Sha256: f8e4d23eb2f68f11f60dd4b999978410b059d9abb66f5cbe396f5c044c368373

Contents?: true

Size: 1.62 KB

Versions: 29

Compression:

Stored size: 1.62 KB

Contents

require 'rbbt/util/log'
require 'rbbt/util/log/progress/util'
require 'rbbt/util/log/progress/report'
module Log
  class ProgressBar

    attr_accessor :max, :ticks, :frequency, :depth, :desc, :file, :bytes
    def initialize(max = nil, options = {})
      options = Misc.add_defaults options, :depth => 0, :num_reports => 100, :desc => "Progress", :io => STDERR, :severity => Log.severity
      depth, num_reports, desc, io, severity, file, bytes = Misc.process_options options, :depth, :num_reports, :desc, :io, :severity, :file, :bytes

      @max = max
      @ticks = 0
      @frequency = 2
      @last_time = nil
      @last_count = nil
      @last_percent = nil
      @depth = depth
      @desc = desc.nil? ? nil : desc.gsub(/\n/,' ')
      @file = file
    end

    def percent
      return 0 if @ticks == 0
      (@ticks * 100) / @max
    end

    def init
      @start = @last_time = Time.now
      @last_count = 0
      report
    end

    def tick(step = 1)
      return if ENV["RBBT_NO_PROGRESS"] == "true"
      @ticks += step

      begin
        time = Time.now
        if @last_time.nil?
          @last_time = time
          @last_count = @ticks
          @start = time
          return
        end

        diff = time - @last_time
        report and return if diff > @frequency
        return unless max and max > 0

        percent = self.percent
        if @last_percent.nil?
          @last_percent = percent
          return
        end
        report and return if percent > @last_percent and diff > 0.3
      rescue Exception
        Log.warn "Exception during report: " << $!.message
        Log.exception $!
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
rbbt-util-5.17.70 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.69 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.68 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.67 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.66 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.65 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.64 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.63 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.62 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.61 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.60 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.59 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.58 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.57 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.56 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.55 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.54 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.53 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.52 lib/rbbt/util/log/progress.rb
rbbt-util-5.17.51 lib/rbbt/util/log/progress.rb