/
spin_progress.go
54 lines (44 loc) · 1.19 KB
/
spin_progress.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
43
44
45
46
47
48
49
50
51
52
53
54
package ui
import (
"fmt"
"time"
"github.com/briandowns/spinner"
"github.com/kyokomi/emoji"
)
// This file implements the SpinProgress form of the Progress
// interface. This encapsulates the currently used progress
// indicator, a spinner.
type SpinProgress struct {
s *spinner.Spinner
}
// Standard values for the current spinner-based progress
const spinSet = 37
const spinTime = 100 * time.Millisecond
func NewSpinProgress(message string) *SpinProgress {
message = emoji.Sprint(message + " :zzz:")
s := &SpinProgress{
s: spinner.New(spinner.CharSets[spinSet], spinTime),
}
s.s.Suffix = message
s.s.Start()
return s
}
func (p *SpinProgress) Start() {
p.s.Start()
}
func (p *SpinProgress) Stop() {
p.s.Stop()
}
// ChangeMessagef extends the spinner-based progress with the ability
// to change the message mid-flight
func (p *SpinProgress) ChangeMessagef(message string, a ...interface{}) {
p.ChangeMessage(fmt.Sprintf(message, a...))
}
// ChangeMessage extends the spinner-based progress with the ability
// to change the message mid-flight
func (p *SpinProgress) ChangeMessage(message string) {
message = emoji.Sprint(message + " :zzz:")
p.s.Stop()
p.s.Suffix = message
p.s.Start()
}