class MultiProgressBar::TotalBar

Works just like Bar, but displays the total of other bars. TotalBar#inc and TotalBar#set don’t work.

Public Class Methods

new(title, bars = MultiProgressBar.bars.dup) click to toggle source

Create a new TotalBar. bars is an array of Bar objects, and defaults to all existing bars.

Calls superclass method MultiProgressBar::Bar::new
# File lib/ruby-multi-progressbar/total_bar.rb, line 7
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

Private Instance Methods

update_total() click to toggle source
# File lib/ruby-multi-progressbar/total_bar.rb, line 27
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