Class: Sus::Output::Progress

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Progress.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sus/output/progress.rb', line 17

def initialize(output, total = 0, minimum_output_duration: 1.0)
	@output = output
	@subject = subject
	
	@start_time = Progress.now
	
	if @output.interactive?
		@bar = Bar.new
		@lines = Lines.new(@output)
		@lines[0] = @bar
	end
	
	@current = 0
	@total = total
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



34
35
36
# File 'lib/sus/output/progress.rb', line 34

def current
  @current
end

#subjectObject (readonly)

Returns the value of attribute subject.



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

def subject
  @subject
end

#totalObject (readonly)

Returns the value of attribute total.



35
36
37
# File 'lib/sus/output/progress.rb', line 35

def total
  @total
end

Class Method Details

.nowObject



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

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

Instance Method Details

#average_durationObject



49
50
51
52
53
# File 'lib/sus/output/progress.rb', line 49

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

#clearObject



87
88
89
# File 'lib/sus/output/progress.rb', line 87

def clear
	@lines&.clear
end

#durationObject



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

def duration
	Progress.now - @start_time
end

#estimated_remaining_timeObject



55
56
57
58
59
# File 'lib/sus/output/progress.rb', line 55

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

#expand(amount = 1) ⇒ Object

Increase the total size of the progress.



72
73
74
75
76
77
78
79
# File 'lib/sus/output/progress.rb', line 72

def expand(amount = 1)
	@total += amount
	
	@bar&.update(@current, @total, self.to_s)
	@lines&.redraw(0)
	
	return self
end

#increment(amount = 1) ⇒ Object

Increase the amont of work done.



62
63
64
65
66
67
68
69
# File 'lib/sus/output/progress.rb', line 62

def increment(amount = 1)
	@current += amount
	
	@bar&.update(@current, @total, self.to_s)
	@lines&.redraw(0)
	
	return self
end

#progressObject



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

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

#remainingObject



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

def remaining
	@total - @current
end

#report(index, context, state) ⇒ Object



81
82
83
84
85
# File 'lib/sus/output/progress.rb', line 81

def report(index, context, state)
	@lines&.[]=(index+1, Status.new(state, context))
	
	return self
end

#to_sObject



91
92
93
94
95
96
97
# File 'lib/sus/output/progress.rb', line 91

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"
	end
end