Class: Console::Progress

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Progress.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/console/progress.rb', line 15

def initialize(output, subject, total = 0, minimum_output_duration: 0.1)
	@output = output
	@subject = subject
	
	@start_time = Progress.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.



29
30
31
# File 'lib/console/progress.rb', line 29

def current
  @current
end

#subjectObject (readonly)

Returns the value of attribute subject.



28
29
30
# File 'lib/console/progress.rb', line 28

def subject
  @subject
end

#totalObject (readonly)

Returns the value of attribute total.



30
31
32
# File 'lib/console/progress.rb', line 30

def total
  @total
end

Class Method Details

.nowObject



11
12
13
# File 'lib/console/progress.rb', line 11

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

Instance Method Details

#average_durationObject



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

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

#durationObject



32
33
34
# File 'lib/console/progress.rb', line 32

def duration
	Progress.now - @start_time
end

#estimated_remaining_timeObject



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

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

#increment(amount = 1) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/console/progress.rb', line 56

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

#markObject



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

def mark(...)
	@output.info(@subject, ...)
end

#ratioObject



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

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

#remainingObject



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

def remaining
	@total - @current
end

#resize(total) ⇒ Object



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

def resize(total)
	@total = total
	
	@output.info(@subject, self) {Event::Progress.new(@current, @total)}
	@last_output_time = Progress.now
	
	return self
end

#to_sObject



80
81
82
83
84
85
86
# File 'lib/console/progress.rb', line 80

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