| 4 | 4 |
"fmt"
|
| 5 | 5 |
"io"
|
| 6 | 6 |
"strings"
|
|
7 |
"sync"
|
| 7 | 8 |
"time"
|
| 8 | 9 |
"unicode/utf8"
|
| 9 | 10 |
|
|
| 32 | 33 |
|
| 33 | 34 |
// completed is set from master Progress goroutine only
|
| 34 | 35 |
completed bool
|
|
36 |
|
|
37 |
removeOnComplete bool
|
| 35 | 38 |
|
| 36 | 39 |
operateState chan func(*bState)
|
| 37 | 40 |
// done is closed by Bar's goroutine, after cacheState is written
|
|
| 54 | 57 |
totalAutoIncrBy int64
|
| 55 | 58 |
trimLeftSpace bool
|
| 56 | 59 |
trimRightSpace bool
|
| 57 | |
completed bool
|
| 58 | |
removed bool
|
|
60 |
toComplete bool
|
|
61 |
removeOnComplete bool
|
| 59 | 62 |
dynamic bool
|
| 60 | 63 |
startTime time.Time
|
| 61 | 64 |
timeElapsed time.Duration
|
|
| 71 | 74 |
char rune
|
| 72 | 75 |
till int64
|
| 73 | 76 |
}
|
| 74 | |
renderedReader struct {
|
| 75 | |
io.Reader
|
|
77 |
renderedState struct {
|
|
78 |
bar *Bar
|
|
79 |
reader io.Reader
|
| 76 | 80 |
toComplete bool
|
| 77 | |
toRemove bool
|
| 78 | 81 |
}
|
| 79 | 82 |
)
|
| 80 | 83 |
|
| 81 | |
func newBar(id int, total int64, cancel <-chan struct{}, options ...BarOption) *Bar {
|
|
84 |
func newBar(wg *sync.WaitGroup, id int, total int64, cancel <-chan struct{}, options ...BarOption) *Bar {
|
| 82 | 85 |
if total <= 0 {
|
| 83 | 86 |
total = time.Now().Unix()
|
| 84 | 87 |
}
|
|
| 98 | 101 |
s.bufA = bytes.NewBuffer(make([]byte, 0, s.width/2))
|
| 99 | 102 |
|
| 100 | 103 |
b := &Bar{
|
| 101 | |
priority: id,
|
| 102 | |
operateState: make(chan func(*bState)),
|
| 103 | |
done: make(chan struct{}),
|
| 104 | |
shutdown: make(chan struct{}),
|
| 105 | |
}
|
| 106 | |
|
| 107 | |
go b.serve(s, cancel)
|
|
104 |
priority: id,
|
|
105 |
removeOnComplete: s.removeOnComplete,
|
|
106 |
operateState: make(chan func(*bState)),
|
|
107 |
done: make(chan struct{}),
|
|
108 |
shutdown: make(chan struct{}),
|
|
109 |
}
|
|
110 |
|
|
111 |
go b.serve(wg, s, cancel)
|
| 108 | 112 |
return b
|
| 109 | 113 |
}
|
| 110 | 114 |
|
|
| 132 | 136 |
// Increment is a shorthand for b.IncrBy(1)
|
| 133 | 137 |
func (b *Bar) Increment() {
|
| 134 | 138 |
b.IncrBy(1)
|
|
139 |
}
|
|
140 |
|
|
141 |
// ResumeFill fills bar with different r rune,
|
|
142 |
// from 0 to till amount of progress.
|
|
143 |
func (b *Bar) ResumeFill(r rune, till int64) {
|
|
144 |
if till < 1 {
|
|
145 |
return
|
|
146 |
}
|
|
147 |
select {
|
|
148 |
case b.operateState <- func(s *bState) { s.refill = &refill{r, till} }:
|
|
149 |
case <-b.done:
|
|
150 |
}
|
|
151 |
}
|
|
152 |
|
|
153 |
// NumOfAppenders returns current number of append decorators
|
|
154 |
func (b *Bar) NumOfAppenders() int {
|
|
155 |
result := make(chan int, 1)
|
|
156 |
select {
|
|
157 |
case b.operateState <- func(s *bState) { result <- len(s.aDecorators) }:
|
|
158 |
return <-result
|
|
159 |
case <-b.done:
|
|
160 |
return len(b.cacheState.aDecorators)
|
|
161 |
}
|
|
162 |
}
|
|
163 |
|
|
164 |
// NumOfPrependers returns current number of prepend decorators
|
|
165 |
func (b *Bar) NumOfPrependers() int {
|
|
166 |
result := make(chan int, 1)
|
|
167 |
select {
|
|
168 |
case b.operateState <- func(s *bState) { result <- len(s.pDecorators) }:
|
|
169 |
return <-result
|
|
170 |
case <-b.done:
|
|
171 |
return len(b.cacheState.pDecorators)
|
|
172 |
}
|
|
173 |
}
|
|
174 |
|
|
175 |
// ID returs id of the bar
|
|
176 |
func (b *Bar) ID() int {
|
|
177 |
result := make(chan int, 1)
|
|
178 |
select {
|
|
179 |
case b.operateState <- func(s *bState) { result <- s.id }:
|
|
180 |
return <-result
|
|
181 |
case <-b.done:
|
|
182 |
return b.cacheState.id
|
|
183 |
}
|
|
184 |
}
|
|
185 |
|
|
186 |
// Current returns bar's current number, in other words sum of all increments.
|
|
187 |
func (b *Bar) Current() int64 {
|
|
188 |
result := make(chan int64, 1)
|
|
189 |
select {
|
|
190 |
case b.operateState <- func(s *bState) { result <- s.current }:
|
|
191 |
return <-result
|
|
192 |
case <-b.done:
|
|
193 |
return b.cacheState.current
|
|
194 |
}
|
|
195 |
}
|
|
196 |
|
|
197 |
// Total returns bar's total number.
|
|
198 |
func (b *Bar) Total() int64 {
|
|
199 |
result := make(chan int64, 1)
|
|
200 |
select {
|
|
201 |
case b.operateState <- func(s *bState) { result <- s.total }:
|
|
202 |
return <-result
|
|
203 |
case <-b.done:
|
|
204 |
return b.cacheState.total
|
|
205 |
}
|
|
206 |
}
|
|
207 |
|
|
208 |
// SetTotal sets total dynamically. The final param indicates the very last set,
|
|
209 |
// in other words you should set it to true when total is determined.
|
|
210 |
func (b *Bar) SetTotal(total int64, final bool) {
|
|
211 |
select {
|
|
212 |
case b.operateState <- func(s *bState) {
|
|
213 |
s.total = total
|
|
214 |
s.dynamic = !final
|
|
215 |
}:
|
|
216 |
case <-b.done:
|
|
217 |
}
|
| 135 | 218 |
}
|
| 136 | 219 |
|
| 137 | 220 |
// IncrBy increments progress bar by amount of n
|
|
| 141 | 224 |
}
|
| 142 | 225 |
select {
|
| 143 | 226 |
case b.operateState <- func(s *bState) {
|
| 144 | |
if s.completed {
|
|
227 |
if s.toComplete {
|
| 145 | 228 |
return
|
| 146 | 229 |
}
|
| 147 | 230 |
next := time.Now()
|
|
| 161 | 244 |
}
|
| 162 | 245 |
} else if s.current >= s.total {
|
| 163 | 246 |
s.current = s.total
|
| 164 | |
s.completed = true
|
| 165 | |
}
|
| 166 | |
}:
|
| 167 | |
case <-b.done:
|
| 168 | |
}
|
| 169 | |
}
|
| 170 | |
|
| 171 | |
// ResumeFill fills bar with different r rune,
|
| 172 | |
// from 0 to till amount of progress.
|
| 173 | |
func (b *Bar) ResumeFill(r rune, till int64) {
|
| 174 | |
if till < 1 {
|
| 175 | |
return
|
| 176 | |
}
|
| 177 | |
select {
|
| 178 | |
case b.operateState <- func(s *bState) { s.refill = &refill{r, till} }:
|
| 179 | |
case <-b.done:
|
| 180 | |
}
|
| 181 | |
}
|
| 182 | |
|
| 183 | |
// NumOfAppenders returns current number of append decorators
|
| 184 | |
func (b *Bar) NumOfAppenders() int {
|
| 185 | |
result := make(chan int, 1)
|
| 186 | |
select {
|
| 187 | |
case b.operateState <- func(s *bState) { result <- len(s.aDecorators) }:
|
| 188 | |
return <-result
|
| 189 | |
case <-b.done:
|
| 190 | |
return len(b.cacheState.aDecorators)
|
| 191 | |
}
|
| 192 | |
}
|
| 193 | |
|
| 194 | |
// NumOfPrependers returns current number of prepend decorators
|
| 195 | |
func (b *Bar) NumOfPrependers() int {
|
| 196 | |
result := make(chan int, 1)
|
| 197 | |
select {
|
| 198 | |
case b.operateState <- func(s *bState) { result <- len(s.pDecorators) }:
|
| 199 | |
return <-result
|
| 200 | |
case <-b.done:
|
| 201 | |
return len(b.cacheState.pDecorators)
|
| 202 | |
}
|
| 203 | |
}
|
| 204 | |
|
| 205 | |
// ID returs id of the bar
|
| 206 | |
func (b *Bar) ID() int {
|
| 207 | |
result := make(chan int, 1)
|
| 208 | |
select {
|
| 209 | |
case b.operateState <- func(s *bState) { result <- s.id }:
|
| 210 | |
return <-result
|
| 211 | |
case <-b.done:
|
| 212 | |
return b.cacheState.id
|
| 213 | |
}
|
| 214 | |
}
|
| 215 | |
|
| 216 | |
// Current returns bar's current number, in other words sum of all increments.
|
| 217 | |
func (b *Bar) Current() int64 {
|
| 218 | |
result := make(chan int64, 1)
|
| 219 | |
select {
|
| 220 | |
case b.operateState <- func(s *bState) { result <- s.current }:
|
| 221 | |
return <-result
|
| 222 | |
case <-b.done:
|
| 223 | |
return b.cacheState.current
|
| 224 | |
}
|
| 225 | |
}
|
| 226 | |
|
| 227 | |
// Total returns bar's total number.
|
| 228 | |
func (b *Bar) Total() int64 {
|
| 229 | |
result := make(chan int64, 1)
|
| 230 | |
select {
|
| 231 | |
case b.operateState <- func(s *bState) { result <- s.total }:
|
| 232 | |
return <-result
|
| 233 | |
case <-b.done:
|
| 234 | |
return b.cacheState.total
|
| 235 | |
}
|
| 236 | |
}
|
| 237 | |
|
| 238 | |
// SetTotal sets total dynamically. The final param indicates the very last set,
|
| 239 | |
// in other words you should set it to true when total is determined.
|
| 240 | |
func (b *Bar) SetTotal(total int64, final bool) {
|
| 241 | |
select {
|
| 242 | |
case b.operateState <- func(s *bState) {
|
| 243 | |
s.total = total
|
| 244 | |
s.dynamic = !final
|
|
247 |
s.toComplete = true
|
|
248 |
}
|
| 245 | 249 |
}:
|
| 246 | 250 |
case <-b.done:
|
| 247 | 251 |
}
|
|
| 251 | 255 |
func (b *Bar) Completed() bool {
|
| 252 | 256 |
result := make(chan bool, 1)
|
| 253 | 257 |
select {
|
| 254 | |
case b.operateState <- func(s *bState) { result <- s.completed }:
|
| 255 | |
return <-result
|
| 256 | |
case <-b.done:
|
| 257 | |
return b.cacheState.completed
|
| 258 | |
}
|
| 259 | |
}
|
| 260 | |
|
| 261 | |
// Complete stops bar's progress tracking, but doesn't remove the bar from rendering queue.
|
| 262 | |
// If you need to remove, invoke Progress.RemoveBar(*Bar) instead.
|
| 263 | |
func (b *Bar) Complete() {
|
| 264 | |
b.askToComplete(false)
|
| 265 | |
}
|
| 266 | |
|
| 267 | |
func (b *Bar) askToComplete(toRemove bool) bool {
|
| 268 | |
result := make(chan bool, 1)
|
| 269 | |
select {
|
| 270 | |
case b.operateState <- func(s *bState) {
|
| 271 | |
s.removed = toRemove
|
| 272 | |
s.completed = true
|
| 273 | |
result <- true
|
| 274 | |
}:
|
| 275 | |
return <-result
|
| 276 | |
case <-b.done:
|
| 277 | |
return false
|
| 278 | |
}
|
| 279 | |
}
|
| 280 | |
|
| 281 | |
func (b *Bar) serve(s *bState, cancel <-chan struct{}) {
|
|
258 |
case b.operateState <- func(s *bState) { result <- s.toComplete }:
|
|
259 |
return <-result
|
|
260 |
case <-b.done:
|
|
261 |
return b.cacheState.toComplete
|
|
262 |
}
|
|
263 |
}
|
|
264 |
|
|
265 |
func (b *Bar) serve(wg *sync.WaitGroup, s *bState, cancel <-chan struct{}) {
|
|
266 |
defer wg.Done()
|
| 282 | 267 |
for {
|
| 283 | 268 |
select {
|
| 284 | 269 |
case op := <-b.operateState:
|
| 285 | 270 |
op(s)
|
| 286 | 271 |
case <-cancel:
|
| 287 | |
s.completed = true
|
|
272 |
s.toComplete = true
|
| 288 | 273 |
cancel = nil
|
| 289 | 274 |
case <-b.shutdown:
|
| 290 | 275 |
b.cacheState = s
|
|
| 294 | 279 |
}
|
| 295 | 280 |
}
|
| 296 | 281 |
|
| 297 | |
func (b *Bar) render(tw int, pSyncer, aSyncer *widthSyncer) <-chan *renderedReader {
|
| 298 | |
ch := make(chan *renderedReader, 1)
|
|
282 |
func (b *Bar) render(tw int, pSyncer, aSyncer *widthSyncer) <-chan *renderedState {
|
|
283 |
ch := make(chan *renderedState, 1)
|
| 299 | 284 |
|
| 300 | 285 |
go func() {
|
| 301 | 286 |
select {
|
|
| 307 | 292 |
s.panicMsg = fmt.Sprintf("b#%02d panic: %v\n", s.id, p)
|
| 308 | 293 |
s.pDecorators = nil
|
| 309 | 294 |
s.aDecorators = nil
|
| 310 | |
s.completed = true
|
|
295 |
s.toComplete = true
|
| 311 | 296 |
r = strings.NewReader(s.panicMsg)
|
| 312 | 297 |
}
|
| 313 | |
ch <- &renderedReader{r, s.completed, s.removed}
|
|
298 |
ch <- &renderedState{b, r, s.toComplete}
|
| 314 | 299 |
}()
|
| 315 | |
s.draw(tw, pSyncer, aSyncer)
|
| 316 | |
r = io.MultiReader(s.bufP, s.bufB, s.bufA)
|
|
300 |
r = s.draw(tw, pSyncer, aSyncer)
|
| 317 | 301 |
}:
|
| 318 | 302 |
case <-b.done:
|
| 319 | 303 |
s := b.cacheState
|
|
| 321 | 305 |
if s.panicMsg != "" {
|
| 322 | 306 |
r = strings.NewReader(s.panicMsg)
|
| 323 | 307 |
} else {
|
| 324 | |
s.draw(tw, pSyncer, aSyncer)
|
| 325 | |
r = io.MultiReader(s.bufP, s.bufB, s.bufA)
|
|
308 |
r = s.draw(tw, pSyncer, aSyncer)
|
| 326 | 309 |
}
|
| 327 | |
ch <- &renderedReader{r, s.completed, s.removed}
|
|
310 |
ch <- &renderedState{b, r, s.toComplete}
|
| 328 | 311 |
}
|
| 329 | 312 |
}()
|
| 330 | 313 |
|
| 331 | 314 |
return ch
|
|
315 |
}
|
|
316 |
|
|
317 |
func (s *bState) draw(termWidth int, pSyncer, aSyncer *widthSyncer) io.Reader {
|
|
318 |
if termWidth <= 0 {
|
|
319 |
termWidth = s.width
|
|
320 |
}
|
|
321 |
|
|
322 |
stat := newStatistics(s)
|
|
323 |
|
|
324 |
// render prepend functions to the left of the bar
|
|
325 |
s.bufP.Reset()
|
|
326 |
for i, f := range s.pDecorators {
|
|
327 |
s.bufP.WriteString(f(stat, pSyncer.Accumulator[i], pSyncer.Distributor[i]))
|
|
328 |
}
|
|
329 |
|
|
330 |
if !s.trimLeftSpace {
|
|
331 |
s.bufP.WriteByte(' ')
|
|
332 |
}
|
|
333 |
|
|
334 |
// render append functions to the right of the bar
|
|
335 |
s.bufA.Reset()
|
|
336 |
if !s.trimRightSpace {
|
|
337 |
s.bufA.WriteByte(' ')
|
|
338 |
}
|
|
339 |
|
|
340 |
for i, f := range s.aDecorators {
|
|
341 |
s.bufA.WriteString(f(stat, aSyncer.Accumulator[i], aSyncer.Distributor[i]))
|
|
342 |
}
|
|
343 |
|
|
344 |
prependCount := utf8.RuneCount(s.bufP.Bytes())
|
|
345 |
appendCount := utf8.RuneCount(s.bufA.Bytes())
|
|
346 |
|
|
347 |
if termWidth > s.width {
|
|
348 |
s.fillBar(s.width)
|
|
349 |
} else {
|
|
350 |
s.fillBar(termWidth - prependCount - appendCount)
|
|
351 |
}
|
|
352 |
barCount := utf8.RuneCount(s.bufB.Bytes())
|
|
353 |
totalCount := prependCount + barCount + appendCount
|
|
354 |
if totalCount > termWidth {
|
|
355 |
s.fillBar(termWidth - prependCount - appendCount)
|
|
356 |
}
|
|
357 |
s.bufA.WriteByte('\n')
|
|
358 |
|
|
359 |
return io.MultiReader(s.bufP, s.bufB, s.bufA)
|
| 332 | 360 |
}
|
| 333 | 361 |
|
| 334 | 362 |
func (s *bState) updateTimePerItemEstimate(amount int, now, next time.Time) {
|
|
| 336 | 364 |
lastItemEstimate := float64(lastBlockTime) / float64(amount)
|
| 337 | 365 |
s.timePerItem = time.Duration((s.etaAlpha * lastItemEstimate) + (1-s.etaAlpha)*float64(s.timePerItem))
|
| 338 | 366 |
s.blockStartTime = next
|
| 339 | |
}
|
| 340 | |
|
| 341 | |
func (s *bState) draw(termWidth int, pSyncer, aSyncer *widthSyncer) {
|
| 342 | |
if termWidth <= 0 {
|
| 343 | |
termWidth = s.width
|
| 344 | |
}
|
| 345 | |
|
| 346 | |
stat := newStatistics(s)
|
| 347 | |
|
| 348 | |
// render prepend functions to the left of the bar
|
| 349 | |
s.bufP.Reset()
|
| 350 | |
for i, f := range s.pDecorators {
|
| 351 | |
s.bufP.WriteString(f(stat, pSyncer.Accumulator[i], pSyncer.Distributor[i]))
|
| 352 | |
}
|
| 353 | |
|
| 354 | |
if !s.trimLeftSpace {
|
| 355 | |
s.bufP.WriteByte(' ')
|
| 356 | |
}
|
| 357 | |
|
| 358 | |
// render append functions to the right of the bar
|
| 359 | |
s.bufA.Reset()
|
| 360 | |
if !s.trimRightSpace {
|
| 361 | |
s.bufA.WriteByte(' ')
|
| 362 | |
}
|
| 363 | |
|
| 364 | |
for i, f := range s.aDecorators {
|
| 365 | |
s.bufA.WriteString(f(stat, aSyncer.Accumulator[i], aSyncer.Distributor[i]))
|
| 366 | |
}
|
| 367 | |
|
| 368 | |
prependCount := utf8.RuneCount(s.bufP.Bytes())
|
| 369 | |
appendCount := utf8.RuneCount(s.bufA.Bytes())
|
| 370 | |
|
| 371 | |
if termWidth > s.width {
|
| 372 | |
s.fillBar(s.width)
|
| 373 | |
} else {
|
| 374 | |
s.fillBar(termWidth - prependCount - appendCount)
|
| 375 | |
}
|
| 376 | |
barCount := utf8.RuneCount(s.bufB.Bytes())
|
| 377 | |
totalCount := prependCount + barCount + appendCount
|
| 378 | |
if totalCount > termWidth {
|
| 379 | |
s.fillBar(termWidth - prependCount - appendCount)
|
| 380 | |
}
|
| 381 | |
s.bufA.WriteByte('\n')
|
| 382 | 367 |
}
|
| 383 | 368 |
|
| 384 | 369 |
func (s *bState) fillBar(width int) {
|
|
| 427 | 412 |
func newStatistics(s *bState) *decor.Statistics {
|
| 428 | 413 |
return &decor.Statistics{
|
| 429 | 414 |
ID: s.id,
|
| 430 | |
Completed: s.completed,
|
| 431 | |
Removed: s.removed,
|
|
415 |
Completed: s.toComplete,
|
| 432 | 416 |
Total: s.total,
|
| 433 | 417 |
Current: s.current,
|
| 434 | 418 |
StartTime: s.startTime,
|