package cli import ( "fmt" "time" ) // ShowWaitAnimation "draws" an animated ellipses to stdout until something is // received on the signal channel. An empty string sent to the channel to // noftify the caller that the animation has completed (carriage returned). func ShowWaitAnimation(signal chan any) { animationStep := 0 for { select { case _ = <-signal: fmt.Print("\r") signal <- "" return default: modSix := animationStep % 6 if modSix == 3 || modSix == 0 { fmt.Print("\r") } if modSix < 3 { fmt.Print(".") } else { fmt.Print(" ") } animationStep++ time.Sleep(250 * time.Millisecond) } } } // HandledDelayedResponse writes a waiting animation (abusing \r) and the // content received on the response channel to stdout. Blocks until the channel // is closed. func HandleDelayedResponse(response chan string) { waitSignal := make(chan any) go ShowWaitAnimation(waitSignal) firstChunk := true for chunk := range response { if firstChunk { waitSignal <- "" <-waitSignal firstChunk = false } fmt.Print(chunk) } }