Sha256: 7a5846ed5429e9d15c892c850d76954eb8d17e41c56093bc58343338ce430632

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

module MultiProgressBar
  # Works just like +Bar+, but displays the total of other bars.
  # TotalBar#inc and TotalBar#set don't work.
  class TotalBar < Bar
    # Create a new TotalBar.  +bars+ is an array of Bar objects, and defaults to
    # all existing bars.
    def initialize(title, bars = MultiProgressBar.bars.dup)
      @bars = bars

      @bars.each do |bar|
        bar.observe do
          update_total
        end
      end

      total_total = @bars.inject(0) { |sum, bar| sum + bar.total }
      super title, total_total
    end

    [:inc, :set].each do |name|
      define_method(name) do
        raise NoMethodError, "NoMethodError: private method `#{name}' called for #{self}"
      end
    end

    private
    def update_total
      total_current = @bars.inject(0) { |sum, bar| sum + bar.current }
      total_total   = @bars.inject(0) { |sum, bar| sum + bar.total }
      finished      = @bars.all? { |bar| bar.finished? }
      @renderer.total = total_total
      @renderer.set(total_current)
      @renderer.finish if finished
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
ruby-multi-progressbar-1.0.0 lib/ruby-multi-progressbar/total_bar.rb
multi_progress_bar-0.3.0 lib/multi_progress_bar/total_bar.rb
multi_progress_bar-0.2.0 lib/multi_progress_bar/total_bar.rb
multi_progress_bar-0.1.3 lib/multi_progress_bar/total_bar.rb