Class: Sus::Output::Progress
- Inherits:
-
Object
- Object
- Sus::Output::Progress
- Defined in:
- lib/sus/output/progress.rb
Instance Attribute Summary collapse
-
#current ⇒ Object
readonly
Returns the value of attribute current.
-
#subject ⇒ Object
readonly
Returns the value of attribute subject.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Class Method Summary collapse
Instance Method Summary collapse
- #average_duration ⇒ Object
- #clear ⇒ Object
- #duration ⇒ Object
- #estimated_remaining_time ⇒ Object
-
#expand(amount = 1) ⇒ Object
Increase the total size of the progress.
-
#increment(amount = 1) ⇒ Object
Increase the amont of work done.
-
#initialize(output, total = 0, minimum_output_duration: 1.0) ⇒ Progress
constructor
A new instance of Progress.
- #progress ⇒ Object
- #remaining ⇒ Object
- #report(index, context, state) ⇒ Object
- #to_s ⇒ Object
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
#current ⇒ Object (readonly)
Returns the value of attribute current.
34 35 36 |
# File 'lib/sus/output/progress.rb', line 34 def current @current end |
#subject ⇒ Object (readonly)
Returns the value of attribute subject.
33 34 35 |
# File 'lib/sus/output/progress.rb', line 33 def subject @subject end |
#total ⇒ Object (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
.now ⇒ Object
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_duration ⇒ Object
49 50 51 52 53 |
# File 'lib/sus/output/progress.rb', line 49 def average_duration if @current > 0 duration / @current end end |
#clear ⇒ Object
87 88 89 |
# File 'lib/sus/output/progress.rb', line 87 def clear @lines&.clear end |
#duration ⇒ Object
37 38 39 |
# File 'lib/sus/output/progress.rb', line 37 def duration Progress.now - @start_time end |
#estimated_remaining_time ⇒ Object
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 (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 |
#progress ⇒ Object
41 42 43 |
# File 'lib/sus/output/progress.rb', line 41 def progress @current.to_f / @total.to_f end |
#remaining ⇒ Object
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_s ⇒ Object
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 |