Codebase list golang-github-vbauerster-mpb / 1168f54
New BarOptions, BarReplaceOnComplete and BarClearOnComplete Vladimir Bauer 8 years ago
3 changed file(s) with 62 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
3030 type Bar struct {
3131 priority int
3232 index int
33
34 // pointer to running bar, which this bar should replace
35 waitBar *Bar
3336
3437 // completed is set from master Progress goroutine only
3538 completed bool
5962 trimRightSpace bool
6063 toComplete bool
6164 dynamic bool
65 noBarOnComplete bool
6266 startTime time.Time
6367 timeElapsed time.Duration
6468 blockStartTime time.Time
7276 // following options are assigned to the *Bar
7377 priority int
7478 removeOnComplete bool
79 waitBar *Bar
7580 }
7681 refill struct {
7782 char rune
109114 b := &Bar{
110115 priority: s.priority,
111116 removeOnComplete: s.removeOnComplete,
117 waitBar: s.waitBar,
112118 operateState: make(chan func(*bState)),
113119 done: make(chan struct{}),
114120 shutdown: make(chan struct{}),
121 }
122
123 if b.waitBar != nil {
124 b.priority = b.waitBar.priority
115125 }
116126
117127 go b.serve(wg, s, cancel)
321331 }
322332
323333 func (s *bState) draw(termWidth int, pSyncer, aSyncer *widthSyncer) io.Reader {
334 defer s.bufA.WriteByte('\n')
335
324336 if termWidth <= 0 {
325337 termWidth = s.width
326338 }
332344 s.bufP.WriteString(f(stat, pSyncer.Accumulator[i], pSyncer.Distributor[i]))
333345 }
334346
335 if !s.trimLeftSpace {
336 s.bufP.WriteByte(' ')
337 }
338
339 // render append functions to the right of the bar
340 if !s.trimRightSpace {
341 s.bufA.WriteByte(' ')
342 }
343
344347 for i, f := range s.aDecorators {
345348 s.bufA.WriteString(f(stat, aSyncer.Accumulator[i], aSyncer.Distributor[i]))
346349 }
347350
348351 prependCount := utf8.RuneCount(s.bufP.Bytes())
349352 appendCount := utf8.RuneCount(s.bufA.Bytes())
353
354 if s.toComplete && s.noBarOnComplete {
355 return io.MultiReader(s.bufP, s.bufA)
356 }
350357
351358 s.fillBar(s.width)
352359 barCount := utf8.RuneCount(s.bufB.Bytes())
355362 s.fillBar(termWidth - prependCount - appendCount)
356363 }
357364
358 s.bufA.WriteByte('\n')
359365 return io.MultiReader(s.bufP, s.bufB, s.bufA)
360366 }
361367
362368 func (s *bState) fillBar(width int) {
369 defer func() {
370 if !s.trimRightSpace {
371 s.bufB.WriteByte(' ')
372 }
373 }()
363374 s.bufB.Reset()
375 if !s.trimLeftSpace {
376 s.bufB.WriteByte(' ')
377 }
364378 s.bufB.WriteRune(s.runes[rLeft])
365379 if width <= 2 {
366380 s.bufB.WriteRune(s.runes[rRight])
00 package mpb
11
2 import "github.com/vbauerster/mpb/decor"
2 import (
3 "github.com/vbauerster/mpb/decor"
4 )
35
46 // BarOption is a function option which changes the default behavior of a bar,
57 // if passed to p.AddBar(int64, ...BarOption)
7476 }
7577 }
7678
77 // BarRemoveOnComplete is a flag, which tells whether the intention is to remove the bar after completion.
79 // BarRemoveOnComplete is a flag, which will trigger bar auto remove on completion event.
7880 func BarRemoveOnComplete() BarOption {
7981 return func(s *bState) {
8082 s.removeOnComplete = true
8385
8486 // BarPriority sets bar's priority.
8587 // Zero is highest priority, i.e. bar will be on top.
88 // If `BarReplaceOnComplete` option is supplied, this option is ignored.
8689 func BarPriority(priority int) BarOption {
8790 return func(s *bState) {
8891 s.priority = priority
92 }
93 }
94
95 // BarReplaceOnComplete provided `b` is usually already running bar which this bar should replace.
96 func BarReplaceOnComplete(b *Bar) BarOption {
97 return func(s *bState) {
98 s.waitBar = b
99 }
100 }
101
102 // BarClearOnComplete clears the bar section on complete event.
103 func BarClearOnComplete() BarOption {
104 return func(s *bState) {
105 s.noBarOnComplete = true
89106 }
90107 }
91108
4545 cancel <-chan struct{}
4646 shutdownNotifier chan struct{}
4747 interceptors []func(io.Writer)
48 waitBars map[*Bar]*Bar
4849 }
4950 widthSyncer struct {
5051 // Public for easy testing
5960 pq := make(priorityQueue, 0)
6061 heap.Init(&pq)
6162 s := &pState{
62 bHeap: &pq,
63 width: pwidth,
64 format: pformat,
65 cw: cwriter.New(os.Stdout),
66 rr: prr,
67 ticker: time.NewTicker(prr),
63 bHeap: &pq,
64 width: pwidth,
65 format: pformat,
66 cw: cwriter.New(os.Stdout),
67 rr: prr,
68 ticker: time.NewTicker(prr),
69 waitBars: make(map[*Bar]*Bar),
6870 }
6971
7072 for _, opt := range options {
9193 case p.operateState <- func(s *pState) {
9294 options = append(options, barWidth(s.width), barFormat(s.format))
9395 b := newBar(p.wg, s.idCounter, total, s.cancel, options...)
94 heap.Push(s.bHeap, b)
95 s.heapUpdated = true
96 if b.waitBar != nil {
97 s.waitBars[b.waitBar] = b
98 } else {
99 heap.Push(s.bHeap, b)
100 s.heapUpdated = true
101 }
96102 s.idCounter++
97103 result <- b
98104 }:
207213 if rs.bar.removeOnComplete {
208214 s.heapUpdated = heap.Remove(s.bHeap, rs.bar.index) != nil
209215 }
216 if replacementBar, ok := s.waitBars[rs.bar]; ok {
217 heap.Push(s.bHeap, replacementBar)
218 s.heapUpdated = true
219 delete(s.waitBars, rs.bar)
220 }
210221 defer func() {
211222 s.shutdownPending = append(s.shutdownPending, rs.bar)
212223 }()