Skip to content

Instantly share code, notes, and snippets.

@trajber
Last active February 18, 2022 17:39
Show Gist options
  • Save trajber/2c0825e4f5bc35574e2b9831f25d3437 to your computer and use it in GitHub Desktop.
Save trajber/2c0825e4f5bc35574e2b9831f25d3437 to your computer and use it in GitHub Desktop.
Super simple emoji spinner/waiting animation in Golang
package main
import (
"fmt"
"time"
)
var (
monkeys = []string{"πŸ™ˆ", "πŸ™ˆ", "πŸ™‰", "πŸ™Š"}
clocks = []string{"πŸ•›", "πŸ•", "πŸ•‘", "πŸ•’", "πŸ•“", "πŸ•”", "πŸ••", "πŸ•–", "πŸ•—", "πŸ•˜", "πŸ•™", "πŸ•š"}
moons = []string{"πŸŒ‘", "πŸŒ’", "πŸŒ“", "πŸŒ”", "πŸŒ•", "πŸŒ–", "πŸŒ—", "🌘"}
smiley = []string{"πŸ˜„", "πŸ€”", "😐"}
)
func spinner(message string, animation []string, interval time.Duration) func() {
ch := make(chan struct{})
i := 0
go func() {
for {
select {
case <-time.After(interval):
fmt.Printf("\r%s %s", animation[i], message)
i = (i + 1) % len(animation)
case <-ch:
return
}
}
}()
return func() { close(ch) }
}
func main() {
// choose your animation and its interval
cancel := spinner("Waiting...", monkeys, 200*time.Millisecond)
// some long operation...
time.Sleep(20 * time.Second)
// call cancel function when done
cancel()
}
@trajber
Copy link
Author

trajber commented Feb 18, 2022

⚠️ It only works if your output is a terminal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment