class Progress::Eta

Estimate time of arrival

Public Class Methods

new() click to toggle source
# File lib/progress/eta.rb, line 8
def initialize
  @started_at = ElapsedTime.now
end

Public Instance Methods

elapsed() click to toggle source
# File lib/progress/eta.rb, line 19
def elapsed
  seconds_to_string(ElapsedTime.now - @started_at)
end
left(completed) click to toggle source
# File lib/progress/eta.rb, line 12
def left(completed)
  seconds = seconds_left(completed)
  return unless seconds && seconds > 0

  seconds_to_string(seconds)
end

Private Instance Methods

seconds_left(completed) click to toggle source
# File lib/progress/eta.rb, line 40
def seconds_left(completed)
  now = ElapsedTime.now
  return unless completed > 0 && now - @started_at >= 1

  current_eta = @started_at + (now - @started_at) / completed
  @left = if @left
    @left + (current_eta - @left) * (1 + completed) * 0.5
  else
    current_eta
  end
  @left - now
end
seconds_to_string(seconds) click to toggle source
# File lib/progress/eta.rb, line 25
def seconds_to_string(seconds)
  return unless seconds

  case seconds
  when 0...60
    format '%.0fs', seconds
  when 60...3600
    format '%.1fm', seconds / 60
  when 3600...86_400
    format '%.1fh', seconds / 3600
  else
    format '%.1fd', seconds / 86_400
  end
end