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 +++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 3 ++- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 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") + } +} diff --git a/main.go b/main.go index 6ea7ed7..5b7147c 100644 --- a/main.go +++ b/main.go @@ -25,10 +25,11 @@ func main() { } pkgName := cmd.Args().First() // prompt - color.White("What the heck is \"%s\" ...?", pkgName) + showLoadingPrompt(fmt.Sprintf("What the heck is \"%s\"", pkgName)) // search and print formulae, cask := GetBrewInfo(pkgName) data := UnifyInfo(formulae, cask, pkgName) + hideLoadingPrompt() PrintInfo(data) return nil }, -- cgit v1.2.3