Class: Console::Progress
- Inherits:
-
Object
- Object
- Console::Progress
- Defined in:
- lib/console/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
- #duration ⇒ Object
- #estimated_remaining_time ⇒ Object
- #increment(amount = 1) ⇒ Object
-
#initialize(subject, total = 0, minimum_output_duration: 0.1, **options) ⇒ Progress
constructor
A new instance of Progress.
- #mark(*arguments, **options) ⇒ Object
- #ratio ⇒ Object
- #remaining ⇒ Object
- #resize(total) ⇒ Object
- #to_hash ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(subject, total = 0, minimum_output_duration: 0.1, **options) ⇒ Progress
Returns a new instance of Progress.
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/console/progress.rb', line 15 def initialize(subject, total = 0, minimum_output_duration: 0.1, **) @subject = subject @options = @start_time = Progress.now @last_output_time = nil @minimum_output_duration = minimum_output_duration @current = 0 @total = total end |
Instance Attribute Details
#current ⇒ Object (readonly)
Returns the value of attribute current.
29 30 31 |
# File 'lib/console/progress.rb', line 29 def current @current end |
#subject ⇒ Object (readonly)
Returns the value of attribute subject.
28 29 30 |
# File 'lib/console/progress.rb', line 28 def subject @subject end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
30 31 32 |
# File 'lib/console/progress.rb', line 30 def total @total end |
Class Method Details
.now ⇒ Object
11 12 13 |
# File 'lib/console/progress.rb', line 11 def self.now Process.clock_gettime(Process::CLOCK_MONOTONIC) end |
Instance Method Details
#average_duration ⇒ Object
44 45 46 47 48 |
# File 'lib/console/progress.rb', line 44 def average_duration if @current > 0 duration / @current end end |
#duration ⇒ Object
32 33 34 |
# File 'lib/console/progress.rb', line 32 def duration Progress.now - @start_time end |
#estimated_remaining_time ⇒ Object
50 51 52 53 54 |
# File 'lib/console/progress.rb', line 50 def estimated_remaining_time if average_duration = self.average_duration average_duration * remaining end end |
#increment(amount = 1) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/console/progress.rb', line 67 def increment(amount = 1) @current += amount if output? Console.call(@subject, self.to_s, event: self.to_hash, **@options) @last_output_time = Progress.now end return self end |
#mark(*arguments, **options) ⇒ Object
87 88 89 |
# File 'lib/console/progress.rb', line 87 def mark(*arguments, **) Console.call(@subject, *arguments, **, **@options) end |
#ratio ⇒ Object
36 37 38 |
# File 'lib/console/progress.rb', line 36 def ratio Rational(@current.to_f, @total.to_f) end |
#remaining ⇒ Object
40 41 42 |
# File 'lib/console/progress.rb', line 40 def remaining @total - @current end |
#resize(total) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/console/progress.rb', line 78 def resize(total) @total = total Console.call(@subject, self.to_s, event: self.to_hash, **@options) @last_output_time = Progress.now return self end |
#to_hash ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/console/progress.rb', line 56 def to_hash Hash.new.tap do |hash| hash[:type] = :progress hash[:current] = @current hash[:total] = @total hash[:duration] = self.duration hash[:estimated_remaining_time] = self.estimated_remaining_time end end |
#to_s ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/console/progress.rb', line 91 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 |