summaryrefslogtreecommitdiff
path: root/animation.go
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-11-11 18:05:30 +0800
committerEltrac <hi@guhub.cn>2025-11-11 18:05:30 +0800
commita461fdfd9b010dafbdf422002c7549cb21023fc4 (patch)
tree4d3c02441dc10218c1abdc736014e12659f65aae /animation.go
parent363bc4114ccf8e59e7ddbe32361fd8d6eeca93ca (diff)
feat(animation): animate loading prompt
Diffstat (limited to 'animation.go')
-rw-r--r--animation.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/animation.go b/animation.go
new file mode 100644
index 0000000..226f309
--- /dev/null
+++ b/animation.go
@@ -0,0 +1,49 @@
+package main
+
+import (
+ "fmt"
+ "time"
+ "strings"
+ "github.com/fatih/color"
+)
+
+var loadingDone chan bool
+
+func showLoadingPrompt(msg string) {
+ if loadingDone != nil {
+ return
+ }
+
+ loadingDone = make(chan bool)
+ go func() {
+ white := color.New(color.FgWhite)
+ for {
+ select {
+ case <-loadingDone:
+ fmt.Print("\r") // move cursor to line start
+ fmt.Print(strings.Repeat(" ", len(msg)+4))
+ fmt.Print("\r") // clear line
+ return
+ default:
+ white.Printf("\r%s", msg)
+ for i := range 4 {
+ if i == 3 {
+ white.Print("?")
+ } else {
+ white.Print(".")
+ }
+ time.Sleep(700 * time.Millisecond)
+ }
+ }
+ }
+ }()
+}
+
+// hideLoadingPrompt stops the animation cleanly.
+func hideLoadingPrompt() {
+ if loadingDone != nil {
+ close(loadingDone)
+ loadingDone = nil
+ fmt.Print("\r\033[2K")
+ }
+}