Skip to content

Commit 1c96742

Browse files
committed
feat: add wait print function
1 parent e5202e5 commit 1c96742

File tree

6 files changed

+159
-17
lines changed

6 files changed

+159
-17
lines changed

cmd/sponge/commands/generate/common.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package generate
44
import (
55
"bufio"
66
"bytes"
7+
"context"
78
"encoding/json"
89
"errors"
910
"fmt"
@@ -1054,3 +1055,30 @@ func getGRPCServiceFields() []replacer.Field {
10541055
},
10551056
}
10561057
}
1058+
1059+
func clearCurrentLine() {
1060+
fmt.Print("\033[2K\r")
1061+
}
1062+
1063+
func PrintWaiting(ctx context.Context, runningTip string, finishTip string) {
1064+
defer func() {
1065+
clearCurrentLine()
1066+
fmt.Println(finishTip)
1067+
}()
1068+
symbols := []string{runningTip + ".", runningTip + "..", runningTip + "...", runningTip + "....", runningTip + ".....", runningTip + "......"}
1069+
index := 0
1070+
fmt.Printf("\r%s", symbols[index])
1071+
for {
1072+
select {
1073+
case <-ctx.Done():
1074+
return
1075+
case <-time.After(500 * time.Millisecond):
1076+
index++
1077+
if index >= len(symbols) {
1078+
index = 0
1079+
clearCurrentLine()
1080+
}
1081+
fmt.Printf("\r%s", symbols[index])
1082+
}
1083+
}
1084+
}

cmd/sponge/commands/init.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package commands
22

33
import (
4-
"fmt"
5-
64
"github.com/fatih/color"
75
"github.com/spf13/cobra"
86
)
@@ -20,8 +18,6 @@ func InitCommand() *cobra.Command {
2018
SilenceErrors: true,
2119
SilenceUsage: true,
2220
RunE: func(cmd *cobra.Command, args []string) error {
23-
fmt.Println("initializing sponge, please wait a moment ......")
24-
2521
targetVersion := latestVersion
2622
// download sponge template code
2723
_, err := runUpgrade(targetVersion)

cmd/sponge/commands/plugins.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,31 +115,31 @@ func showDependencyPlugins(installedNames []string, lackNames []string) {
115115
var content string
116116

117117
if len(installedNames) > 0 {
118-
content = "Installed dependency plugins:\n"
118+
content = "installed dependency plugins:\n"
119119
for _, name := range installedNames {
120120
content += " " + installedSymbol + " " + name + "\n"
121121
}
122122
}
123123

124124
if len(lackNames) > 0 {
125-
content += "\nUninstalled dependency plugins:\n"
125+
content += "\nuninstalled dependency plugins:\n"
126126
for _, name := range lackNames {
127127
content += " " + lackSymbol + " " + name + "\n"
128128
}
129-
content += "\nInstalling dependency plugins using the command: sponge plugins --install\n"
129+
content += "\ninstalling dependency plugins using the command: sponge plugins --install\n"
130130
} else {
131-
content += "\nAll dependency plugins installed.\n"
131+
content += "\nall dependency plugins installed.\n"
132132
}
133133

134134
fmt.Println(content)
135135
}
136136

137137
func installPlugins(lackNames []string) {
138138
if len(lackNames) == 0 {
139-
fmt.Printf("\n All dependency plugins installed.\n\n")
139+
fmt.Printf("\n all dependency plugins installed.\n\n")
140140
return
141141
}
142-
fmt.Printf("install a total of %d dependency plugins, need to wait a little time.\n\n", len(lackNames))
142+
fmt.Printf("\ninstalling %d dependency plugins, please wait a moment.\n\n", len(lackNames))
143143

144144
var wg = &sync.WaitGroup{}
145145
var manuallyNames []string

cmd/sponge/commands/upgrade.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ func UpgradeCommand() *cobra.Command {
3232
SilenceErrors: true,
3333
SilenceUsage: true,
3434
RunE: func(cmd *cobra.Command, args []string) error {
35-
fmt.Println("upgrading sponge, please wait a moment ......")
3635
if targetVersion == "" {
3736
targetVersion = latestVersion
3837
}
@@ -50,24 +49,41 @@ func UpgradeCommand() *cobra.Command {
5049
}
5150

5251
func runUpgrade(targetVersion string) (string, error) {
52+
runningTip := "upgrading sponge binary "
53+
finishTip := "upgrade sponge binary done " + installedSymbol
54+
failTip := "upgrade sponge binary failed " + lackSymbol
55+
p := utils.NewWaitPrinter(time.Millisecond * 500)
56+
p.LoopPrint(runningTip)
5357
err := runUpgradeCommand(targetVersion)
5458
if err != nil {
55-
fmt.Println(lackSymbol + "upgrade sponge binary.")
59+
p.StopPrint(failTip)
5660
return "", err
5761
}
58-
fmt.Println(installedSymbol + "upgraded sponge binary.")
62+
p.StopPrint(finishTip)
63+
64+
runningTip = "upgrading template code "
65+
finishTip = "upgrade template code done " + installedSymbol
66+
failTip = "upgrade template code failed " + lackSymbol
67+
p = utils.NewWaitPrinter(time.Millisecond * 500)
68+
p.LoopPrint(runningTip)
5969
ver, err := copyToTempDir(targetVersion)
6070
if err != nil {
61-
fmt.Println(lackSymbol + "upgrade template code.")
71+
p.StopPrint(failTip)
6272
return "", err
6373
}
64-
fmt.Println(installedSymbol + "upgraded template code.")
74+
p.StopPrint(finishTip)
75+
76+
runningTip = "upgrading the built-in plugins of sponge "
77+
finishTip = "upgrade the built-in plugins of sponge done " + installedSymbol
78+
failTip = "upgrade the built-in plugins of sponge failed " + lackSymbol
79+
p = utils.NewWaitPrinter(time.Millisecond * 500)
80+
p.LoopPrint(runningTip)
6581
err = updateSpongeInternalPlugin(ver)
6682
if err != nil {
67-
fmt.Println(lackSymbol + "upgrade protoc plugins.")
83+
p.StopPrint(failTip)
6884
return "", err
6985
}
70-
fmt.Println(installedSymbol + "upgraded protoc plugins.")
86+
p.StopPrint(finishTip)
7187
return ver, nil
7288
}
7389

pkg/utils/wait_print.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package utils
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
)
8+
9+
// WaitPrinter is a waiting printer.
10+
type WaitPrinter struct {
11+
ctx context.Context
12+
cancel context.CancelFunc
13+
printFrequency time.Duration
14+
}
15+
16+
// NewWaitPrinter create a new WaitPrinter instance.
17+
func NewWaitPrinter(interval time.Duration) *WaitPrinter {
18+
ctx, cancel := context.WithCancel(context.Background())
19+
if interval < time.Millisecond*100 || interval > time.Second*5 {
20+
interval = time.Millisecond * 500
21+
}
22+
return &WaitPrinter{
23+
ctx: ctx,
24+
cancel: cancel,
25+
printFrequency: interval,
26+
}
27+
}
28+
29+
// LoopPrint start the waiting loop and print the running tip message.
30+
func (p *WaitPrinter) LoopPrint(runningTip string) {
31+
if p == nil {
32+
return
33+
}
34+
go func() {
35+
symbols := []string{runningTip + ".", runningTip + "..", runningTip +
36+
"...", runningTip + "....", runningTip + ".....", runningTip + "......"}
37+
index := 0
38+
fmt.Printf("\r%s", symbols[index])
39+
40+
ticker := time.NewTicker(p.printFrequency)
41+
defer ticker.Stop()
42+
43+
for {
44+
select {
45+
case <-p.ctx.Done():
46+
return
47+
case <-ticker.C:
48+
index++
49+
if index >= len(symbols) {
50+
index = 0
51+
p.clearCurrentLine()
52+
}
53+
fmt.Printf("\r%s", symbols[index])
54+
}
55+
}
56+
}()
57+
}
58+
59+
// StopPrint stop the waiting loop and print the tip message.
60+
func (p *WaitPrinter) StopPrint(tip string) {
61+
if p == nil {
62+
return
63+
}
64+
65+
defer func() {
66+
recover()
67+
}()
68+
69+
p.cancel()
70+
p.clearCurrentLine()
71+
if tip == "" {
72+
return
73+
}
74+
fmt.Println(tip)
75+
}
76+
77+
func (p *WaitPrinter) clearCurrentLine() {
78+
fmt.Print("\033[2K\r")
79+
}

pkg/utils/wait_print_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestLoopPrint(t *testing.T) {
9+
runningTip := "upgrading sponge binary "
10+
finishTip := "upgrade sponge binary successfully"
11+
failedTip := "failed to upgrade sponge binary "
12+
13+
p := NewWaitPrinter(time.Millisecond * 100)
14+
p.LoopPrint(runningTip)
15+
time.Sleep(time.Millisecond * 1000)
16+
p.StopPrint(finishTip)
17+
18+
p = NewWaitPrinter(0)
19+
p.LoopPrint(runningTip)
20+
time.Sleep(time.Millisecond * 1100)
21+
p.StopPrint(failedTip)
22+
time.Sleep(time.Millisecond * 100)
23+
}

0 commit comments

Comments
 (0)