How TTY::Spinner::Multi works with auto_spin
masterTo create a top-level spinner that tracks the activity of all registered sub-spinners, initialize TTY::Spinner::Multi with a message. The top-level spinner will automatically animate whenever at least one registered sub-spinner starts spinning.
There are two ways to manage the lifecycle of sub-spinners:
- Manual Async: Register spinners without tasks, start them manually with
#auto_spin, and control their completion (success/error) manually. If a sub-spinner is marked with#error, the top-level multi-spinner will also be marked as a failure. - Auto Async Tasks: Register spinners with a block containing the task logic. The task receives the spinner instance as an argument. When
#auto_spinis called on the multi-spinner, it kicks off all tasks. The spinners will automatically animate and finish based on the outcome of the blocks.
# Example of Auto Async Tasks
multi_spinner = TTY::Spinner::Multi.new("[:spinner] top")
multi_spinner.register("[:spinner] one") { |sp| sleep(2); sp.success("yes 2") }
multi_spinner.register("[:spinner] two") { |sp| sleep(3); sp.error("no 2") }
multi_spinner.auto_spin