Files
asciinema/util/spinner.go

40 lines
664 B
Go
Raw Normal View History

2016-02-21 16:58:39 +01:00
package util
import (
"fmt"
"os"
"time"
)
var spinner = []rune("▉▊▋▌▍▎▏▎▍▌▋▊▉")
func WithSpinner(delay int, f func()) {
stopChan := make(chan struct{})
go func() {
2016-02-21 18:22:37 +01:00
select {
case <-stopChan:
return
case <-time.After(time.Duration(delay) * time.Millisecond):
}
2016-02-21 16:58:39 +01:00
i := 0
fmt.Fprintf(os.Stdout, "\x1b[?25l") // hide cursor
for {
select {
case <-stopChan:
return
case <-time.After(100 * time.Millisecond):
fmt.Fprintf(os.Stdout, "\r%c", spinner[i])
i = (i + 1) % len(spinner)
}
}
}()
f()
close(stopChan)
fmt.Fprintf(os.Stdout, "\r\x1b[K\x1b[?25h") // clear line and show cursor back
}