Codebase list golang-github-vbauerster-mpb / 0a872e9
Refactor userConf to pConf Vladimir Bauer 9 years ago
2 changed file(s) with 29 addition(s) and 48 deletion(s). Raw diff Collapse all Expand all
1919 result []chan int
2020 }
2121
22 // config changeable by user
23 userConf struct {
22 // progress config, all fieals are adjustable by user
23 pConf struct {
2424 bars []*Bar
2525
2626 width int
5252 wg *sync.WaitGroup
5353
5454 done chan struct{}
55 ops chan func(*userConf)
55 ops chan func(*pConf)
5656 stopReqCh chan struct{}
5757
5858 // following is used after (*Progress.done) is closed
59 conf userConf
59 conf pConf
6060 }
6161
6262 // New creates new Progress instance, which will orchestrate bars rendering
6666 p := &Progress{
6767 wg: new(sync.WaitGroup),
6868 done: make(chan struct{}),
69 ops: make(chan func(*userConf)),
69 ops: make(chan func(*pConf)),
7070 stopReqCh: make(chan struct{}),
7171 }
72 go p.server(userConf{
72 go p.server(pConf{
7373 bars: make([]*Bar, 0, 3),
7474 width: pwidth,
7575 format: pformat,
8686 if ch == nil {
8787 panic("nil cancel channel")
8888 }
89 return updateConf(p, func(c *userConf) {
89 return updateConf(p, func(c *pConf) {
9090 c.cancel = ch
9191 })
9292 }
9696 if width < 2 {
9797 return p
9898 }
99 return updateConf(p, func(c *userConf) {
99 return updateConf(p, func(c *pConf) {
100100 c.width = width
101101 })
102102 }
106106 if w == nil {
107107 return p
108108 }
109 return updateConf(p, func(c *userConf) {
109 return updateConf(p, func(c *pConf) {
110110 c.cw.Flush()
111111 c.cw = cwriter.New(w)
112112 })
114114
115115 // RefreshRate overrides default (100ms) refresh rate value
116116 func (p *Progress) RefreshRate(d time.Duration) *Progress {
117 return updateConf(p, func(c *userConf) {
117 return updateConf(p, func(c *pConf) {
118118 c.ticker.Stop()
119119 c.ticker = time.NewTicker(d)
120120 c.rr = d
123123
124124 // BeforeRenderFunc accepts a func, which gets called before render process.
125125 func (p *Progress) BeforeRenderFunc(f BeforeRender) *Progress {
126 return updateConf(p, func(c *userConf) {
126 return updateConf(p, func(c *pConf) {
127127 c.beforeRender = f
128128 })
129129 }
136136 // AddBarWithID creates a new progress bar and adds to the container.
137137 func (p *Progress) AddBarWithID(id int, total int64) *Bar {
138138 result := make(chan *Bar, 1)
139 op := func(c *userConf) {
139 op := func(c *pConf) {
140140 bar := newBar(id, total, c.width, c.format, p.wg, c.cancel)
141141 c.bars = append(c.bars, bar)
142142 p.wg.Add(1)
153153 // RemoveBar removes bar at any time.
154154 func (p *Progress) RemoveBar(b *Bar) bool {
155155 result := make(chan bool, 1)
156 op := func(c *userConf) {
156 op := func(c *pConf) {
157157 var ok bool
158158 for i, bar := range c.bars {
159159 if bar == b {
173173 }
174174 }
175175
176 // BarCount returns bars count in the container.
176 // BarCount returns bars count
177177 func (p *Progress) BarCount() int {
178178 result := make(chan int, 1)
179 op := func(c *userConf) {
179 op := func(c *pConf) {
180180 result <- len(c.bars)
181181 }
182182 select {
189189
190190 // ShutdownNotify means to be notified when main rendering goroutine quits, usualy after p.Stop() call.
191191 func (p *Progress) ShutdownNotify(ch chan struct{}) *Progress {
192 return updateConf(p, func(c *userConf) {
192 return updateConf(p, func(c *pConf) {
193193 c.shutdownNotifier = ch
194194 })
195195 }
199199 if utf8.RuneCountInString(format) != numFmtRunes {
200200 return p
201201 }
202 return updateConf(p, func(c *userConf) {
202 return updateConf(p, func(c *pConf) {
203203 c.format = format
204204 })
205205 }
214214 return
215215 default:
216216 // complete Total unknown bars
217 p.ops <- func(c *userConf) {
217 p.ops <- func(c *pConf) {
218218 for _, b := range c.bars {
219219 s := b.getState()
220220 if !s.completed && !s.aborted {
231231 }
232232 }
233233
234 // func (p *Progress) getConf() userConf {
235 // select {
236 // case conf := <-p.userConfCh:
237 // return conf
238 // case <-p.done:
239 // return p.conf
240 // }
241 // }
242
243 // func (p *Progress) updateConf(op func(*userConf)) {
244 // // c := p.getConf()
245 // // cb(&c)
246 // select {
247 // case p.ops <- op:
248 // case <-p.done:
249 // return
250 // }
251 // }
252
253 func updateConf(p *Progress, op func(*userConf)) *Progress {
254 select {
255 case p.ops <- op:
256 return p
257 case <-p.done:
258 return nil
259 }
260 }
261
262234 // server monitors underlying channels and renders any progress bars
263 func (p *Progress) server(conf userConf) {
235 func (p *Progress) server(conf pConf) {
264236
265237 defer func() {
266238 conf.ticker.Stop()
385357 return ch
386358 }
387359
360 func updateConf(p *Progress, op func(*pConf)) *Progress {
361 select {
362 case p.ops <- op:
363 return p
364 case <-p.done:
365 return nil
366 }
367 }
368
388369 func max(slice []int) int {
389370 max := slice[0]
390371
99 if ctx == nil {
1010 panic("nil context")
1111 }
12 return updateConf(p, func(c *userConf) {
12 return updateConf(p, func(c *pConf) {
1313 c.cancel = ctx.Done()
1414 })
1515 }