diff options
Diffstat (limited to 'animation.go')
| -rw-r--r-- | animation.go | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/animation.go b/animation.go index 5d2bc47..8bd94f5 100644 --- a/animation.go +++ b/animation.go @@ -7,22 +7,27 @@ import ( "time" ) -var loadingDone chan bool +var ( + loadingDone chan struct{} + loadingStopped chan struct{} +) func showLoadingPrompt(msg string) { if loadingDone != nil { return } - loadingDone = make(chan bool) + 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") // move cursor to line start + fmt.Print("\r") fmt.Print(strings.Repeat(" ", len(msg)+4)) - fmt.Print("\r") // clear line + fmt.Print("\r") return default: white.Printf("\r%s", msg) @@ -39,10 +44,10 @@ func showLoadingPrompt(msg string) { }() } -// hideLoadingPrompt stops the animation cleanly. func hideLoadingPrompt() { if loadingDone != nil { close(loadingDone) + <-loadingStopped loadingDone = nil fmt.Print("\r\033[2K") } |
