diff options
| author | Eltrac <hi@guhub.cn> | 2025-11-11 18:05:30 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-11-11 18:05:30 +0800 |
| commit | a461fdfd9b010dafbdf422002c7549cb21023fc4 (patch) | |
| tree | 4d3c02441dc10218c1abdc736014e12659f65aae | |
| parent | 363bc4114ccf8e59e7ddbe32361fd8d6eeca93ca (diff) | |
feat(animation): animate loading prompt
| -rw-r--r-- | animation.go | 49 | ||||
| -rw-r--r-- | main.go | 3 |
2 files changed, 51 insertions, 1 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") + } +} @@ -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 }, |
