Class: Factorix::Progress::Presenter
- Inherits:
-
Object
- Object
- Factorix::Progress::Presenter
- Defined in:
- lib/factorix/progress/presenter.rb
Overview
Progress presenter implementation
This class provides a simple progress presentation interface using tty-progressbar.
Instance Method Summary collapse
-
#finish ⇒ void
Mark the progress presenter as finished.
-
#increase_total(increment) ⇒ void
Increase the total count dynamically.
-
#initialize(title: "Progress", output: $stderr) ⇒ Presenter
constructor
Create a new progress presenter.
-
#start(total: nil, format: nil) ⇒ void
Start the progress presentation with a specific total.
-
#update(current = nil) ⇒ void
Update the progress presenter to a specific value.
Constructor Details
#initialize(title: "Progress", output: $stderr) ⇒ Presenter
Create a new progress presenter
15 16 17 18 19 |
# File 'lib/factorix/progress/presenter.rb', line 15 def initialize(title: "Progress", output: $stderr) @title = title @output = output @tty_bar = nil end |
Instance Method Details
#finish ⇒ void
This method returns an undefined value.
Mark the progress presenter as finished
64 |
# File 'lib/factorix/progress/presenter.rb', line 64 def finish = @tty_bar&.finish |
#increase_total(increment) ⇒ void
This method returns an undefined value.
Increase the total count dynamically
54 55 56 57 58 59 |
# File 'lib/factorix/progress/presenter.rb', line 54 def increase_total(increment) return unless @tty_bar current_total = @tty_bar.total || 0 @tty_bar.update(total: current_total + increment) end |
#start(total: nil, format: nil) ⇒ void
This method returns an undefined value.
Start the progress presentation with a specific total
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/factorix/progress/presenter.rb', line 26 def start(total: nil, format: nil) format ||= total.nil? ? "#{@title} [:bar] :current" : "#{@title} [:bar] :percent :current/:total" @tty_bar = TTY::ProgressBar.new( format, total:, output: @output, frequency: 1, # Always update (important for testing with StringIO) force: true # Force output even when not a TTY ) end |
#update(current = nil) ⇒ void
This method returns an undefined value.
Update the progress presenter to a specific value
41 42 43 44 45 46 47 48 |
# File 'lib/factorix/progress/presenter.rb', line 41 def update(current=nil) if current @tty_bar&.current = current else # For indeterminate progress, just advance @tty_bar&.advance end end |