Class: Zenspec::ProgressLoader
- Inherits:
-
Object
- Object
- Zenspec::ProgressLoader
- Defined in:
- lib/zenspec/progress_loader.rb
Overview
Docker-style progress loader for displaying progress in terminal Displays progress bars similar to Docker's layer download progress
Constant Summary collapse
- DEFAULT_WIDTH =
Default width for the progress bar
40
Instance Attribute Summary collapse
-
#current ⇒ Object
readonly
Returns the value of attribute current.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
-
#elapsed_time ⇒ Float
Calculate elapsed time.
-
#estimated_time_remaining ⇒ Float?
Calculate estimated time remaining.
-
#finish(description: nil) ⇒ Object
Mark the progress as finished.
-
#format_time(seconds) ⇒ String
Format time duration.
-
#increment(description: nil) ⇒ Object
Increment progress by one.
-
#initialize(total:, width: DEFAULT_WIDTH, description: nil, output: $stderr) ⇒ ProgressLoader
constructor
A new instance of ProgressLoader.
-
#percentage ⇒ Integer
Calculate current percentage.
-
#update(current, description: nil) ⇒ Object
Update the progress.
Constructor Details
#initialize(total:, width: DEFAULT_WIDTH, description: nil, output: $stderr) ⇒ ProgressLoader
Returns a new instance of ProgressLoader.
29 30 31 32 33 34 35 36 37 |
# File 'lib/zenspec/progress_loader.rb', line 29 def initialize(total:, width: DEFAULT_WIDTH, description: nil, output: $stderr) @total = total @width = width @current = 0 @description = description @output = output @finished = false @start_time = Time.now end |
Instance Attribute Details
#current ⇒ Object (readonly)
Returns the value of attribute current.
20 21 22 |
# File 'lib/zenspec/progress_loader.rb', line 20 def current @current end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
20 21 22 |
# File 'lib/zenspec/progress_loader.rb', line 20 def total @total end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
20 21 22 |
# File 'lib/zenspec/progress_loader.rb', line 20 def width @width end |
Instance Method Details
#elapsed_time ⇒ Float
Calculate elapsed time
74 75 76 |
# File 'lib/zenspec/progress_loader.rb', line 74 def elapsed_time Time.now - @start_time end |
#estimated_time_remaining ⇒ Float?
Calculate estimated time remaining
80 81 82 83 84 85 86 87 |
# File 'lib/zenspec/progress_loader.rb', line 80 def estimated_time_remaining return nil if @current.zero? elapsed = elapsed_time rate = @current.to_f / elapsed remaining_items = @total - @current remaining_items / rate end |
#finish(description: nil) ⇒ Object
Mark the progress as finished
56 57 58 59 60 61 62 |
# File 'lib/zenspec/progress_loader.rb', line 56 def finish(description: nil) @current = @total @description = description if description @finished = true render @output.puts # New line after completion end |
#format_time(seconds) ⇒ String
Format time duration
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/zenspec/progress_loader.rb', line 92 def format_time(seconds) return "0s" if seconds.nil? || seconds.zero? if seconds < 60 "#{seconds.round}s" elsif seconds < 3600 minutes = (seconds / 60).round "#{minutes}m" else hours = (seconds / 3600).round "#{hours}h" end end |
#increment(description: nil) ⇒ Object
Increment progress by one
50 51 52 |
# File 'lib/zenspec/progress_loader.rb', line 50 def increment(description: nil) update(@current + 1, description: description) end |
#percentage ⇒ Integer
Calculate current percentage
66 67 68 69 70 |
# File 'lib/zenspec/progress_loader.rb', line 66 def percentage return 100 if @total.zero? ((@current.to_f / @total) * 100).round end |
#update(current, description: nil) ⇒ Object
Update the progress
42 43 44 45 46 |
# File 'lib/zenspec/progress_loader.rb', line 42 def update(current, description: nil) @current = current @description = description if description render unless @finished end |