Class: SwarmCLI::UI::State::SpinnerManager
- Inherits:
-
Object
- Object
- SwarmCLI::UI::State::SpinnerManager
- Defined in:
- lib/swarm_cli/ui/state/spinner_manager.rb
Overview
Manages active spinners with elapsed time display
Instance Method Summary collapse
-
#active?(key) ⇒ Boolean
Check if a spinner is active.
-
#error(key, message = "failed") ⇒ Object
Stop spinner with error.
-
#initialize ⇒ SpinnerManager
constructor
A new instance of SpinnerManager.
-
#pause_all ⇒ void
Pause all active spinners (for interactive debugging).
-
#resume_all ⇒ void
Resume all paused spinners.
-
#start(key, message, format: :dots) ⇒ TTY::Spinner
Start a spinner with elapsed time tracking.
-
#stop(key) ⇒ Object
Stop spinner without success/error (just stop).
-
#stop_all ⇒ Object
Stop all active spinners.
-
#success(key, message = "completed") ⇒ Object
Stop spinner with success.
Constructor Details
#initialize ⇒ SpinnerManager
Returns a new instance of SpinnerManager.
8 9 10 11 |
# File 'lib/swarm_cli/ui/state/spinner_manager.rb', line 8 def initialize @active_spinners = {} @time_updaters = {} end |
Instance Method Details
#active?(key) ⇒ Boolean
Check if a spinner is active
108 109 110 |
# File 'lib/swarm_cli/ui/state/spinner_manager.rb', line 108 def active?(key) @active_spinners.key?(key) end |
#error(key, message = "failed") ⇒ Object
Stop spinner with error
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/swarm_cli/ui/state/spinner_manager.rb', line 73 def error(key, = "failed") spinner = @active_spinners[key] return unless spinner # Kill time updater kill_updater(key) # Show final time final_time = format_duration(spinner.duration || 0) spinner.error("#{} (#{final_time})") cleanup(key) end |
#pause_all ⇒ void
This method returns an undefined value.
Pause all active spinners (for interactive debugging)
This temporarily stops spinner animation while preserving state, allowing interactive sessions like binding.irb to run cleanly.
118 119 120 121 122 123 124 |
# File 'lib/swarm_cli/ui/state/spinner_manager.rb', line 118 def pause_all @active_spinners.each_value do |spinner| spinner.stop if spinner.spinning? end # Keep time updaters running (they'll safely handle stopped spinners) end |
#resume_all ⇒ void
This method returns an undefined value.
Resume all paused spinners
Restarts spinner animation for all spinners that were paused.
131 132 133 134 135 |
# File 'lib/swarm_cli/ui/state/spinner_manager.rb', line 131 def resume_all @active_spinners.each_value do |spinner| spinner.auto_spin unless spinner.spinning? end end |
#start(key, message, format: :dots) ⇒ TTY::Spinner
Start a spinner with elapsed time tracking
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/swarm_cli/ui/state/spinner_manager.rb', line 19 def start(key, , format: :dots) # Stop any existing spinner with this key stop(key) if @active_spinners[key] # Create spinner with elapsed time token spinner = TTY::Spinner.new( "[:spinner] #{} (:elapsed)", format: format, hide_cursor: true, ) spinner.auto_spin # Spawn thread to update elapsed time every 1 second # This is 10x slower than spinner animation (100ms), preventing flicker @time_updaters[key] = Thread.new do loop do elapsed = spinner.duration break unless elapsed formatted_time = format_duration(elapsed) spinner.update(elapsed: formatted_time) sleep(1.0) # 1s refresh rate - smooth without flicker rescue StandardError break end end @active_spinners[key] = spinner spinner end |
#stop(key) ⇒ Object
Stop spinner without success/error (just stop)
90 91 92 93 94 95 96 97 |
# File 'lib/swarm_cli/ui/state/spinner_manager.rb', line 90 def stop(key) spinner = @active_spinners[key] return unless spinner kill_updater(key) spinner.stop cleanup(key) end |
#stop_all ⇒ Object
Stop all active spinners
100 101 102 |
# File 'lib/swarm_cli/ui/state/spinner_manager.rb', line 100 def stop_all @active_spinners.keys.each { |key| stop(key) } end |
#success(key, message = "completed") ⇒ Object
Stop spinner with success
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/swarm_cli/ui/state/spinner_manager.rb', line 55 def success(key, = "completed") spinner = @active_spinners[key] return unless spinner # Kill time updater kill_updater(key) # Show final time final_time = format_duration(spinner.duration || 0) spinner.success("#{} (#{final_time})") cleanup(key) end |