Skip to content

Commit

Permalink
Update spinner api to store and stop all spinners (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
chandrareddyp authored Apr 10, 2024
1 parent 1b32717 commit 72cfcba
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions component/output_spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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()
}
}
}

Expand Down

0 comments on commit 72cfcba

Please sign in to comment.