class Console::Progress
def self.now
def self.now Process.clock_gettime(Process::CLOCK_MONOTONIC) end
def average_duration
def average_duration if @current > 0 duration / @current end end
def duration
def duration Progress.now - @start_time end
def duration_since_last_output
def duration_since_last_output if @last_output_time Progress.now - @last_output_time end end
def estimated_remaining_time
def estimated_remaining_time if average_duration = self.average_duration average_duration * remaining end end
def formatted_duration(duration)
def formatted_duration(duration) if duration < 60.0 return "#{duration.round(2)}s" end duration /= 60.0 if duration < 60.0 return "#{duration.round}m" end duration /= 60.0 if duration < 60.0 return "#{duration.round(1)}h" end duration /= 24.0 return "#{duration.round(1)}d" end
def increment(amount = 1)
def increment(amount = 1) @current += amount if output? @output.info(@subject, self) {Event::Progress.new(@current, @total)} @last_output_time = Progress.now end return self end
def initialize(output, subject, total = 0, minimum_output_duration: 1.0)
def initialize(output, subject, total = 0, minimum_output_duration: 1.0) @output = output @subject = subject @start_time = Progress.now @last_output_time = nil @minimum_output_duration = 0.1 @current = 0 @total = total end
def mark(*arguments)
def mark(*arguments) @output.info(@subject, *arguments) end
def output?
def output? if duration = duration_since_last_output return duration > @minimum_output_duration else return true end end
def progress
def progress @current.to_f / @total.to_f end
def remaining
def remaining @total - @current end
def resize(total)
def resize(total) @total = total @output.info(@subject, self) {Event::Progress.new(@current, @total)} @last_output_time = Progress.now return self end
def to_s
def to_s if estimated_remaining_time = self.estimated_remaining_time "#{@current}/#{@total} completed in #{formatted_duration(self.duration)}, #{formatted_duration(estimated_remaining_time)} remaining." else "#{@current}/#{@total} completed, waiting for estimate..." end end