summaryrefslogtreecommitdiff
path: root/animation.go
blob: 8bd94f5b074f0dfe8353b84b8cd6168bd7385d4b (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
50
51
52
53
54
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")
	}
}