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.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/console/progress.rb', line 32

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.



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

def current
  @current
end

#subjectObject (readonly)

Returns the value of attribute subject.



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

def subject
  @subject
end

#totalObject (readonly)

Returns the value of attribute total.



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

def total
  @total
end

Class Method Details

.nowObject



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

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

Instance Method Details

#average_durationObject



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

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

#durationObject



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

def duration
	Progress.now - @start_time
end

#estimated_remaining_timeObject



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

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

#increment(amount = 1) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/console/progress.rb', line 73

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



93
94
95
# File 'lib/console/progress.rb', line 93

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

#progressObject



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

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

#remainingObject



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

def remaining
	@total - @current
end

#resize(total) ⇒ Object



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

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

#to_sObject



97
98
99
100
101
102
103
# File 'lib/console/progress.rb', line 97

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