tj / go-spin

Terminal spinner package for Golang
MIT License
533 stars 19 forks source link

new example #13

Open emicklei opened 4 years ago

emicklei commented 4 years ago

with the following definition

func showSpinnerWhile() func() {
    spinner := spin.New()
    spinner.Set(spin.Box1)
    done := make(chan bool)
    go func() {
        for {
            select {
            case <-done:
            default:
                // reprint new spinner state
                fmt.Fprintf(os.Stderr, "\r%s", spinner.Next())
                time.Sleep(100 * time.Millisecond)
            }
        }
    }()
    return func() {
        done <- true
        // remove spinner
        fmt.Fprintf(os.Stderr, "\033[%dD", 1)
    }
}

a program can show a spinner while doing a lengthy operation like this

done := showSpinnerWhile()

// do the hard work

done()

The effect is that the spinner character is removed from the terminal before proceeding.

I use this in my https://github.com/emicklei/gsuite tool