Sha256: 05c8a3e508276afbe7812606a5929fe019e1b82023744b4a2394a1b7b4e4b461

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

module Fortschritt
  class Meter
    attr_accessor :total, :done, :updated_at, :average_seconds, :silent

    def initialize(total, silent: false)
      @total           = total
      @done            = 0
      @updated_at      = Time.now
      @average_seconds = 0
      @started_at      = Time.now
      @silent          = silent
    end

    def increment
      @_now            = Time.now
      elapsed_seconds  = @_now - updated_at
      @average_seconds = calculate_average_seconds(elapsed_seconds)
      @updated_at      = @_now
      @done           += 1
      print! unless @silent
    end

    def completed?
      done >= total
    end

    def remaining_seconds
      remaining * average_seconds
    end

    def remaining
      [total - done, 0].max
    end

    def calculate_average_seconds(value)
      average_seconds.zero? and return value
      ((average_seconds * done) + value) / (done + 1)
    end

    def total_elapsed_seconds
      Time.now - @started_at
    end

    def print!
      Fortschritt.printer.print(self)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fortschritt-0.2.5 lib/fortschritt/meter.rb
fortschritt-0.2.4 lib/fortschritt/meter.rb
fortschritt-0.2.3 lib/fortschritt/meter.rb