SusSourceSusOutputProgress

class Progress

Represents a progress tracker for test execution.

Definitions

def self.now

Get the current monotonic time.

Signature

returns Float

The current time in seconds.

Implementation

def self.now
	::Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

def initialize(output, total = 0, minimum_output_duration: 1.0)

Initialize a new Progress tracker.

Signature

parameter output Output

The output handler.

parameter total Integer

The total number of items to track.

parameter minimum_output_duration Float

Minimum duration before showing output (unused).

Implementation

def initialize(output, total = 0, minimum_output_duration: 1.0)
	@output = output
	@subject = subject
	
	@start_time = Progress.now
	
	if @output.interactive?
		@bar = Bar.new
		@lines = Lines.new(@output)
		@lines[0] = @bar
	end
	
	@current = 0
	@total = total
end

attr :subject

Signature

attribute Object, nil

The subject being tracked.

attr :current

Signature

attribute Integer

The current progress value.

attr :total

Signature

attribute Integer

The total value.

def duration

Signature

returns Float

The elapsed duration in seconds.

Implementation

def duration
	Progress.now - @start_time
end

def progress

Signature

returns Float

The progress as a fraction (0.0 to 1.0).

Implementation

def progress
	@current.to_f / @total.to_f
end

def remaining

Signature

returns Integer

The remaining items to process.

Implementation

def remaining
	@total - @current
end

def average_duration

Signature

returns Float, nil

The average duration per item, or nil if no items completed.

Implementation

def average_duration
	if @current > 0
		duration / @current
	end
end

def estimated_remaining_time

Signature

returns Float, nil

The estimated remaining time, or nil if cannot be calculated.

Implementation

def estimated_remaining_time
	if average_duration = self.average_duration
		average_duration * remaining
	end
end

def increment(amount = 1)

Increase the amount of work done.

Signature

parameter amount Integer

The amount to increment by.

returns Progress

Returns self for method chaining.

Implementation

def increment(amount = 1)
	@current += amount
	
	@bar&.update(@current, @total, self.to_s)
	@lines&.redraw(0)
	
	return self
end

def expand(amount = 1)

Increase the total size of the progress.

Signature

parameter amount Integer

The amount to expand by.

returns Progress

Returns self for method chaining.

Implementation

def expand(amount = 1)
	@total += amount
	
	@bar&.update(@current, @total, self.to_s)
	@lines&.redraw(0)
	
	return self
end

def report(index, context, state)

Report the status of a specific item.

Signature

parameter index Integer

The index of the item.

parameter context Object

The context to display.

parameter state Symbol

The state (:free or :busy).

returns Progress

Returns self for method chaining.

Implementation

def report(index, context, state)
	@lines&.[]=(index+1, Status.new(state, context))
	
	return self
end

def clear

Clear the progress display.

Implementation

def clear
	@lines&.clear
end

def to_s

Signature

returns String

A string representation of the progress.

Implementation

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"
	end
end