class Popro::Formatter::Estimate

Constants

UNITS

Attributes

info[R]

TODO: cleaner implementation/formatstring

Public Class Methods

new() click to toggle source
# File lib/popro/formatter.rb, line 50
def initialize
  @start_time = current_time
  @info = nil
end

Public Instance Methods

call(info, *_args) click to toggle source
# File lib/popro/formatter.rb, line 55
def call(info, *_args)
  @info = info

  [
    "estimated time left: #{format_duration(estimated_left)}",
    "[#{format_duration(elapsed)}/#{format_duration(estimated_total)}]"
  ].join(', ')
end

Private Instance Methods

current_time() click to toggle source
# File lib/popro/formatter.rb, line 112
def current_time
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
elapsed() click to toggle source
# File lib/popro/formatter.rb, line 75
def elapsed
  current_time - @start_time
end
estimated_left() click to toggle source
# File lib/popro/formatter.rb, line 85
def estimated_left
  return nil if info.current.zero? || info.total.zero?

  (info.total / info.current) * elapsed
end
estimated_total() click to toggle source
# File lib/popro/formatter.rb, line 79
def estimated_total
  return nil if info.current.zero? || info.total.zero?

  elapsed + (info.total / info.current) * elapsed
end
format_duration(secs) click to toggle source
# File lib/popro/formatter.rb, line 91
def format_duration(secs)
  return '?' if secs.nil?

  return format('%.3fs', secs) if secs < 10

  format_duration_long(secs)
end
format_duration_long(secs) click to toggle source
# File lib/popro/formatter.rb, line 99
def format_duration_long(secs)
  UNITS.map do |(divisor, format_str)|
    next if secs < 1

    if divisor
      amount = secs % divisor
      secs /= divisor
    end

    format(format_str, divisor ? amount : secs)
  end.take_while(&:itself).reverse!.join
end