package main import ( "fmt" "github.com/fatih/color" "strings" "time" ) var ( loadingDone chan struct{} loadingStopped chan struct{} ) func showLoadingPrompt(msg string) { if loadingDone != nil { return } loadingDone = make(chan struct{}) loadingStopped = make(chan struct{}) go func() { defer close(loadingStopped) white := color.New(color.FgWhite) for { select { case <-loadingDone: fmt.Print("\r") fmt.Print(strings.Repeat(" ", len(msg)+4)) fmt.Print("\r") return default: white.Printf("\r%s", msg) for i := range 4 { if i == 3 { white.Print("?") } else { white.Print(".") } time.Sleep(700 * time.Millisecond) } } } }() } func hideLoadingPrompt() { if loadingDone != nil { close(loadingDone) <-loadingStopped loadingDone = nil fmt.Print("\r\033[2K") } }