refactoring manualRefresh to refreshCh
Vladimir Bauer
3 years ago
| 50 | 50 | buffers [3]*bytes.Buffer |
| 51 | 51 | filler BarFiller |
| 52 | 52 | extender extenderFunc |
| 53 | manualRefresh chan interface{} | |
| 53 | refreshCh chan time.Time | |
| 54 | 54 | |
| 55 | 55 | wait struct { |
| 56 | 56 | bar *Bar // key for (*pState).queueBars |
| 185 | 185 | if s.current >= s.total { |
| 186 | 186 | s.current = s.total |
| 187 | 187 | s.completed = true |
| 188 | b.forceRefresh(s.manualRefresh) | |
| 188 | b.forceRefresh(s.refreshCh) | |
| 189 | 189 | } else { |
| 190 | 190 | s.triggerComplete = true |
| 191 | 191 | } |
| 213 | 213 | if triggerCompleteNow { |
| 214 | 214 | s.current = s.total |
| 215 | 215 | s.completed = true |
| 216 | b.forceRefresh(s.manualRefresh) | |
| 216 | b.forceRefresh(s.refreshCh) | |
| 217 | 217 | } |
| 218 | 218 | }: |
| 219 | 219 | case <-b.done: |
| 231 | 231 | if s.triggerComplete && s.current >= s.total { |
| 232 | 232 | s.current = s.total |
| 233 | 233 | s.completed = true |
| 234 | b.forceRefresh(s.manualRefresh) | |
| 234 | b.forceRefresh(s.refreshCh) | |
| 235 | 235 | } |
| 236 | 236 | }: |
| 237 | 237 | case <-b.done: |
| 253 | 253 | if s.triggerComplete && s.current >= s.total { |
| 254 | 254 | s.current = s.total |
| 255 | 255 | s.completed = true |
| 256 | b.forceRefresh(s.manualRefresh) | |
| 256 | b.forceRefresh(s.refreshCh) | |
| 257 | 257 | } |
| 258 | 258 | }: |
| 259 | 259 | case <-b.done: |
| 281 | 281 | if s.triggerComplete && s.current >= s.total { |
| 282 | 282 | s.current = s.total |
| 283 | 283 | s.completed = true |
| 284 | b.forceRefresh(s.manualRefresh) | |
| 284 | b.forceRefresh(s.refreshCh) | |
| 285 | 285 | } |
| 286 | 286 | }: |
| 287 | 287 | case <-b.done: |
| 311 | 311 | if s.triggerComplete && s.current >= s.total { |
| 312 | 312 | s.current = s.total |
| 313 | 313 | s.completed = true |
| 314 | b.forceRefresh(s.manualRefresh) | |
| 314 | b.forceRefresh(s.refreshCh) | |
| 315 | 315 | } |
| 316 | 316 | }: |
| 317 | 317 | case <-b.done: |
| 349 | 349 | } |
| 350 | 350 | s.aborted = true |
| 351 | 351 | s.dropOnComplete = drop |
| 352 | b.forceRefresh(s.manualRefresh) | |
| 352 | b.forceRefresh(s.refreshCh) | |
| 353 | 353 | }: |
| 354 | 354 | case <-b.done: |
| 355 | 355 | } |
| 448 | 448 | } |
| 449 | 449 | } |
| 450 | 450 | |
| 451 | func (b *Bar) forceRefresh(refreshCh chan<- interface{}) { | |
| 451 | func (b *Bar) forceRefresh(refreshCh chan<- time.Time) { | |
| 452 | 452 | b.container.bwg.Add(1) |
| 453 | 453 | go b.forceRefreshImpl(refreshCh) |
| 454 | 454 | } |
| 455 | 455 | |
| 456 | func (b *Bar) forceRefreshImpl(refreshCh chan<- interface{}) { | |
| 456 | func (b *Bar) forceRefreshImpl(refreshCh chan<- time.Time) { | |
| 457 | 457 | defer b.container.bwg.Done() |
| 458 | 458 | var anyOtherRunning bool |
| 459 | 459 | b.container.traverseBars(func(bar *Bar) bool { |
| 38 | 38 | |
| 39 | 39 | // WithManualRefresh disables internal auto refresh time.Ticker. |
| 40 | 40 | // Refresh will occur upon receive value from provided ch. |
| 41 | func WithManualRefresh(ch chan interface{}) ContainerOption { | |
| 41 | func WithManualRefresh(ch <-chan interface{}) ContainerOption { | |
| 42 | 42 | return func(s *pState) { |
| 43 | s.manualRefresh = ch | |
| 44 | s.disableAutoRefresh = true | |
| 43 | s.manualRefresh = true | |
| 44 | go func(refreshCh chan<- time.Time, done <-chan struct{}) { | |
| 45 | for { | |
| 46 | select { | |
| 47 | case x := <-ch: | |
| 48 | if t, ok := x.(time.Time); ok { | |
| 49 | refreshCh <- t | |
| 50 | } else { | |
| 51 | refreshCh <- time.Now() | |
| 52 | } | |
| 53 | case <-done: | |
| 54 | return | |
| 55 | } | |
| 56 | } | |
| 57 | }(s.refreshCh, s.ctx.Done()) | |
| 45 | 58 | } |
| 46 | 59 | } |
| 47 | 60 |
| 35 | 35 | ctx context.Context |
| 36 | 36 | hm heapManager |
| 37 | 37 | dropS, dropD chan struct{} |
| 38 | refreshCh chan time.Time | |
| 38 | 39 | rows []io.Reader |
| 39 | 40 | |
| 40 | 41 | // following are provided/overrided by user |
| 41 | refreshRate time.Duration | |
| 42 | idCount int | |
| 43 | reqWidth int | |
| 44 | popPriority int | |
| 45 | popCompleted bool | |
| 46 | disableAutoRefresh bool | |
| 47 | forceAutoRefresh bool | |
| 48 | manualRefresh chan interface{} | |
| 49 | renderDelay <-chan struct{} | |
| 50 | shutdownNotifier chan<- interface{} | |
| 51 | queueBars map[*Bar]*Bar | |
| 52 | output io.Writer | |
| 53 | debugOut io.Writer | |
| 54 | uwg *sync.WaitGroup | |
| 42 | refreshRate time.Duration | |
| 43 | idCount int | |
| 44 | reqWidth int | |
| 45 | popPriority int | |
| 46 | popCompleted bool | |
| 47 | manualRefresh bool | |
| 48 | forceAutoRefresh bool | |
| 49 | renderDelay <-chan struct{} | |
| 50 | shutdownNotifier chan<- interface{} | |
| 51 | queueBars map[*Bar]*Bar | |
| 52 | output io.Writer | |
| 53 | debugOut io.Writer | |
| 54 | uwg *sync.WaitGroup | |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | // New creates new Progress container instance. It's not possible to |
| 70 | 70 | hm: make(heapManager), |
| 71 | 71 | dropS: make(chan struct{}), |
| 72 | 72 | dropD: make(chan struct{}), |
| 73 | refreshCh: make(chan time.Time), | |
| 73 | 74 | rows: make([]io.Reader, 32), |
| 74 | 75 | refreshRate: defaultRefreshRate, |
| 75 | 76 | popPriority: math.MinInt32, |
| 82 | 83 | if opt != nil { |
| 83 | 84 | opt(s) |
| 84 | 85 | } |
| 85 | } | |
| 86 | ||
| 87 | if s.manualRefresh == nil { | |
| 88 | s.manualRefresh = make(chan interface{}) | |
| 89 | 86 | } |
| 90 | 87 | |
| 91 | 88 | p := &Progress{ |
| 273 | 270 | ch := make(chan time.Time, 1) |
| 274 | 271 | go func() { |
| 275 | 272 | var autoRefresh <-chan time.Time |
| 276 | if (isTerminal || s.forceAutoRefresh) && !s.disableAutoRefresh { | |
| 273 | if (isTerminal || s.forceAutoRefresh) && !s.manualRefresh { | |
| 277 | 274 | if s.renderDelay != nil { |
| 278 | 275 | <-s.renderDelay |
| 279 | 276 | } |
| 285 | 282 | select { |
| 286 | 283 | case t := <-autoRefresh: |
| 287 | 284 | ch <- t |
| 288 | case x := <-s.manualRefresh: | |
| 289 | if t, ok := x.(time.Time); ok { | |
| 290 | ch <- t | |
| 291 | } else { | |
| 292 | ch <- time.Now() | |
| 293 | } | |
| 285 | case t := <-s.refreshCh: | |
| 286 | ch <- t | |
| 294 | 287 | case <-s.ctx.Done(): |
| 295 | 288 | close(done) |
| 296 | 289 | return |
| 400 | 393 | |
| 401 | 394 | func (s *pState) makeBarState(total int64, filler BarFiller, options ...BarOption) *bState { |
| 402 | 395 | bs := &bState{ |
| 403 | id: s.idCount, | |
| 404 | priority: s.idCount, | |
| 405 | reqWidth: s.reqWidth, | |
| 406 | total: total, | |
| 407 | filler: filler, | |
| 408 | manualRefresh: s.manualRefresh, | |
| 396 | id: s.idCount, | |
| 397 | priority: s.idCount, | |
| 398 | reqWidth: s.reqWidth, | |
| 399 | total: total, | |
| 400 | filler: filler, | |
| 401 | refreshCh: s.refreshCh, | |
| 409 | 402 | } |
| 410 | 403 | |
| 411 | 404 | if total > 0 { |