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.



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

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.



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

def current
  @current
end

#subjectObject (readonly)

Returns the value of attribute subject.



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

def subject
  @subject
end

#totalObject (readonly)

Returns the value of attribute total.



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

def total
  @total
end

Class Method Details

.nowObject



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

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

Instance Method Details

#average_durationObject



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

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

#durationObject



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

def duration
	Progress.now - @start_time
end

#estimated_remaining_timeObject



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

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

#increment(amount = 1) ⇒ Object



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

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



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

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

#ratioObject



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

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

#remainingObject



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

def remaining
	@total - @current
end

#resize(total) ⇒ Object



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

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

#to_sObject



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

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