call b.stop in defer
Vladimir Bauer
9 years ago
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | func (b *Bar) server(ctx context.Context, wg *sync.WaitGroup, total int64) { |
| 201 | defer wg.Done() | |
| 202 | 201 | var completed bool |
| 203 | state := state{total: total} | |
| 204 | 202 | timeStarted := time.Now() |
| 205 | 203 | blockStartTime := timeStarted |
| 204 | state := state{total: total} | |
| 205 | defer func() { | |
| 206 | b.stop(&state) | |
| 207 | wg.Done() | |
| 208 | }() | |
| 206 | 209 | for { |
| 207 | 210 | select { |
| 208 | 211 | case i := <-b.incrCh: |
| 233 | 236 | case state.trimRightSpace = <-b.trimRightCh: |
| 234 | 237 | case <-b.flushedCh: |
| 235 | 238 | if completed { |
| 236 | b.stop(&state) | |
| 237 | 239 | return |
| 238 | 240 | } |
| 239 | 241 | case <-b.removeReqCh: |
| 240 | b.stop(&state) | |
| 241 | 242 | return |
| 242 | 243 | case <-ctx.Done(): |
| 243 | b.stop(&state) | |
| 244 | 244 | return |
| 245 | 245 | } |
| 246 | 246 | } |
| 252 | 252 | } |
| 253 | 253 | |
| 254 | 254 | func (b *Bar) draw(s state, termWidth int) []byte { |
| 255 | ||
| 256 | 255 | buf := make([]byte, 0, termWidth) |
| 256 | ||
| 257 | 257 | stat := &Statistics{ |
| 258 | 258 | Total: s.total, |
| 259 | 259 | Current: s.current, |
| 328 | 328 | |
| 329 | 329 | return buf |
| 330 | 330 | } |
| 331 | ||
| 332 | // func (b *Bar) closeDone() { | |
| 333 | // if !b.isDone() { | |
| 334 | // close(b.done) | |
| 335 | // } | |
| 336 | // } | |
| 337 | 331 | |
| 338 | 332 | func (b *Bar) isDone() bool { |
| 339 | 333 | select { |