-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
output-renderer.go
70 lines (63 loc) · 1.51 KB
/
output-renderer.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
package main
import (
"fmt"
"os"
"time"
"github.com/schollz/progressbar/v3"
)
type OutputRenderer struct {
ProgressBar *progressbar.ProgressBar
Quiet bool
}
func (r *OutputRenderer) initProgressBar(maxBytes int64, desc string) {
if r.Quiet {
return
}
r.ProgressBar = progressbar.NewOptions64(
maxBytes,
progressbar.OptionSetDescription(desc),
progressbar.OptionSetWriter(os.Stderr),
progressbar.OptionShowBytes(true),
progressbar.OptionSetWidth(20),
progressbar.OptionShowCount(),
progressbar.OptionOnCompletion(func() { fmt.Printf("\n") }),
progressbar.OptionSpinnerType(14),
progressbar.OptionSetRenderBlankState(true),
progressbar.OptionThrottle(50*time.Millisecond),
)
}
func (r *OutputRenderer) initSpinner(desc string) {
if r.Quiet {
return
}
r.ProgressBar = progressbar.NewOptions64(
-1,
progressbar.OptionSetDescription(desc),
progressbar.OptionSetWriter(os.Stderr),
progressbar.OptionSetWidth(20),
progressbar.OptionOnCompletion(func() {}),
progressbar.OptionSpinnerType(14),
progressbar.OptionShowBytes(false),
progressbar.OptionSetRenderBlankState(true),
progressbar.OptionClearOnFinish(),
)
// Spin in an asynchronous thread
go func() {
for !r.ProgressBar.IsFinished() {
r.ProgressBar.Add(1)
time.Sleep(50 * time.Millisecond)
}
}()
}
func (r *OutputRenderer) stopSpinner() {
if r.Quiet {
return
}
r.ProgressBar.Finish()
}
func (r *OutputRenderer) updateProgressBar(totalBytes int64) {
if r.Quiet {
return
}
r.ProgressBar.Set64(totalBytes)
}