-
Notifications
You must be signed in to change notification settings - Fork 351
/
progress.go
147 lines (126 loc) · 2.87 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package local
import (
"io"
"os"
"time"
"github.com/jedib0t/go-pretty/v6/progress"
)
const (
progressTrackerLength = 20
progressTrackerWidth = 40
progressTrackerDone = 100
progressTrackerUpdateFrequency = 50 * time.Millisecond
)
type ProgressUpdaterReader struct {
r io.Reader
t *progress.Tracker
}
func (pu *ProgressUpdaterReader) Read(p []byte) (n int, err error) {
n, err = pu.r.Read(p)
pu.t.Increment(int64(n))
if err == io.EOF {
pu.t.MarkAsDone()
} else if err != nil {
pu.t.IncrementWithError(int64(n))
}
return
}
type ProgressUpdater struct {
t *progress.Tracker
}
func (p *ProgressUpdater) Reader(reader io.Reader) io.Reader {
return &ProgressUpdaterReader{
r: reader,
t: p.t,
}
}
func (p *ProgressUpdater) Done() {
p.t.MarkAsDone()
}
func (p *ProgressUpdater) Error() {
p.t.MarkAsErrored()
}
type ProgressSpinner struct {
t *progress.Tracker
}
func (p *ProgressSpinner) Error() {
p.t.MarkAsErrored()
}
func (p *ProgressSpinner) Done() {
p.t.MarkAsDone()
}
type ProgressPool struct {
pw progress.Writer
done chan bool
}
func (p *ProgressPool) Start() {
const tickerDuration = 25 * time.Millisecond
go p.pw.Render()
go func() {
t := time.NewTicker(tickerDuration)
for {
select {
case <-p.done:
t.Stop()
return
case <-t.C:
}
}
}()
}
func (p *ProgressPool) Stop() {
p.pw.Stop()
p.done <- true
// according to examples, give enough time for the last render loop to complete.
for p.pw.IsRenderInProgress() {
const renderSleep = 5 * time.Millisecond
time.Sleep(renderSleep)
}
}
func (p *ProgressPool) AddReader(name string, sizeBytes int64) *ProgressUpdater {
tracker := &progress.Tracker{
Message: name,
Total: sizeBytes,
Units: progress.UnitsBytes,
}
p.pw.AppendTracker(tracker)
return &ProgressUpdater{t: tracker}
}
func (p *ProgressPool) AddSpinner(name string) *ProgressSpinner {
tracker := &progress.Tracker{
Message: name,
Total: progressTrackerDone,
Units: progress.Units{
Notation: "%",
Formatter: progress.FormatNumber,
},
}
p.pw.AppendTracker(tracker)
return &ProgressSpinner{t: tracker}
}
func NewProgressPool() *ProgressPool {
pw := progress.NewWriter()
pw.SetAutoStop(false) // important
pw.SetTrackerLength(progressTrackerLength)
pw.SetMessageLength(progressTrackerWidth)
pw.SetSortBy(progress.SortByValue)
pw.SetStyle(progress.StyleDefault)
pw.SetTrackerPosition(progress.PositionRight)
pw.SetUpdateFrequency(progressTrackerUpdateFrequency)
pw.Style().Colors = progress.StyleColorsExample
pw.Style().Options.PercentFormat = "%4.1f%%"
return &ProgressPool{
pw: pw,
done: make(chan bool),
}
}
type fileWrapper struct {
file *os.File
reader io.Reader
}
func (f fileWrapper) Read(p []byte) (n int, err error) {
return f.reader.Read(p)
}
func (f fileWrapper) Seek(offset int64, whence int) (int64, error) {
return f.file.Seek(offset, whence)
}