-
Notifications
You must be signed in to change notification settings - Fork 42
/
spinner_demo.go
76 lines (64 loc) · 1.74 KB
/
spinner_demo.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package cmd
import (
"github.com/gookit/cliapp"
"github.com/gookit/cliapp/progress"
"time"
)
type spinnerDemo struct {
speed int
themeNum int
}
func SpinnerDemoCmd() *cliapp.Command {
sd := &spinnerDemo{}
return &cliapp.Command{
Name: "spinner",
UseFor: "there are some CLI spinner bar run demos",
Aliases: []string{"spr", "spr:demo"},
Func: sd.Run,
Config: func(c *cliapp.Command) {
c.IntOpt(&sd.speed, "speed", "s", 100, "setting the spinner running speed")
c.IntOpt(&sd.themeNum, "theme-num", "t", 0, "setting the theme numbering. allow: 0 - 16")
c.AddArg("name",
"spinner type name. allow: loading,roundTrip",
false,
)
},
Examples: `Loading spinner:
{$fullCmd} loading
roundTrip spinner:
{$fullCmd} roundTrip`,
}
}
func (sd *spinnerDemo) Run(c *cliapp.Command, _ []string) int {
name := c.Arg("name").String()
switch name {
case "", "spinner", "load", "loading":
sd.runLoadingSpinner()
case "rt", "roundTrip":
sd.runRoundTripSpinner()
default:
return c.Errorf("the spinner type name only allow: loading,roundTrip. input is: %s", name)
}
return 0
}
func (sd *spinnerDemo) runRoundTripSpinner() {
s := progress.RoundTripSpinner(
progress.GetCharTheme(sd.themeNum),
time.Duration(sd.speed)*time.Millisecond,
)
// s.Start("%s work handling ... ...")
s.Start("[%s] work handling ... ...")
// Run for some time to simulate work
time.Sleep(4 * time.Second)
s.Stop("work handle complete")
}
func (sd *spinnerDemo) runLoadingSpinner() {
s := progress.LoadingSpinner(
progress.GetCharsTheme(sd.themeNum),
time.Duration(sd.speed)*time.Millisecond,
)
s.Start("%s work handling ... ...")
// Run for some time to simulate work
time.Sleep(4 * time.Second)
s.Stop("work handle complete")
}