data race in b.draw fix
Vladimir Bauer
9 years ago
| 17 | 17 | leftEnd byte |
| 18 | 18 | rightEnd byte |
| 19 | 19 | |
| 20 | trimLeftSpace, trimRightSpace bool | |
| 21 | ||
| 22 | 20 | incrCh chan int |
| 21 | trimLeftCh chan bool | |
| 22 | trimRightCh chan bool | |
| 23 | 23 | redrawReqCh chan *redrawRequest |
| 24 | 24 | currentReqCh chan chan int |
| 25 | 25 | statusReqCh chan chan int |
| 56 | 56 | width: width, |
| 57 | 57 | |
| 58 | 58 | incrCh: make(chan int), |
| 59 | trimLeftCh: make(chan bool), | |
| 60 | trimRightCh: make(chan bool), | |
| 59 | 61 | redrawReqCh: make(chan *redrawRequest), |
| 60 | 62 | currentReqCh: make(chan chan int), |
| 61 | 63 | statusReqCh: make(chan chan int), |
| 79 | 81 | |
| 80 | 82 | // TrimLeftSpace removes space befor LeftEnd charater |
| 81 | 83 | func (b *Bar) TrimLeftSpace() *Bar { |
| 82 | b.trimLeftSpace = true | |
| 84 | if !b.isDone() { | |
| 85 | b.trimLeftCh <- true | |
| 86 | } | |
| 83 | 87 | return b |
| 84 | 88 | } |
| 85 | 89 | |
| 86 | 90 | // TrimRightSpace removes space after RightEnd charater |
| 87 | 91 | func (b *Bar) TrimRightSpace() *Bar { |
| 88 | b.trimRightSpace = true | |
| 92 | if !b.isDone() { | |
| 93 | b.trimRightCh <- true | |
| 94 | } | |
| 89 | 95 | return b |
| 90 | 96 | } |
| 91 | 97 | |
| 169 | 175 | |
| 170 | 176 | // PrependFunc prepends DecoratorFunc |
| 171 | 177 | func (b *Bar) PrependFunc(f DecoratorFunc) *Bar { |
| 172 | b.decoratorCh <- &decorator{decoratorPrepend, f} | |
| 178 | if !b.isDone() { | |
| 179 | b.decoratorCh <- &decorator{decoratorPrepend, f} | |
| 180 | } | |
| 173 | 181 | return b |
| 174 | 182 | } |
| 175 | 183 | |
| 176 | 184 | // AppendFunc appends DecoratorFunc |
| 177 | 185 | func (b *Bar) AppendFunc(f DecoratorFunc) *Bar { |
| 178 | b.decoratorCh <- &decorator{decoratorAppend, f} | |
| 186 | if !b.isDone() { | |
| 187 | b.decoratorCh <- &decorator{decoratorAppend, f} | |
| 188 | } | |
| 179 | 189 | return b |
| 180 | 190 | } |
| 181 | 191 | |
| 193 | 203 | blockStartTime := timeStarted |
| 194 | 204 | var timePerItem, timeElapsed time.Duration |
| 195 | 205 | var appendFuncs, prependFuncs []DecoratorFunc |
| 196 | var completed, wgDoneReported bool | |
| 206 | var completed, wgDoneReported, trimLeftSpace, trimRightSpace bool | |
| 197 | 207 | var current int |
| 198 | 208 | for { |
| 199 | 209 | select { |
| 222 | 232 | respCh <- current |
| 223 | 233 | case r := <-b.redrawReqCh: |
| 224 | 234 | 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) | |
| 226 | 236 | case respCh := <-b.statusReqCh: |
| 227 | 237 | respCh <- percentage(total, current, 100) |
| 238 | case result := <-b.trimLeftCh: | |
| 239 | trimLeftSpace = result | |
| 240 | case result := <-b.trimRightCh: | |
| 241 | trimRightSpace = result | |
| 228 | 242 | case <-b.flushedCh: |
| 229 | 243 | if completed && !wgDoneReported { |
| 230 | 244 | wgDoneReported = true |
| 240 | 254 | } |
| 241 | 255 | } |
| 242 | 256 | |
| 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 { | |
| 244 | 258 | |
| 245 | 259 | buf := make([]byte, 0, stat.TermWidth) |
| 246 | 260 | |
| 264 | 278 | |
| 265 | 279 | var leftSpace, rightSpace []byte |
| 266 | 280 | space := []byte{' '} |
| 267 | if !b.trimLeftSpace { | |
| 281 | ||
| 282 | if !trimLeftSpace { | |
| 268 | 283 | prependCount++ |
| 269 | 284 | leftSpace = space |
| 270 | 285 | } |
| 271 | if !b.trimRightSpace { | |
| 286 | if !trimRightSpace { | |
| 272 | 287 | appendCount++ |
| 273 | 288 | rightSpace = space |
| 274 | 289 | } |