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: 1.0) ⇒ Progress

Returns a new instance of Progress.


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/console/progress.rb', line 29

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

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current


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

def current
  @current
end

#subjectObject (readonly)

Returns the value of attribute subject


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

def subject
  @subject
end

#totalObject (readonly)

Returns the value of attribute total


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

def total
  @total
end

Class Method Details

.nowObject


25
26
27
# File 'lib/console/progress.rb', line 25

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

Instance Method Details

#average_durationObject


58
59
60
61
62
# File 'lib/console/progress.rb', line 58

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

#durationObject


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

def duration
	Progress.now - @start_time
end

#estimated_remaining_timeObject


64
65
66
67
68
# File 'lib/console/progress.rb', line 64

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

#increment(amount = 1) ⇒ Object


70
71
72
73
74
75
76
77
78
79
# File 'lib/console/progress.rb', line 70

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) ⇒ Object


90
91
92
# File 'lib/console/progress.rb', line 90

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

#progressObject


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

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

#remainingObject


54
55
56
# File 'lib/console/progress.rb', line 54

def remaining
	@total - @current
end

#resize(total) ⇒ Object


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

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

#to_sObject


94
95
96
97
98
99
100
# File 'lib/console/progress.rb', line 94

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, waiting for estimate..."
	end
end