summaryrefslogtreecommitdiff
path: root/animation.go
blob: 226f3096f72c4f9f00c56f3a069aefbd59e67db6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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")
	}
}