File: run-with-spinner.go

package info (click to toggle)
golang-github-pterm-pterm 0.12.79-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,640 kB
  • sloc: makefile: 4
file content (24 lines) | stat: -rw-r--r-- 713 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package putils

import "github.com/pterm/pterm"

// RunWithSpinner starts a spinner, then runs a function and after the function is done, the spinner will stop again.
func RunWithSpinner(spinner *pterm.SpinnerPrinter, f func(spinner *pterm.SpinnerPrinter) error) error {
	s, err := spinner.Start()
	if err != nil {
		return err
	}

	err = f(s)

	if s.IsActive {
		s.Stop()
	}

	return err
}

// RunWithDefaultSpinner starts a default spinner, then runs a function and after the function is done, the spinner will stop again.
func RunWithDefaultSpinner(initzialSpinnerText string, f func(spinner *pterm.SpinnerPrinter) error) error {
	return RunWithSpinner(pterm.DefaultSpinner.WithText(initzialSpinnerText), f)
}