Codebase list golang-github-vbauerster-mpb / 9843691
Func mpb.New() signature change; new methods: WithContext, WithCancel Vladimir Bauer 9 years ago
3 changed file(s) with 47 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
00 package mpb
11
22 import (
3 "context"
43 "io"
54 "sync"
65 "time"
7877 }
7978 )
8079
81 func newBar(ctx context.Context, wg *sync.WaitGroup, id int, total int64, width int, format string) *Bar {
80 func newBar(id int, total int64, width int, format string, wg *sync.WaitGroup, cancel <-chan struct{}) *Bar {
8281 b := &Bar{
8382 stateReqCh: make(chan chan state, 1),
8483 widthCh: make(chan int),
9493 completeReqCh: make(chan struct{}),
9594 done: make(chan struct{}),
9695 }
97 go b.server(ctx, wg, id, total, width, format)
96 go b.server(id, total, width, format, wg, cancel)
9897 return b
9998 }
10099
259258 return <-ch
260259 }
261260
262 func (b *Bar) server(ctx context.Context, wg *sync.WaitGroup, id int, total int64, width int, format string) {
261 func (b *Bar) server(id int, total int64, width int, format string, wg *sync.WaitGroup, cancel <-chan struct{}) {
263262 var completed bool
264263 timeStarted := time.Now()
265264 blockStartTime := timeStarted
323322 return
324323 case <-b.removeReqCh:
325324 return
326 case <-ctx.Done():
325 case <-cancel:
327326 return
328327 }
329328 }
00 package mpb
11
22 import (
3 "context"
43 "errors"
54 "io"
65 "log"
6463 // Progress represents the container that renders Progress bars
6564 type Progress struct {
6665 // Context for canceling bars rendering
67 ctx context.Context
66 // ctx context.Context
6867 // WaitGroup for internal rendering sync
6968 wg *sync.WaitGroup
7069
7877 barCountReqCh chan chan int
7978 brCh chan BeforeRender
8079 done chan struct{}
80 cancel <-chan struct{}
8181 }
8282
8383 // New creates new Progress instance, which will orchestrate bars rendering
8484 // process. It acceepts context.Context, for cancellation.
8585 // If you don't plan to cancel, it is safe to feed with nil
86 func New(ctx context.Context) *Progress {
87 if ctx == nil {
88 ctx = context.Background()
89 }
86 func New() *Progress {
9087 p := &Progress{
9188 width: pwidth,
9289 operationCh: make(chan *operation),
9693 brCh: make(chan BeforeRender),
9794 done: make(chan struct{}),
9895 wg: new(sync.WaitGroup),
99 ctx: ctx,
10096 }
10197 go p.server(cwriter.New(os.Stdout), time.NewTicker(rr*time.Millisecond))
10298 return p
10399 }
104100
101 // WithCancel cancellation via channel
102 func (p *Progress) WithCancel(ch <-chan struct{}) *Progress {
103 if ch == nil {
104 panic("nil cancel channel")
105 }
106 p2 := new(Progress)
107 *p2 = *p
108 p2.cancel = ch
109 return p2
110 }
111
105112 // SetWidth overrides default (70) width of bar(s)
106113 func (p *Progress) SetWidth(n int) *Progress {
107 if n <= 0 {
108 return p
109 }
110 p.width = n
111 return p
114 if n < 0 {
115 panic("negative width")
116 }
117 p2 := new(Progress)
118 *p2 = *p
119 p2.width = n
120 return p2
112121 }
113122
114123 // SetOut sets underlying writer of progress. Default is os.Stdout
156165 panic(ErrCallAfterStop)
157166 }
158167 result := make(chan bool)
159 bar := newBar(p.ctx, p.wg, id, total, p.width, p.format)
168 bar := newBar(id, total, p.width, p.format, p.wg, p.cancel)
160169 p.operationCh <- &operation{barAdd, bar, result}
161170 if <-result {
162171 p.wg.Add(1)
196205 return p
197206 }
198207
199 // Stop waits for bars to finish rendering and stops the rendering goroutine
208 // Stop shutdowns Progress' goroutine
209 // Should be called only after each bor's work done, i.e. bar has reached its
210 // 100 %. It is NOT for concelation. Use WithContext or WithCancel for
211 // cancelation purposes.
200212 func (p *Progress) Stop() {
201213 p.wg.Wait()
202214 if isClosed(p.done) {
304316 case d := <-p.rrChangeReqCh:
305317 t.Stop()
306318 t = time.NewTicker(d)
307 case <-p.ctx.Done():
319 case <-p.cancel:
308320 return
309321 }
310322 }
0 //+build go1.7
1
2 package mpb
3
4 import "context"
5
6 // WithContext cancellation via context
7 func (p *Progress) WithContext(ctx context.Context) *Progress {
8 if ctx == nil {
9 panic("nil context")
10 }
11 p2 := new(Progress)
12 *p2 = *p
13 p2.cancel = ctx.Done()
14 return p2
15 }