From 72cfcba566bea2bfba12523bb1c3e0348dc3f8b4 Mon Sep 17 00:00:00 2001 From: Chandra P Date: Wed, 10 Apr 2024 11:46:49 -0500 Subject: [PATCH] Update spinner api to store and stop all spinners (#170) --- component/output_spinner.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/component/output_spinner.go b/component/output_spinner.go index 224ee93e0..c0faff1f7 100644 --- a/component/output_spinner.go +++ b/component/output_spinner.go @@ -44,6 +44,8 @@ type outputwriterspinner struct { // OutputWriterSpinnerOption is an option to configure outputwriterspinner type OutputWriterSpinnerOption func(*outputwriterspinner) +var spinners []OutputWriterSpinner + // WithSpinnerFinalText sets the spinner final text and prefix log indicator // (log.LogTypeOUTPUT can be used for no prefix) func WithSpinnerFinalText(finalText string, prefix log.LogType) OutputWriterSpinnerOption { @@ -153,9 +155,14 @@ func initializeSpinner(ows *outputwriterspinner) OutputWriterSpinner { ows.spinner.Start() } } + storeSpinners(ows) return ows } +func storeSpinners(s OutputWriterSpinner) { + spinners = append(spinners, s) +} + // RenderWithSpinner stops the running spinner instance, displays FinalText if set, then renders the output // // Deprecated: RenderWithSpinner is being deprecated in favor of Render. @@ -177,11 +184,24 @@ func (ows *outputwriterspinner) StartSpinner() { } } -// StopSpinner stops the running spinner instance, displays FinalText if set +// StopSpinner stops the running spinner instances and displays FinalText if set. +// StopSpinner needs to be called explicitly to stop the spinner. +// It helps to stop all active spinners when the command is completed or interrupted. func (ows *outputwriterspinner) StopSpinner() { if ows.spinner != nil && ows.spinner.Active() { ows.spinner.Stop() - fmt.Fprintln(ows.out) + if ows.spinnerFinalText != "" { + fmt.Fprintln(ows.out) + } + } +} + +// StopAllSpinners stops all running spinners if any +func StopAllSpinners() { + for _, s := range spinners { + if s != nil { + s.StopSpinner() + } } }