Codebase list golang-github-vbauerster-mpb / faddb54
data race in b.draw fix Vladimir Bauer 9 years ago
1 changed file(s) with 26 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
1717 leftEnd byte
1818 rightEnd byte
1919
20 trimLeftSpace, trimRightSpace bool
21
2220 incrCh chan int
21 trimLeftCh chan bool
22 trimRightCh chan bool
2323 redrawReqCh chan *redrawRequest
2424 currentReqCh chan chan int
2525 statusReqCh chan chan int
5656 width: width,
5757
5858 incrCh: make(chan int),
59 trimLeftCh: make(chan bool),
60 trimRightCh: make(chan bool),
5961 redrawReqCh: make(chan *redrawRequest),
6062 currentReqCh: make(chan chan int),
6163 statusReqCh: make(chan chan int),
7981
8082 // TrimLeftSpace removes space befor LeftEnd charater
8183 func (b *Bar) TrimLeftSpace() *Bar {
82 b.trimLeftSpace = true
84 if !b.isDone() {
85 b.trimLeftCh <- true
86 }
8387 return b
8488 }
8589
8690 // TrimRightSpace removes space after RightEnd charater
8791 func (b *Bar) TrimRightSpace() *Bar {
88 b.trimRightSpace = true
92 if !b.isDone() {
93 b.trimRightCh <- true
94 }
8995 return b
9096 }
9197
169175
170176 // PrependFunc prepends DecoratorFunc
171177 func (b *Bar) PrependFunc(f DecoratorFunc) *Bar {
172 b.decoratorCh <- &decorator{decoratorPrepend, f}
178 if !b.isDone() {
179 b.decoratorCh <- &decorator{decoratorPrepend, f}
180 }
173181 return b
174182 }
175183
176184 // AppendFunc appends DecoratorFunc
177185 func (b *Bar) AppendFunc(f DecoratorFunc) *Bar {
178 b.decoratorCh <- &decorator{decoratorAppend, f}
186 if !b.isDone() {
187 b.decoratorCh <- &decorator{decoratorAppend, f}
188 }
179189 return b
180190 }
181191
193203 blockStartTime := timeStarted
194204 var timePerItem, timeElapsed time.Duration
195205 var appendFuncs, prependFuncs []DecoratorFunc
196 var completed, wgDoneReported bool
206 var completed, wgDoneReported, trimLeftSpace, trimRightSpace bool
197207 var current int
198208 for {
199209 select {
222232 respCh <- current
223233 case r := <-b.redrawReqCh:
224234 stat := &Statistics{total, current, r.width, timeElapsed, timePerItem}
225 r.respCh <- b.draw(stat, appendFuncs, prependFuncs)
235 r.respCh <- b.draw(stat, appendFuncs, prependFuncs, trimLeftSpace, trimRightSpace)
226236 case respCh := <-b.statusReqCh:
227237 respCh <- percentage(total, current, 100)
238 case result := <-b.trimLeftCh:
239 trimLeftSpace = result
240 case result := <-b.trimRightCh:
241 trimRightSpace = result
228242 case <-b.flushedCh:
229243 if completed && !wgDoneReported {
230244 wgDoneReported = true
240254 }
241255 }
242256
243 func (b *Bar) draw(stat *Statistics, appendFuncs, prependFuncs []DecoratorFunc) []byte {
257 func (b *Bar) draw(stat *Statistics, appendFuncs, prependFuncs []DecoratorFunc, trimLeftSpace, trimRightSpace bool) []byte {
244258
245259 buf := make([]byte, 0, stat.TermWidth)
246260
264278
265279 var leftSpace, rightSpace []byte
266280 space := []byte{' '}
267 if !b.trimLeftSpace {
281
282 if !trimLeftSpace {
268283 prependCount++
269284 leftSpace = space
270285 }
271 if !b.trimRightSpace {
286 if !trimRightSpace {
272287 appendCount++
273288 rightSpace = space
274289 }