class Console::Progress

Attributes

current[R]
subject[R]
total[R]

Public Class Methods

new(output, subject, total = 0, minimum_output_duration: 1.0) click to toggle source
# File lib/console/progress.rb, line 30
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
now() click to toggle source
# File lib/console/progress.rb, line 26
def self.now
        Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

Public Instance Methods

average_duration() click to toggle source
# File lib/console/progress.rb, line 59
def average_duration
        if @current > 0
                duration / @current
        end
end
duration() click to toggle source
# File lib/console/progress.rb, line 47
def duration
        Progress.now - @start_time
end
estimated_remaining_time() click to toggle source
# File lib/console/progress.rb, line 65
def estimated_remaining_time
        if average_duration = self.average_duration
                average_duration * remaining
        end
end
increment(amount = 1) click to toggle source
# File lib/console/progress.rb, line 71
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
mark(*arguments) click to toggle source
# File lib/console/progress.rb, line 91
def mark(*arguments)
        @output.info(@subject, *arguments)
end
progress() click to toggle source
# File lib/console/progress.rb, line 51
def progress
        @current.to_f / @total.to_f
end
remaining() click to toggle source
# File lib/console/progress.rb, line 55
def remaining
        @total - @current
end
resize(total) click to toggle source
# File lib/console/progress.rb, line 82
def resize(total)
        @total = total
        
        @output.info(@subject, self) {Event::Progress.new(@current, @total)}
        @last_output_time = Progress.now
        
        return self
end
to_s() click to toggle source
# File lib/console/progress.rb, line 95
def to_s
        if estimated_remaining_time = self.estimated_remaining_time
                "#{@current}/#{@total} completed in #{Clock.formatted_duration(self.duration)}, #{Clock.formatted_duration(estimated_remaining_time)} remaining."
        else
                "#{@current}/#{@total} completed, waiting for estimate..."
        end
end

Private Instance Methods

duration_since_last_output() click to toggle source
# File lib/console/progress.rb, line 105
def duration_since_last_output
        if @last_output_time
                Progress.now - @last_output_time
        end
end
output?() click to toggle source
# File lib/console/progress.rb, line 111
def output?
        if remaining.zero?
                return true
        elsif duration = duration_since_last_output
                return duration > @minimum_output_duration
        else
                return true
        end
end