class ProgressBar::Progress

Constants

DEFAULT_BEGINNING_POSITION
DEFAULT_SMOOTHING
DEFAULT_TOTAL

Attributes

progress[R]
running_average[RW]
smoothing[RW]
starting_position[RW]
total[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/ruby-progressbar/progress.rb, line 16
def initialize(options = {})
  self.total     = options.fetch(:total, DEFAULT_TOTAL)
  self.smoothing = options[:smoothing] || DEFAULT_SMOOTHING

  start :at => DEFAULT_BEGINNING_POSITION
end

Public Instance Methods

absolute() click to toggle source
# File lib/ruby-progressbar/progress.rb, line 114
def absolute
  progress - starting_position
end
decrement() click to toggle source
# File lib/ruby-progressbar/progress.rb, line 47
def decrement
  if progress == 0
    warn "WARNING: Your progress bar is currently at #{progress} out of #{total} " \
         "and cannot be decremented. In v2.0.0 this will become a " \
         "ProgressBar::InvalidProgressError."
  end

  self.progress -= 1 unless progress == 0
end
finish() click to toggle source
# File lib/ruby-progressbar/progress.rb, line 29
def finish
  self.progress = total unless unknown?
end
finished?() click to toggle source
# File lib/ruby-progressbar/progress.rb, line 33
def finished?
  @progress == @total
end
increment() click to toggle source
# File lib/ruby-progressbar/progress.rb, line 37
def increment
  if progress == total
    warn "WARNING: Your progress bar is currently at #{progress} out of #{total} " \
         "and cannot be incremented. In v2.0.0 this will become a " \
         "ProgressBar::InvalidProgressError."
  end

  self.progress += 1 unless progress == total
end
none?() click to toggle source
# File lib/ruby-progressbar/progress.rb, line 95
def none?
  running_average.zero? || progress.zero?
end
percentage_completed() click to toggle source
# File lib/ruby-progressbar/progress.rb, line 83
def percentage_completed
  return 0   if total.nil?
  return 100 if total == 0

  # progress / total * 100
  #
  # Doing this way so we can avoid converting each
  # number to a float and then back to an integer.
  #
  (progress * 100 / total).to_i
end
percentage_completed_with_precision() click to toggle source
# File lib/ruby-progressbar/progress.rb, line 107
def percentage_completed_with_precision
  return 100.0  if total == 0
  return 0.0    if total.nil?

  '%5.2f' % [(progress * 100 / total.to_f * 100).floor / 100.0]
end
progress=(new_progress) click to toggle source
# File lib/ruby-progressbar/progress.rb, line 61
def progress=(new_progress)
  if total && new_progress > total
    fail ProgressBar::InvalidProgressError,
         "You can't set the item's current value to be greater than the total."
  end

  @progress = new_progress

  self.running_average = Calculators::RunningAverage.calculate(running_average,
                                                               absolute,
                                                               smoothing)
end
reset() click to toggle source
# File lib/ruby-progressbar/progress.rb, line 57
def reset
  start :at => starting_position
end
start(options = {}) click to toggle source
# File lib/ruby-progressbar/progress.rb, line 23
def start(options = {})
  self.running_average   = 0
  self.progress          = \
    self.starting_position = options[:at] || progress
end
total=(new_total) click to toggle source
# File lib/ruby-progressbar/progress.rb, line 74
def total=(new_total)
  unless progress.nil? || new_total.nil? || new_total >= progress
    fail ProgressBar::InvalidProgressError,
         "You can't set the item's total value to less than the current progress."
  end

  @total = new_total
end
total_with_unknown_indicator() click to toggle source
# File lib/ruby-progressbar/progress.rb, line 103
def total_with_unknown_indicator
  total || '??'
end
unknown?() click to toggle source
# File lib/ruby-progressbar/progress.rb, line 99
def unknown?
  progress.nil? || total.nil?
end