-
Notifications
You must be signed in to change notification settings - Fork 342
/
progress.go
171 lines (147 loc) · 4.04 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package cmdutils
import (
"sync/atomic"
"time"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/vbauerster/mpb/v5"
"github.com/vbauerster/mpb/v5/decor"
)
const (
progressCounterFormat = "%d / %d "
spinnerCounterFormat = "%d "
spinnerNoCounterFormat = ""
progressSuffix = "]"
progressPrefix = "["
progressBarWidth = 60
progressBarNameColumnWidth = 40
progressBarCounterColumnWidth = 20
progressBarStyle = " =>- <"
progressRefreshRate = 50 * time.Millisecond
)
const (
Bar = iota
Spinner
SpinnerNoCounter
)
type ProgressReporter interface {
Progress() []*Progress
}
type Progress struct {
label string
current int64
total int64
completed bool
active bool
progressType int
}
type MultiBar struct {
reporter ProgressReporter
mpb *mpb.Progress
mpbBars map[string]*mpb.Bar
ticker *time.Ticker
}
func NewProgress(label string, progressType int) *Progress {
total := 0
if progressType == Spinner || progressType == SpinnerNoCounter {
total = -1
}
return &Progress{
label: label,
total: int64(total),
progressType: progressType,
}
}
func NewActiveProgress(label string, progressType int) *Progress {
res := NewProgress(label, progressType)
res.Activate()
return res
}
func (p *Progress) Label() string {
return p.label
}
func (p *Progress) Current() int64 {
return atomic.LoadInt64(&p.current)
}
func (p *Progress) Total() int64 {
return atomic.LoadInt64(&p.total)
}
func (p *Progress) Incr() {
atomic.AddInt64(&p.current, 1)
}
func (p *Progress) Add(n int64) {
atomic.AddInt64(&p.current, n)
}
func (p *Progress) SetCurrent(n int64) {
atomic.StoreInt64(&p.current, n)
}
func (p *Progress) SetTotal(n int64) {
atomic.StoreInt64(&p.total, n)
}
func (p *Progress) Completed() bool {
return p.completed
}
func (p *Progress) SetCompleted(completed bool) {
p.completed = completed
}
func (p *Progress) Activate() {
p.active = true
}
func NewMultiBar(r ProgressReporter) *MultiBar {
ticker := time.NewTicker(progressRefreshRate)
m := mpb.New(mpb.WithWidth(progressBarWidth))
bars := make(map[string]*mpb.Bar)
return &MultiBar{reporter: r, mpbBars: bars, mpb: m, ticker: ticker}
}
func (b *MultiBar) Start() {
go func() {
for range b.ticker.C {
b.refresh(false)
}
}()
}
func (b *MultiBar) Stop() {
b.refresh(true)
b.ticker.Stop()
b.mpb.Wait()
}
func (b *MultiBar) refresh(isStop bool) {
progress := b.reporter.Progress()
for _, p := range progress {
if p == nil || !p.active {
continue
}
total := p.Total()
bar, ok := b.mpbBars[p.label]
if !ok {
bar = createBar(b.mpb, p, total)
b.mpbBars[p.label] = bar
}
if !isStop {
bar.SetTotal(total, p.Completed())
} else {
bar.SetTotal(p.Current(), true)
}
bar.SetCurrent(p.Current())
}
}
func createBar(m *mpb.Progress, p *Progress, total int64) *mpb.Bar {
var bar *mpb.Bar
labelDecorator := decor.Name(p.label, decor.WC{W: progressBarNameColumnWidth, C: decor.DidentRight})
suffixOption := mpb.AppendDecorators(decor.OnComplete(decor.Name(progressSuffix), text.FgGreen.Sprintf(" done")))
if p.progressType == Spinner || p.progressType == SpinnerNoCounter {
// unknown total, render a spinner
var counterDecorator decor.Decorator
if p.progressType == Spinner {
counterDecorator = decor.CurrentNoUnit(spinnerCounterFormat, decor.WC{W: progressBarCounterColumnWidth})
} else {
counterDecorator = decor.Name(spinnerNoCounterFormat, decor.WC{W: progressBarCounterColumnWidth})
}
bar = m.AddSpinner(total, mpb.SpinnerOnMiddle, mpb.BarFillerClearOnComplete(), suffixOption, mpb.PrependDecorators(labelDecorator, counterDecorator, decor.OnComplete(decor.Name(progressPrefix), "")))
} else { // type == Bar
bar = m.AddBar(total, mpb.BarStyle(progressBarStyle), mpb.BarFillerClearOnComplete(), suffixOption,
mpb.PrependDecorators(labelDecorator,
decor.CountersNoUnit(progressCounterFormat, decor.WC{W: progressBarCounterColumnWidth}),
decor.OnComplete(decor.Name(progressPrefix), "")))
}
return bar
}