-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathprogress.go
277 lines (235 loc) · 7.93 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package progress
import (
"io"
"math"
"os"
"sync"
"time"
"unicode/utf8"
)
var (
// DefaultLengthTracker defines a sane value for a Tracker's length.
DefaultLengthTracker = 20
// DefaultUpdateFrequency defines a sane value for the frequency with which
// all the Tracker's get updated on the screen.
DefaultUpdateFrequency = time.Millisecond * 250
)
// Progress helps track progress for one or more tasks.
type Progress struct {
autoStop bool
done chan bool
lengthTracker int
lengthProgress int
outputWriter io.Writer
hideTime bool
hideTracker bool
hideValue bool
hidePercentage bool
messageWidth int
numTrackersExpected int64
overallTracker *Tracker
overallTrackerMutex sync.RWMutex
renderInProgress bool
renderInProgressMutex sync.RWMutex
showOverallTracker bool
sortBy SortBy
style *Style
trackerPosition Position
trackersActive []*Tracker
trackersActiveMutex sync.RWMutex
trackersDone []*Tracker
trackersDoneMutex sync.RWMutex
trackersInQueue []*Tracker
trackersInQueueMutex sync.RWMutex
updateFrequency time.Duration
}
// Position defines the position of the Tracker with respect to the Tracker's
// Message.
type Position int
const (
// PositionLeft will make the Tracker be displayed first before the Message.
PositionLeft Position = iota
// PositionRight will make the Tracker be displayed after the Message.
PositionRight
)
// AppendTracker appends a single Tracker for tracking. The Tracker gets added
// to a queue, which gets picked up by the Render logic in the next rendering
// cycle.
func (p *Progress) AppendTracker(t *Tracker) {
if t.Total <= 0 {
t.Total = math.MaxInt64
}
t.start()
p.overallTrackerMutex.Lock()
if p.overallTracker == nil {
p.overallTracker = &Tracker{Total: 1}
if p.numTrackersExpected > 0 {
p.overallTracker.Total = p.numTrackersExpected * 100
}
p.overallTracker.start()
}
p.trackersInQueueMutex.Lock()
p.trackersInQueue = append(p.trackersInQueue, t)
p.trackersInQueueMutex.Unlock()
p.overallTracker.mutex.Lock()
if p.overallTracker.Total < int64(p.Length())*100 {
p.overallTracker.Total = int64(p.Length()) * 100
}
p.overallTracker.mutex.Unlock()
p.overallTrackerMutex.Unlock()
}
// AppendTrackers appends one or more Trackers for tracking.
func (p *Progress) AppendTrackers(trackers []*Tracker) {
for _, tracker := range trackers {
p.AppendTracker(tracker)
}
}
// IsRenderInProgress returns true if a call to Render() was made, and is still
// in progress and has not ended yet.
func (p *Progress) IsRenderInProgress() bool {
p.renderInProgressMutex.RLock()
defer p.renderInProgressMutex.RUnlock()
return p.renderInProgress
}
// Length returns the number of Trackers tracked overall.
func (p *Progress) Length() int {
p.trackersActiveMutex.RLock()
p.trackersDoneMutex.RLock()
p.trackersInQueueMutex.RLock()
defer p.trackersActiveMutex.RUnlock()
defer p.trackersDoneMutex.RUnlock()
defer p.trackersInQueueMutex.RUnlock()
return len(p.trackersInQueue) + len(p.trackersActive) + len(p.trackersDone)
}
// LengthActive returns the number of Trackers actively tracked (not done yet).
func (p *Progress) LengthActive() int {
p.trackersActiveMutex.RLock()
p.trackersInQueueMutex.RLock()
defer p.trackersActiveMutex.RUnlock()
defer p.trackersInQueueMutex.RUnlock()
return len(p.trackersInQueue) + len(p.trackersActive)
}
// LengthDone returns the number of Trackers that are done tracking.
func (p *Progress) LengthDone() int {
p.trackersDoneMutex.RLock()
defer p.trackersDoneMutex.RUnlock()
return len(p.trackersDone)
}
// LengthInQueue returns the number of Trackers in queue to be actively tracked
// (not tracking yet).
func (p *Progress) LengthInQueue() int {
p.trackersInQueueMutex.RLock()
defer p.trackersInQueueMutex.RUnlock()
return len(p.trackersInQueue)
}
// SetAutoStop toggles the auto-stop functionality. Auto-stop set to true would
// mean that the Render() function will automatically stop once all currently
// active Trackers reach their final states. When set to false, the client code
// will have to call Progress.Stop() to stop the Render() logic. Default: false.
func (p *Progress) SetAutoStop(autoStop bool) {
p.autoStop = autoStop
}
// SetMessageWidth sets the (printed) length of the tracker message. Any message
// longer the specified width will be snipped abruptly. Any message shorter than
// the specified width will be padded with spaces.
func (p *Progress) SetMessageWidth(width int) {
p.messageWidth = width
}
// SetNumTrackersExpected sets the expected number of trackers to be tracked.
// This helps calculate the overall progress with better accuracy.
func (p *Progress) SetNumTrackersExpected(numTrackers int) {
p.numTrackersExpected = int64(numTrackers)
}
// SetOutputWriter redirects the output of Render to an io.writer object like
// os.Stdout or os.Stderr or a file. Warning: redirecting the output to a file
// may not work well as the Render() logic moves the cursor around a lot.
func (p *Progress) SetOutputWriter(writer io.Writer) {
p.outputWriter = writer
}
// SetSortBy defines the sorting mechanism to use to sort the Active Trackers
// before rendering the. Default: no-sorting == sort-by-insertion-order.
func (p *Progress) SetSortBy(sortBy SortBy) {
p.sortBy = sortBy
}
// SetStyle sets the Style to use for rendering.
func (p *Progress) SetStyle(style Style) {
p.style = &style
}
// SetTrackerLength sets the text-length of all the Trackers.
func (p *Progress) SetTrackerLength(length int) {
p.lengthTracker = length
}
// SetTrackerPosition sets the position of the tracker with respect to the
// Tracker message text.
func (p *Progress) SetTrackerPosition(position Position) {
p.trackerPosition = position
}
// SetUpdateFrequency sets the update frequency while rendering the trackers.
// the lower the value, the more number of times the Trackers get refreshed. A
// sane value would be 250ms.
func (p *Progress) SetUpdateFrequency(frequency time.Duration) {
p.updateFrequency = frequency
}
// ShowPercentage toggles showing the Percent complete for each Tracker.
func (p *Progress) ShowPercentage(show bool) {
p.hidePercentage = !show
}
// ShowOverallTracker toggles showing the Overall progress tracker.
func (p *Progress) ShowOverallTracker(show bool) {
p.showOverallTracker = show
}
// ShowTime toggles showing the Time taken by each Tracker.
func (p *Progress) ShowTime(show bool) {
p.hideTime = !show
}
// ShowTracker toggles showing the Tracker (the progress bar).
func (p *Progress) ShowTracker(show bool) {
p.hideTracker = !show
}
// ShowValue toggles showing the actual Value of the Tracker.
func (p *Progress) ShowValue(show bool) {
p.hideValue = !show
}
// Stop stops the Render() logic that is in progress.
func (p *Progress) Stop() {
if p.IsRenderInProgress() {
p.done <- true
}
}
// Style returns the current Style.
func (p *Progress) Style() *Style {
if p.style == nil {
tempStyle := StyleDefault
p.style = &tempStyle
}
return p.style
}
func (p *Progress) initForRender() {
// pick a default style
p.Style()
// reset the signals
p.done = make(chan bool, 1)
// pick default lengths if no valid ones set
if p.lengthTracker <= 0 {
p.lengthTracker = DefaultLengthTracker
}
// calculate length of the actual progress bar by discount the left/right
// border/box chars
p.lengthProgress = p.lengthTracker -
utf8.RuneCountInString(p.style.Chars.BoxLeft) -
utf8.RuneCountInString(p.style.Chars.BoxRight)
// if not output write has been set, output to STDOUT
if p.outputWriter == nil {
p.outputWriter = os.Stdout
}
// pick a sane update frequency if none set
if p.updateFrequency <= 0 {
p.updateFrequency = DefaultUpdateFrequency
}
}
// renderHint has hints for the Render*() logic
type renderHint struct {
hideTime bool // hide the time
hideValue bool // hide the value
isOverallTracker bool // is the Overall Progress tracker
}