theckman / yacspin

Yet Another CLi Spinner; providing over 80 easy to use and customizable terminal spinners for multiple OSes
Apache License 2.0
434 stars 13 forks source link

Add support for logging progress messages #69

Open weikanglim opened 1 year ago

weikanglim commented 1 year ago

It is sometimes desirable to log messages while rendering the spinner. Users can do this currently is by not setting a StopMessage and using Stop + Start to temporarily pause the spinner, remove the spinner text and log the message to standard output (example program below).

While this does work, I think it is worthwhile to add a spinner.LogMessage API that adds this functionality. Would love to contribute changes here if this idea makes sense.

package main

import (
    "fmt"
    "time"

    "github.com/theckman/yacspin"
)

func logProgress(spin *yacspin.Spinner, message string) {
    spin.Stop()
    fmt.Println(message)
    spin.Start()
}

func main() {
    spin, _ := yacspin.New(yacspin.Config{
        Frequency: time.Millisecond * 200,
        CharSet:   yacspin.CharSets[9],
        Message:   " Spinning...",
    })

    spin.Start()

    for i := 0; i < 10; i++ {
        logProgress(spin, fmt.Sprintf("Step %d completed.", i))
        time.Sleep(1000 * time.Millisecond)
    }

    spin.Stop()
}