class ProgressBar::Base

Constants

RUNNING_AVERAGE_RATE_DEPRECATION_WARNING
SMOOTHING_DEPRECATION_WARNING

rubocop:disable Layout/HeredocIndentation

Attributes

autofinish[RW]
autostart[RW]
bar_component[RW]
finished[RW]
output[RW]
percentage_component[RW]
progressable[RW]
projector[RW]
rate_component[RW]
time_component[RW]
timer[RW]
title_component[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/ruby-progressbar/base.rb, line 45
def initialize(options = {}) # rubocop:disable Metrics/AbcSize
  options[:projector] ||= {}

  self.autostart    = options.fetch(:autostart,  true)
  self.autofinish   = options.fetch(:autofinish, true)
  self.finished     = false

  self.timer        = Timer.new(options)
  projector_opts    = if options[:projector].any?
                        options[:projector]
                      elsif options[:smoothing]
                        warn SMOOTHING_DEPRECATION_WARNING

                        { :strength => options[:smoothing] }
                      elsif options[:running_average_rate]
                        warn RUNNING_AVERAGE_RATE_DEPRECATION_WARNING

                        { :strength => options[:smoothing] }
                      else
                        {}
                      end
  self.projector    = Projector.
                        from_type(options[:projector][:type]).
                        new(projector_opts)
  self.progressable = Progress.new(options)

  options = options.merge(:progress  => progressable,
                          :projector => projector,
                          :timer     => timer)

  self.title_component      = Components::Title.new(options)
  self.bar_component        = Components::Bar.new(options)
  self.percentage_component = Components::Percentage.new(options)
  self.rate_component       = Components::Rate.new(options)
  self.time_component       = Components::Time.new(options)

  self.output       = Output.detect(options.merge(:bar => self))
  @format           = Format::String.new(output.resolve_format(options[:format]))

  start :at => options[:starting_at] if autostart
end

Public Instance Methods

decrement() click to toggle source
# File lib/ruby-progressbar/base.rb, line 137
def decrement
  update_progress(:decrement)
end
finish() click to toggle source
# File lib/ruby-progressbar/base.rb, line 92
def finish
  return if finished?

  output.with_refresh do
    self.finished = true
    progressable.finish
    timer.stop
  end
end
finished?() click to toggle source
# File lib/ruby-progressbar/base.rb, line 129
def finished?
  finished || (autofinish && progressable.finished?)
end
format(other)
Alias for: format=
format=(other) click to toggle source
# File lib/ruby-progressbar/base.rb, line 203
def format=(other)
  output.refresh_with_format_change do
    @format = Format::String.new(other || output.default_format)
  end
end
Also aliased as: format
increment() click to toggle source
# File lib/ruby-progressbar/base.rb, line 141
def increment
  update_progress(:increment)
end
inspect() click to toggle source

rubocop:enable Metrics/AbcSize, Layout/LineLength

# File lib/ruby-progressbar/base.rb, line 199
def inspect
  "#<ProgressBar:#{progress}/#{total || 'unknown'}>"
end
pause() click to toggle source
# File lib/ruby-progressbar/base.rb, line 102
def pause
  output.with_refresh { timer.pause } unless paused?
end
paused?()
Alias for: stopped?
progress=(new_progress) click to toggle source
# File lib/ruby-progressbar/base.rb, line 145
def progress=(new_progress)
  update_progress(:progress=, new_progress)
end
progress_mark=(mark) click to toggle source
# File lib/ruby-progressbar/base.rb, line 153
def progress_mark=(mark)
  output.refresh_with_format_change { bar_component.progress_mark = mark }
end
remainder_mark=(mark) click to toggle source
# File lib/ruby-progressbar/base.rb, line 157
def remainder_mark=(mark)
  output.refresh_with_format_change { bar_component.remainder_mark = mark }
end
reset() click to toggle source
# File lib/ruby-progressbar/base.rb, line 114
def reset
  output.with_refresh do
    self.finished = false
    progressable.reset
    projector.reset
    timer.reset
  end
end
resume() click to toggle source
# File lib/ruby-progressbar/base.rb, line 110
def resume
  output.with_refresh { timer.resume } if stopped?
end
start(options = {}) click to toggle source
# File lib/ruby-progressbar/base.rb, line 87
def start(options = {})
  timer.start
  update_progress(:start, options)
end
started?() click to toggle source
# File lib/ruby-progressbar/base.rb, line 133
def started?
  timer.started?
end
stop() click to toggle source
# File lib/ruby-progressbar/base.rb, line 106
def stop
  output.with_refresh { timer.stop } unless stopped?
end
stopped?() click to toggle source
# File lib/ruby-progressbar/base.rb, line 123
def stopped?
  timer.stopped? || finished?
end
Also aliased as: paused?
title() click to toggle source
# File lib/ruby-progressbar/base.rb, line 161
def title
  title_component.title
end
title=(title) click to toggle source
# File lib/ruby-progressbar/base.rb, line 165
def title=(title)
  output.refresh_with_format_change { title_component.title = title }
end
to_h() click to toggle source

rubocop:disable Metrics/AbcSize, Layout/LineLength

# File lib/ruby-progressbar/base.rb, line 176
def to_h
  {
    'output_stream'                       => output.__send__(:stream),
    'length'                              => output.length,
    'title'                               => title_component.title,
    'progress_mark'                       => bar_component.progress_mark,
    'remainder_mark'                      => bar_component.remainder_mark,
    'progress'                            => progressable.progress,
    'total'                               => progressable.total,
    'percentage'                          => progressable.percentage_completed_with_precision.to_f,
    'elapsed_time_in_seconds'             => time_component.__send__(:timer).elapsed_seconds,
    'estimated_time_remaining_in_seconds' => time_component.__send__(:estimated_seconds_remaining),
    'base_rate_of_change'                 => rate_component.__send__(:base_rate),
    'scaled_rate_of_change'               => rate_component.__send__(:scaled_rate),
    'unknown_progress_animation_steps'    => bar_component.upa_steps,
    'throttle_rate'                       => output.__send__(:throttle).rate,
    'started?'                            => started?,
    'stopped?'                            => stopped?,
    'finished?'                           => finished?
  }
end
to_s(new_format = nil) click to toggle source
# File lib/ruby-progressbar/base.rb, line 169
def to_s(new_format = nil)
  self.format = new_format if new_format

  Format::Formatter.process(@format, output.length, self)
end
total=(new_total) click to toggle source
# File lib/ruby-progressbar/base.rb, line 149
def total=(new_total)
  update_progress(:total=, new_total)
end

Protected Instance Methods

update_progress(*args) click to toggle source
# File lib/ruby-progressbar/base.rb, line 226
def update_progress(*args)
  output.with_refresh do
    progressable.__send__(*args)
    projector.__send__(*args)
    timer.stop if finished?
  end
end