From a461fdfd9b010dafbdf422002c7549cb21023fc4 Mon Sep 17 00:00:00 2001 From: Eltrac Date: Tue, 11 Nov 2025 18:05:30 +0800 Subject: feat(animation): animate loading prompt --- animation.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 animation.go (limited to 'animation.go') 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") + } +} -- cgit v1.2.3