class ProgressBar::Progress

Constants

DEFAULT_BEGINNING_POSITION
DEFAULT_TOTAL

Attributes

progress[R]
starting_position[RW]
total[R]

Public Class Methods

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

  start(:at => DEFAULT_BEGINNING_POSITION)
end

Public Instance Methods

absolute() click to toggle source
# File lib/ruby-progressbar/progress.rb, line 104
def absolute
  progress - starting_position
end
decrement() click to toggle source
# File lib/ruby-progressbar/progress.rb, line 41
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 23
def finish
  self.progress = total unless unknown?
end
finished?() click to toggle source
# File lib/ruby-progressbar/progress.rb, line 27
def finished?
  @progress == @total
end
increment() click to toggle source
# File lib/ruby-progressbar/progress.rb, line 31
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 85
def none?
  progress.zero?
end
percentage_completed() click to toggle source
# File lib/ruby-progressbar/progress.rb, line 73
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 97
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 55
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
end
reset() click to toggle source
# File lib/ruby-progressbar/progress.rb, line 51
def reset
  start(:at => starting_position)
end
start(options = {}) click to toggle source
# File lib/ruby-progressbar/progress.rb, line 18
def start(options = {})
  self.progress = \
    self.starting_position = options[:at] || progress
end
total=(new_total) click to toggle source
# File lib/ruby-progressbar/progress.rb, line 64
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 93
def total_with_unknown_indicator
  total || '??'
end
unknown?() click to toggle source
# File lib/ruby-progressbar/progress.rb, line 89
def unknown?
  progress.nil? || total.nil?
end