Skip to content

Commit

Permalink
Merge pull request #380 from orobardet/spinner-with-info-printer
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Jul 22, 2022
2 parents 86a935d + 4c9ffb6 commit d436318
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
18 changes: 18 additions & 0 deletions _examples/spinner/demo/main.go
Expand Up @@ -7,6 +7,11 @@ import (
)

func main() {
// Create and start a fork of the default spinner.
spinnerInfo, _ := pterm.DefaultSpinner.Start("Some informational action...")
time.Sleep(time.Second * 2) // Simulate 3 seconds of processing something.
spinnerInfo.Info() // Resolve spinner with error message.

// Create and start a fork of the default spinner.
spinnerSuccess, _ := pterm.DefaultSpinner.Start("Doing something important... (will succeed)")
time.Sleep(time.Second * 2) // Simulate 3 seconds of processing something.
Expand All @@ -22,6 +27,19 @@ func main() {
time.Sleep(time.Second * 2) // Simulate 3 seconds of processing something.
spinnerFail.Fail() // Resolve spinner with error message.

// Create and start a fork of the default spinner.
spinnerNochange, _ := pterm.DefaultSpinner.Start("Checking something important... (will result in no change)")
// Replace the InfoPrinter with a custom "NOCHG" one
spinnerNochange.InfoPrinter = &pterm.PrefixPrinter{
MessageStyle: &pterm.Style{pterm.FgLightBlue},
Prefix: pterm.Prefix{
Style: &pterm.Style{pterm.FgBlack, pterm.BgLightBlue},
Text: " NOCHG ",
},
}
time.Sleep(time.Second * 2) // Simulate 3 seconds of processing something.
spinnerNochange.Info("No change were required") // Resolve spinner with error message.

// Create and start a fork of the default spinner.
spinnerLiveText, _ := pterm.DefaultSpinner.Start("Doing a lot of stuff...")
time.Sleep(time.Second) // Simulate 2 seconds of processing something.
Expand Down
17 changes: 17 additions & 0 deletions spinner_printer.go
Expand Up @@ -18,6 +18,7 @@ var DefaultSpinner = SpinnerPrinter{
TimerRoundingFactor: time.Second,
TimerStyle: &ThemeDefault.TimerStyle,
MessageStyle: &ThemeDefault.SpinnerTextStyle,
InfoPrinter: &Info,
SuccessPrinter: &Success,
FailPrinter: &Error,
WarningPrinter: &Warning,
Expand All @@ -32,6 +33,7 @@ type SpinnerPrinter struct {
Style *Style
Delay time.Duration
MessageStyle *Style
InfoPrinter TextPrinter
SuccessPrinter TextPrinter
FailPrinter TextPrinter
WarningPrinter TextPrinter
Expand Down Expand Up @@ -187,6 +189,21 @@ func (s *SpinnerPrinter) GenericStop() (*LivePrinter, error) {
return &lp, nil
}

// Info displays an info message
// If no message is given, the text of the SpinnerPrinter will be reused as the default message.
func (s *SpinnerPrinter) Info(message ...interface{}) {
if s.InfoPrinter == nil {
s.InfoPrinter = &Info
}

if len(message) == 0 {
message = []interface{}{s.Text}
}
fClearLine(s.Writer)
Fprinto(s.Writer, s.InfoPrinter.Sprint(message...))
_ = s.Stop()
}

// Success displays the success printer.
// If no message is given, the text of the SpinnerPrinter will be reused as the default message.
func (s *SpinnerPrinter) Success(message ...interface{}) {
Expand Down
10 changes: 10 additions & 0 deletions spinner_printer_test.go
Expand Up @@ -12,6 +12,7 @@ import (

func TestSpinnerPrinter_NilPrint(t *testing.T) {
p := pterm.SpinnerPrinter{}
p.Info()
p.Success()
p.Warning()
p.Fail()
Expand Down Expand Up @@ -43,6 +44,13 @@ func TestSpinnerPrinter_GenericStop(t *testing.T) {
p.GenericStop()
}

func TestSpinnerPrinter_Info(t *testing.T) {
p := pterm.DefaultSpinner
testPrintContains(t, func(w io.Writer, a interface{}) {
p.Info(a)
})
}

func TestSpinnerPrinter_Success(t *testing.T) {
p := pterm.DefaultSpinner
testPrintContains(t, func(w io.Writer, a interface{}) {
Expand Down Expand Up @@ -172,6 +180,7 @@ func TestSpinnerPrinter_DifferentVariations(t *testing.T) {
Style *pterm.Style
Delay time.Duration
MessageStyle *pterm.Style
InfoPrinter pterm.TextPrinter
SuccessPrinter pterm.TextPrinter
FailPrinter pterm.TextPrinter
WarningPrinter pterm.TextPrinter
Expand All @@ -198,6 +207,7 @@ func TestSpinnerPrinter_DifferentVariations(t *testing.T) {
Style: tt.fields.Style,
Delay: tt.fields.Delay,
MessageStyle: tt.fields.MessageStyle,
InfoPrinter: tt.fields.InfoPrinter,
SuccessPrinter: tt.fields.SuccessPrinter,
FailPrinter: tt.fields.FailPrinter,
WarningPrinter: tt.fields.WarningPrinter,
Expand Down

0 comments on commit d436318

Please sign in to comment.