Class: Console::Progress

Inherits:
Object
  • Object
show all
Defined in:
lib/console/progress.rb

Overview

A simple progress indicator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject, total = 0, minimum_output_duration: 0.1, **options) ⇒ Progress

Create a new progress indicator.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/console/progress.rb', line 23

def initialize(subject, total = 0, minimum_output_duration: 0.1, **options)
	@subject = subject
	@options = options
	
	@start_time = Clock.now
	
	@last_output_time = nil
	@minimum_output_duration = minimum_output_duration
	
	@current = 0
	@total = total
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



46
47
48
# File 'lib/console/progress.rb', line 46

def current
  @current
end

#minimum_output_durationObject (readonly)

Returns the value of attribute minimum_output_duration.



40
41
42
# File 'lib/console/progress.rb', line 40

def minimum_output_duration
  @minimum_output_duration
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



43
44
45
# File 'lib/console/progress.rb', line 43

def start_time
  @start_time
end

#subjectObject (readonly)

Returns the value of attribute subject.



37
38
39
# File 'lib/console/progress.rb', line 37

def subject
  @subject
end

#The current number of steps completed.(currentnumberofstepscompleted.) ⇒ Object (readonly)



46
# File 'lib/console/progress.rb', line 46

attr :current

#The minimum duration between outputs.(minimumdurationbetweenoutputs.) ⇒ Object (readonly)



40
# File 'lib/console/progress.rb', line 40

attr :minimum_output_duration

#The total number of steps.(totalnumberofsteps.) ⇒ Object (readonly)



49
# File 'lib/console/progress.rb', line 49

attr :total

#totalObject (readonly)

Returns the value of attribute total.



49
50
51
# File 'lib/console/progress.rb', line 49

def total
  @total
end

Class Method Details

.nowObject

Deprecated.

Use Clock.now instead.



13
14
15
# File 'lib/console/progress.rb', line 13

def self.now
	Clock.now
end

Instance Method Details

#average_durationObject



67
68
69
70
71
# File 'lib/console/progress.rb', line 67

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

#durationObject



52
53
54
# File 'lib/console/progress.rb', line 52

def duration
	Clock.now - @start_time
end

#estimated_remaining_timeObject



74
75
76
77
78
# File 'lib/console/progress.rb', line 74

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

#increment(amount = 1) ⇒ Object

Increment the progress indicator by the given amount.



98
99
100
101
102
103
104
105
106
107
# File 'lib/console/progress.rb', line 98

def increment(amount = 1)
	@current += amount
	
	if output?
		Console.call(@subject, self.to_s, event: self.to_hash, **@options)
		@last_output_time = Clock.now
	end
	
	return self
end

#mark(*arguments, **options, &block) ⇒ Object

Augment the progress indicator with additional information.



127
128
129
# File 'lib/console/progress.rb', line 127

def mark(*arguments, **options, &block)
	Console.call(@subject, *arguments, **options, **@options, &block)
end

#ratioObject



57
58
59
# File 'lib/console/progress.rb', line 57

def ratio
	Rational(@current.to_f, @total.to_f)
end

#remainingObject



62
63
64
# File 'lib/console/progress.rb', line 62

def remaining
	@total - @current
end

#resize(total) ⇒ Object

Resize the progress indicator to the given total.



113
114
115
116
117
118
119
120
# File 'lib/console/progress.rb', line 113

def resize(total)
	@total = total
	
	Console.call(@subject, self.to_s, event: self.to_hash, **@options)
	@last_output_time = Clock.now
	
	return self
end

#The subject of the progress indicator.=(subjectoftheprogressindicator. = (value)) ⇒ Object



37
# File 'lib/console/progress.rb', line 37

attr :subject

#The time the progress indicator was started.=(timetheprogressindicatorwasstarted. = (value)) ⇒ Object



43
# File 'lib/console/progress.rb', line 43

attr :start_time

#to_hashObject

Generate an appropriate event for the progress indicator.



83
84
85
86
87
88
89
90
91
92
# File 'lib/console/progress.rb', line 83

def to_hash
	Hash.new.tap do |hash|
		hash[:type] = :progress
		hash[:current] = @current
		hash[:total] = @total
		
		hash[:duration] = self.duration
		hash[:estimated_remaining_time] = self.estimated_remaining_time
	end
end

#to_sObject



132
133
134
135
136
137
138
# File 'lib/console/progress.rb', line 132

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