Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds InfoPrinter in SpinnerPrinter #380

Merged
merged 1 commit into from Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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