| 1 | 1 |
|
| 2 | 2 |
import (
|
| 3 | 3 |
"bytes"
|
| 4 | |
"container/heap"
|
| 5 | 4 |
"context"
|
| 6 | 5 |
"fmt"
|
| 7 | 6 |
"io"
|
|
| 34 | 33 |
|
| 35 | 34 |
// pState holds bars in its priorityQueue, it gets passed to (*Progress).serve monitor goroutine.
|
| 36 | 35 |
type pState struct {
|
| 37 | |
bHeap priorityQueue
|
| 38 | |
heapUpdated bool
|
| 39 | |
pMatrix map[int][]chan int
|
| 40 | |
aMatrix map[int][]chan int
|
| 41 | |
rows []io.Reader
|
|
36 |
hm heapManager
|
|
37 |
rows []io.Reader
|
| 42 | 38 |
|
| 43 | 39 |
// following are provided/overrided by user
|
| 44 | 40 |
refreshRate time.Duration
|
|
| 48 | 44 |
popCompleted bool
|
| 49 | 45 |
outputDiscarded bool
|
| 50 | 46 |
disableAutoRefresh bool
|
|
47 |
ignoreNotTTY bool
|
| 51 | 48 |
manualRefresh chan interface{}
|
| 52 | 49 |
renderDelay <-chan struct{}
|
| 53 | 50 |
shutdownNotifier chan struct{}
|
|
| 68 | 65 |
// method has been called.
|
| 69 | 66 |
func NewWithContext(ctx context.Context, options ...ContainerOption) *Progress {
|
| 70 | 67 |
s := &pState{
|
|
68 |
hm: make(heapManager),
|
| 71 | 69 |
rows: make([]io.Reader, 32),
|
| 72 | 70 |
refreshRate: defaultRefreshRate,
|
| 73 | 71 |
popPriority: math.MinInt32,
|
|
| 136 | 134 |
if bs.wait.bar != nil {
|
| 137 | 135 |
ps.queueBars[bs.wait.bar] = bar
|
| 138 | 136 |
} else {
|
| 139 | |
heap.Push(&ps.bHeap, bar)
|
| 140 | |
ps.heapUpdated = true
|
|
137 |
ps.hm.push(bar, true)
|
| 141 | 138 |
}
|
| 142 | 139 |
ps.idCount++
|
| 143 | 140 |
result <- bar
|
|
| 151 | 148 |
}
|
| 152 | 149 |
|
| 153 | 150 |
func (p *Progress) traverseBars(cb func(b *Bar) bool) {
|
| 154 | |
sync := make(chan struct{})
|
|
151 |
iter := make(chan *Bar)
|
|
152 |
drop := make(chan struct{})
|
| 155 | 153 |
select {
|
| 156 | 154 |
case p.operateState <- func(s *pState) {
|
| 157 | |
defer close(sync)
|
| 158 | |
for i := 0; i < s.bHeap.Len(); i++ {
|
| 159 | |
bar := s.bHeap[i]
|
| 160 | |
if !cb(bar) {
|
|
155 |
s.hm.iter(iter, drop)
|
|
156 |
}:
|
|
157 |
for b := range iter {
|
|
158 |
if cb(b) {
|
|
159 |
close(drop)
|
| 161 | 160 |
break
|
| 162 | 161 |
}
|
| 163 | 162 |
}
|
| 164 | |
}:
|
| 165 | |
<-sync
|
| 166 | 163 |
case <-p.done:
|
| 167 | 164 |
}
|
| 168 | 165 |
}
|
|
| 175 | 172 |
return
|
| 176 | 173 |
}
|
| 177 | 174 |
b.priority = priority
|
| 178 | |
heap.Fix(&s.bHeap, b.index)
|
|
175 |
s.hm.fix(b.index)
|
| 179 | 176 |
}:
|
| 180 | 177 |
case <-p.done:
|
| 181 | 178 |
}
|
| 182 | 179 |
}
|
| 183 | 180 |
|
| 184 | 181 |
// BarCount returns bars count.
|
| 185 | |
func (p *Progress) BarCount() int {
|
| 186 | |
result := make(chan int)
|
| 187 | |
select {
|
| 188 | |
case p.operateState <- func(s *pState) { result <- s.bHeap.Len() }:
|
| 189 | |
return <-result
|
| 190 | |
case <-p.done:
|
| 191 | |
return 0
|
| 192 | |
}
|
| 193 | |
}
|
|
182 |
// func (p *Progress) BarCount() int {
|
|
183 |
// result := make(chan int)
|
|
184 |
// select {
|
|
185 |
// case p.operateState <- func(s *pState) { result <- s.bHeap.Len() }:
|
|
186 |
// return <-result
|
|
187 |
// case <-p.done:
|
|
188 |
// return 0
|
|
189 |
// }
|
|
190 |
// }
|
| 194 | 191 |
|
| 195 | 192 |
// Write is implementation of io.Writer.
|
| 196 | 193 |
// Writing to `*mpb.Progress` will print lines above a running bar.
|
|
| 267 | 264 |
}
|
| 268 | 265 |
|
| 269 | 266 |
func (p *Progress) serve(s *pState, cw *cwriter.Writer) {
|
| 270 | |
defer close(p.shutdown)
|
| 271 | |
|
| 272 | |
render := func() error {
|
| 273 | |
return s.render(cw)
|
| 274 | |
}
|
|
267 |
|
|
268 |
go s.hm.run()
|
| 275 | 269 |
|
| 276 | 270 |
refreshCh := p.newTicker(s)
|
| 277 | 271 |
|
|
| 282 | 276 |
case fn := <-p.interceptIo:
|
| 283 | 277 |
fn(cw)
|
| 284 | 278 |
case <-refreshCh:
|
| 285 | |
err := render()
|
|
279 |
err := s.render(cw)
|
| 286 | 280 |
if err != nil {
|
| 287 | |
s.heapUpdated = false
|
| 288 | |
render = func() error { return nil }
|
|
281 |
refreshCh = nil
|
| 289 | 282 |
_, _ = fmt.Fprintln(s.debugOut, err.Error())
|
| 290 | 283 |
p.cancel() // cancel all bars
|
| 291 | 284 |
}
|
| 292 | 285 |
case <-p.done:
|
| 293 | |
for s.heapUpdated {
|
| 294 | |
err := render()
|
| 295 | |
if err != nil {
|
| 296 | |
_, _ = fmt.Fprintln(s.debugOut, err.Error())
|
| 297 | |
return
|
| 298 | |
}
|
| 299 | |
}
|
|
286 |
close(s.hm)
|
|
287 |
close(p.shutdown)
|
| 300 | 288 |
return
|
| 301 | 289 |
}
|
| 302 | 290 |
}
|
| 303 | 291 |
}
|
| 304 | 292 |
|
| 305 | 293 |
func (s *pState) render(cw *cwriter.Writer) error {
|
| 306 | |
var wg sync.WaitGroup
|
| 307 | |
if s.heapUpdated {
|
| 308 | |
s.updateSyncMatrix()
|
| 309 | |
s.heapUpdated = false
|
| 310 | |
}
|
| 311 | |
syncWidth(&wg, s.pMatrix)
|
| 312 | |
syncWidth(&wg, s.aMatrix)
|
| 313 | |
|
| 314 | 294 |
width, height, err := cw.GetTermSize()
|
| 315 | 295 |
if err != nil {
|
|
296 |
if !s.ignoreNotTTY {
|
|
297 |
return err
|
|
298 |
}
|
| 316 | 299 |
width = s.reqWidth
|
| 317 | |
height = s.bHeap.Len()
|
| 318 | |
}
|
| 319 | |
for i := 0; i < s.bHeap.Len(); i++ {
|
| 320 | |
bar := s.bHeap[i]
|
| 321 | |
go bar.render(width)
|
| 322 | |
}
|
| 323 | |
|
| 324 | |
err = s.flush(&wg, cw, height)
|
|
300 |
height = 100
|
|
301 |
}
|
|
302 |
|
|
303 |
s.hm.sync()
|
|
304 |
|
|
305 |
iter := make(chan *Bar)
|
|
306 |
s.hm.iter(iter, nil)
|
|
307 |
for b := range iter {
|
|
308 |
go b.render(width)
|
|
309 |
}
|
|
310 |
|
|
311 |
wg := new(sync.WaitGroup)
|
|
312 |
err = s.flush(wg, cw, height)
|
| 325 | 313 |
wg.Wait()
|
| 326 | 314 |
return err
|
| 327 | 315 |
}
|
| 328 | 316 |
|
| 329 | 317 |
func (s *pState) flush(wg *sync.WaitGroup, cw *cwriter.Writer, height int) error {
|
| 330 | 318 |
var popCount int
|
| 331 | |
pool := make([]*Bar, 0, s.bHeap.Len())
|
| 332 | 319 |
s.rows = s.rows[:0]
|
| 333 | 320 |
|
| 334 | |
for s.bHeap.Len() > 0 {
|
| 335 | |
b := heap.Pop(&s.bHeap).(*Bar)
|
|
321 |
iter := make(chan *Bar)
|
|
322 |
drop := make(chan struct{})
|
|
323 |
s.hm.popAll(iter, drop)
|
|
324 |
|
|
325 |
for b := range iter {
|
| 336 | 326 |
frame := <-b.frameCh
|
| 337 | 327 |
if frame.err != nil {
|
| 338 | |
// b.frameCh is buffered it's ok to return here
|
| 339 | |
return frame.err
|
|
328 |
close(drop)
|
|
329 |
return frame.err // b.frameCh is buffered it's ok to return here
|
| 340 | 330 |
}
|
| 341 | 331 |
var usedRows int
|
| 342 | 332 |
for i := len(frame.rows) - 1; i >= 0; i-- {
|
|
| 344 | 334 |
s.rows = append(s.rows, row)
|
| 345 | 335 |
usedRows++
|
| 346 | 336 |
} else {
|
| 347 | |
wg.Add(1)
|
| 348 | |
go func() {
|
| 349 | |
_, _ = io.Copy(io.Discard, row)
|
| 350 | |
wg.Done()
|
| 351 | |
}()
|
|
337 |
_, _ = io.Copy(io.Discard, row)
|
| 352 | 338 |
}
|
| 353 | 339 |
}
|
| 354 | 340 |
if frame.shutdown {
|
|
| 356 | 342 |
if qb, ok := s.queueBars[b]; ok {
|
| 357 | 343 |
delete(s.queueBars, b)
|
| 358 | 344 |
qb.priority = b.priority
|
| 359 | |
pool = append(pool, qb)
|
| 360 | |
s.heapUpdated = true
|
|
345 |
wg.Add(1)
|
|
346 |
go func(b *Bar) {
|
|
347 |
s.hm.push(b, true)
|
|
348 |
wg.Done()
|
|
349 |
}(qb)
|
| 361 | 350 |
continue
|
| 362 | 351 |
}
|
| 363 | 352 |
if s.popCompleted && !b.bs.noPop {
|
|
| 368 | 357 |
default:
|
| 369 | 358 |
if b.bs.dropOnComplete {
|
| 370 | 359 |
popCount += usedRows
|
| 371 | |
s.heapUpdated = true
|
| 372 | 360 |
continue
|
| 373 | 361 |
}
|
| 374 | 362 |
}
|
| 375 | 363 |
} else if b.bs.dropOnComplete {
|
| 376 | |
s.heapUpdated = true
|
| 377 | 364 |
continue
|
| 378 | 365 |
}
|
| 379 | 366 |
}
|
| 380 | |
pool = append(pool, b)
|
| 381 | |
}
|
| 382 | |
|
| 383 | |
if len(pool) != 0 {
|
| 384 | 367 |
wg.Add(1)
|
| 385 | |
go func() {
|
| 386 | |
for _, b := range pool {
|
| 387 | |
heap.Push(&s.bHeap, b)
|
| 388 | |
}
|
|
368 |
go func(b *Bar) {
|
|
369 |
s.hm.push(b, false)
|
| 389 | 370 |
wg.Done()
|
| 390 | |
}()
|
|
371 |
}(b)
|
| 391 | 372 |
}
|
| 392 | 373 |
|
| 393 | 374 |
for i := len(s.rows) - 1; i >= 0; i-- {
|
|
| 397 | 378 |
}
|
| 398 | 379 |
}
|
| 399 | 380 |
|
| 400 | |
err := cw.Flush(len(s.rows) - popCount)
|
| 401 | |
return err
|
| 402 | |
}
|
| 403 | |
|
| 404 | |
func (s *pState) updateSyncMatrix() {
|
| 405 | |
s.pMatrix = make(map[int][]chan int)
|
| 406 | |
s.aMatrix = make(map[int][]chan int)
|
| 407 | |
for i := 0; i < s.bHeap.Len(); i++ {
|
| 408 | |
bar := s.bHeap[i]
|
| 409 | |
table := bar.wSyncTable()
|
| 410 | |
|
| 411 | |
for i, ch := range table[0] {
|
| 412 | |
s.pMatrix[i] = append(s.pMatrix[i], ch)
|
| 413 | |
}
|
| 414 | |
|
| 415 | |
for i, ch := range table[1] {
|
| 416 | |
s.aMatrix[i] = append(s.aMatrix[i], ch)
|
| 417 | |
}
|
| 418 | |
}
|
|
381 |
return cw.Flush(len(s.rows) - popCount)
|
| 419 | 382 |
}
|
| 420 | 383 |
|
| 421 | 384 |
func (s *pState) makeBarState(total int64, filler BarFiller, options ...BarOption) *bState {
|
|
| 452 | 415 |
return bs
|
| 453 | 416 |
}
|
| 454 | 417 |
|
| 455 | |
func syncWidth(wg *sync.WaitGroup, matrix map[int][]chan int) {
|
|
418 |
func syncWidth(matrix map[int][]chan int) {
|
| 456 | 419 |
for _, column := range matrix {
|
| 457 | |
wg.Add(1)
|
| 458 | |
go maxWidthDistributor(wg, column)
|
| 459 | |
}
|
| 460 | |
}
|
| 461 | |
|
| 462 | |
func maxWidthDistributor(wg *sync.WaitGroup, column []chan int) {
|
|
420 |
go maxWidthDistributor(column)
|
|
421 |
}
|
|
422 |
}
|
|
423 |
|
|
424 |
func maxWidthDistributor(column []chan int) {
|
| 463 | 425 |
var maxWidth int
|
| 464 | 426 |
for _, ch := range column {
|
| 465 | 427 |
if w := <-ch; w > maxWidth {
|
|
| 469 | 431 |
for _, ch := range column {
|
| 470 | 432 |
ch <- maxWidth
|
| 471 | 433 |
}
|
| 472 | |
wg.Done()
|
| 473 | |
}
|
|
434 |
}
|