class ProgressBar::Components::Time

Constants

ELAPSED_LABEL
ESTIMATED_LABEL
NO_TIME_ELAPSED_TEXT
OOB_FRIENDLY_TIME_TEXT
OOB_LIMIT_IN_HOURS
OOB_TEXT_TO_FORMAT
OOB_TIME_FORMATS
OOB_UNKNOWN_TIME_TEXT
TIME_FORMAT

Attributes

out_of_bounds_time_format[R]
progress[RW]
timer[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/ruby-progressbar/components/time.rb, line 20
def initialize(options = {})
  self.out_of_bounds_time_format = options[:out_of_bounds_time_format]
  self.timer                     = options[:timer]
  self.progress                  = options[:progress]
end

Public Instance Methods

elapsed_with_label() click to toggle source
# File lib/ruby-progressbar/components/time.rb, line 30
def elapsed_with_label
  "#{ELAPSED_LABEL}: #{elapsed}"
end
estimated_with_label() click to toggle source
# File lib/ruby-progressbar/components/time.rb, line 26
def estimated_with_label
  "#{ESTIMATED_LABEL}: #{estimated}"
end

Protected Instance Methods

estimated_with_friendly_oob() click to toggle source
# File lib/ruby-progressbar/components/time.rb, line 48
def estimated_with_friendly_oob
  self.out_of_bounds_time_format = :friendly

  estimated_with_elapsed_fallback
end
estimated_with_no_oob() click to toggle source
# File lib/ruby-progressbar/components/time.rb, line 36
def estimated_with_no_oob
  self.out_of_bounds_time_format = nil

  estimated_with_elapsed_fallback
end
estimated_with_unknown_oob() click to toggle source
# File lib/ruby-progressbar/components/time.rb, line 42
def estimated_with_unknown_oob
  self.out_of_bounds_time_format = :unknown

  estimated_with_elapsed_fallback
end
out_of_bounds_time_format=(format) click to toggle source
# File lib/ruby-progressbar/components/time.rb, line 58
def out_of_bounds_time_format=(format)
  unless OOB_TIME_FORMATS.include? format
    fail StandardError, "Invalid Out Of Bounds time format.  Valid formats are #{OOB_TIME_FORMATS.inspect}"
  end

  @out_of_bounds_time_format = format
end

Private Instance Methods

elapsed() click to toggle source
# File lib/ruby-progressbar/components/time.rb, line 82
def elapsed
  return NO_TIME_ELAPSED_TEXT unless timer.started?

  hours, minutes, seconds = timer.divide_seconds(timer.elapsed_whole_seconds)

  TIME_FORMAT % [hours, minutes, seconds]
end
estimated() click to toggle source
# File lib/ruby-progressbar/components/time.rb, line 68
def estimated
  memo_estimated_seconds_remaining = estimated_seconds_remaining

  return OOB_UNKNOWN_TIME_TEXT unless memo_estimated_seconds_remaining

  hours, minutes, seconds = timer.divide_seconds(memo_estimated_seconds_remaining)

  if hours > OOB_LIMIT_IN_HOURS && out_of_bounds_time_format
    OOB_TEXT_TO_FORMAT[out_of_bounds_time_format]
  else
    TIME_FORMAT % [hours, minutes, seconds]
  end
end
estimated_seconds_remaining() click to toggle source
# File lib/ruby-progressbar/components/time.rb, line 94
def estimated_seconds_remaining
  return if progress.unknown? || progress.none? || timer.stopped? || timer.reset?

  (timer.elapsed_seconds * (progress.total / progress.running_average - 1)).round
end
estimated_with_elapsed_fallback() click to toggle source
# File lib/ruby-progressbar/components/time.rb, line 90
def estimated_with_elapsed_fallback
  progress.finished? ? elapsed_with_label : estimated_with_label
end