Sha256: eeb84bede7432e1f2dd343cb5da5725b156f8bddb4fa99b21966953d93c11c98

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

module MultiProgressBar
  # Represents a progress bar at the bottom of the screen.
  #
  # +Bar+ exposes the same interface as +ProgressBar+ from the +progressbar+
  # gem which backs its display.
  #
  #   file_progress = MultiProgressBar::Bar.new("file_1", 2596)
  #   file_progress.inc     # Increment value by 1.
  #   file_progress.inc(10) # Increment value by 10.
  #   file_progress.set(30) # Set value to 30.
  #
  #   # Change bar format.
  #   file_progress.format = "%-14s (%s) %3d%% %s"
  #   file_progress.format_arguments = [:title, :stat, :percentage, :bar].
  #
  # See the +ruby-progressbar+ gem (http://0xcc.net/ruby-progressbar/index.html.en)
  # for more details.
  class Bar < DelegateClass(BarRenderer)
    # Create a new Bar with a +title+ and a +total+ value.
    def initialize(title, total)
      MultiProgressBar.add_bar(self)

      @observers = []

      @renderer = BarRenderer.new(title, total, MultiProgressBar.width) do |rendered_bar|
        MultiProgressBar.update_bar(self, rendered_bar)
      end

      super @renderer
    end

    # Increment the current value of the bar.
    def inc(step = 1)
      super
      notify_observers
    end

    # Set the current value of the bar absolutely.
    def set(count)
      super
      notify_observers
    end

    def observe(&b)  #:nodoc:
      @observers << b
    end

    private
    def notify_observers
      @observers.each { |b| b.call(self) }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
multi_progress_bar-0.1.0 lib/multi_progress_bar/bar.rb
multi_progress_bar-0.0.0 lib/multi_progress_bar/bar.rb