/
waiter.go
42 lines (37 loc) · 915 Bytes
/
waiter.go
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
package commands
import (
"fmt"
"sync"
"time"
"github.com/Skarlso/go-furnace/config"
"github.com/fatih/color"
)
var yellow = color.New(color.FgYellow).SprintFunc()
var red = color.New(color.FgRed).SprintFunc()
var keyName = color.New(color.FgWhite, color.Bold).SprintFunc()
// WaitForFunctionWithStatusOutput waits for a function to complete its action.
func waitForFunctionWithStatusOutput(state string, freq int, f func()) {
var wg sync.WaitGroup
wg.Add(1)
done := make(chan bool)
go func() {
defer wg.Done()
f()
done <- true
}()
go func() {
counter := 0
for {
counter = (counter + 1) % len(config.Spinners[config.SPINNER])
fmt.Printf("\r[%s] Waiting for state: %s", yellow(string(config.Spinners[config.SPINNER][counter])), red(state))
time.Sleep(time.Duration(freq) * time.Second)
select {
case <-done:
fmt.Println()
break
default:
}
}
}()
wg.Wait()
}