Class: Console::Progress
- Inherits:
-
Object
- Object
- Console::Progress
- Defined in:
- lib/console/progress.rb
Overview
A simple progress indicator
Instance Attribute Summary collapse
-
#current ⇒ Object
readonly
Returns the value of attribute current.
-
#minimum_output_duration ⇒ Object
readonly
Returns the value of attribute minimum_output_duration.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
-
#subject ⇒ Object
readonly
Returns the value of attribute subject.
- #The current number of steps completed.(currentnumberofstepscompleted.) ⇒ Object readonly
- #The minimum duration between outputs.(minimumdurationbetweenoutputs.) ⇒ Object readonly
- #The total number of steps.(totalnumberofsteps.) ⇒ Object readonly
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Class Method Summary collapse
-
.now ⇒ Object
deprecated
Deprecated.
Use Clock.now instead.
Instance Method Summary collapse
- #average_duration ⇒ Object
- #duration ⇒ Object
- #estimated_remaining_time ⇒ Object
-
#increment(amount = 1) ⇒ Object
Increment the progress indicator by the given amount.
-
#initialize(subject, total = 0, minimum_output_duration: 0.1, **options) ⇒ Progress
constructor
Create a new progress indicator.
-
#mark(*arguments, **options, &block) ⇒ Object
Augment the progress indicator with additional information.
- #ratio ⇒ Object
- #remaining ⇒ Object
-
#resize(total) ⇒ Object
Resize the progress indicator to the given total.
- #The subject of the progress indicator.=(subjectoftheprogressindicator. = (value)) ⇒ Object
- #The time the progress indicator was started.=(timetheprogressindicatorwasstarted. = (value)) ⇒ Object
-
#to_hash ⇒ Object
Generate an appropriate event for the progress indicator.
- #to_s ⇒ Object
Constructor Details
#initialize(subject, total = 0, minimum_output_duration: 0.1, **options) ⇒ Progress
Create a new progress indicator.
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/console/progress.rb', line 23 def initialize(subject, total = 0, minimum_output_duration: 0.1, **) @subject = subject @options = @start_time = Clock.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.
46 47 48 |
# File 'lib/console/progress.rb', line 46 def current @current end |
#minimum_output_duration ⇒ Object (readonly)
Returns the value of attribute minimum_output_duration.
40 41 42 |
# File 'lib/console/progress.rb', line 40 def minimum_output_duration @minimum_output_duration end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
43 44 45 |
# File 'lib/console/progress.rb', line 43 def start_time @start_time end |
#subject ⇒ Object (readonly)
Returns the value of attribute subject.
37 38 39 |
# File 'lib/console/progress.rb', line 37 def subject @subject end |
#The current number of steps completed.(currentnumberofstepscompleted.) ⇒ Object (readonly)
46 |
# File 'lib/console/progress.rb', line 46 attr :current |
#The minimum duration between outputs.(minimumdurationbetweenoutputs.) ⇒ Object (readonly)
40 |
# File 'lib/console/progress.rb', line 40 attr :minimum_output_duration |
#The total number of steps.(totalnumberofsteps.) ⇒ Object (readonly)
49 |
# File 'lib/console/progress.rb', line 49 attr :total |
#total ⇒ Object (readonly)
Returns the value of attribute total.
49 50 51 |
# File 'lib/console/progress.rb', line 49 def total @total end |
Class Method Details
Instance Method Details
#average_duration ⇒ Object
67 68 69 70 71 |
# File 'lib/console/progress.rb', line 67 def average_duration if @current > 0 duration / @current end end |
#duration ⇒ Object
52 53 54 |
# File 'lib/console/progress.rb', line 52 def duration Clock.now - @start_time end |
#estimated_remaining_time ⇒ Object
74 75 76 77 78 |
# File 'lib/console/progress.rb', line 74 def estimated_remaining_time if average_duration = self.average_duration average_duration * remaining end end |
#increment(amount = 1) ⇒ Object
Increment the progress indicator by the given amount.
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/console/progress.rb', line 98 def increment(amount = 1) @current += amount if output? Console.call(@subject, self.to_s, event: self.to_hash, **@options) @last_output_time = Clock.now end return self end |
#mark(*arguments, **options, &block) ⇒ Object
Augment the progress indicator with additional information.
127 128 129 |
# File 'lib/console/progress.rb', line 127 def mark(*arguments, **, &block) Console.call(@subject, *arguments, **, **@options, &block) end |
#ratio ⇒ Object
57 58 59 |
# File 'lib/console/progress.rb', line 57 def ratio Rational(@current.to_f, @total.to_f) end |
#remaining ⇒ Object
62 63 64 |
# File 'lib/console/progress.rb', line 62 def remaining @total - @current end |
#resize(total) ⇒ Object
Resize the progress indicator to the given total.
113 114 115 116 117 118 119 120 |
# File 'lib/console/progress.rb', line 113 def resize(total) @total = total Console.call(@subject, self.to_s, event: self.to_hash, **@options) @last_output_time = Clock.now return self end |
#The subject of the progress indicator.=(subjectoftheprogressindicator. = (value)) ⇒ Object
37 |
# File 'lib/console/progress.rb', line 37 attr :subject |
#The time the progress indicator was started.=(timetheprogressindicatorwasstarted. = (value)) ⇒ Object
43 |
# File 'lib/console/progress.rb', line 43 attr :start_time |
#to_hash ⇒ Object
Generate an appropriate event for the progress indicator.
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/console/progress.rb', line 83 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
132 133 134 135 136 137 138 |
# File 'lib/console/progress.rb', line 132 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 |