Last active
February 18, 2022 17:39
-
-
Save trajber/2c0825e4f5bc35574e2b9831f25d3437 to your computer and use it in GitHub Desktop.
Super simple emoji spinner/waiting animation in Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
Author
trajber
commented
Feb 18, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment