godocs
Vladimir Bauer
7 years ago
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | // Filler interface. |
| 36 | // Bar renders by calling Filler's Fill method. You can literally | |
| 37 | // have any bar kind, by implementing this interface and passing it | |
| 38 | // to the Add method. | |
| 36 | // Bar renders by calling Filler's Fill method. You can literally have | |
| 37 | // any bar kind, by implementing this interface and passing it to the | |
| 38 | // Add method. | |
| 39 | 39 | type Filler interface { |
| 40 | 40 | Fill(w io.Writer, width int, s *decor.Statistics) |
| 41 | 41 | } |
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | // IncrBy increments progress bar by amount of n. |
| 220 | // wdd is optional work duration i.e. time.Since(start), | |
| 221 | // which expected to be provided, if any ewma based decorator is used. | |
| 220 | // wdd is optional work duration i.e. time.Since(start), which expected | |
| 221 | // to be provided, if any ewma based decorator is used. | |
| 222 | 222 | func (b *Bar) IncrBy(n int, wdd ...time.Duration) { |
| 223 | 223 | select { |
| 224 | 224 | case b.operateState <- func(s *bState) { |
| 238 | 238 | // Completed reports whether the bar is in completed state. |
| 239 | 239 | func (b *Bar) Completed() bool { |
| 240 | 240 | // omit select here, because primary usage of the method is for loop |
| 241 | // condition, like for !bar.Completed() {...} | |
| 242 | // so when toComplete=true it is called once (at which time, the bar is still alive), | |
| 243 | // then quits the loop and never suppose to be called afterwards. | |
| 241 | // condition, like for !bar.Completed() {...} so when toComplete=true | |
| 242 | // it is called once (at which time, the bar is still alive), then | |
| 243 | // quits the loop and never suppose to be called afterwards. | |
| 244 | 244 | return <-b.boolCh |
| 245 | 245 | } |
| 246 | 246 | |
| 6 | 6 | ) |
| 7 | 7 | |
| 8 | 8 | // MovingAverage is the interface that computes a moving average over |
| 9 | // a time- series stream of numbers. The average may be over a window | |
| 9 | // a time-series stream of numbers. The average may be over a window | |
| 10 | 10 | // or exponentially decaying. |
| 11 | 11 | type MovingAverage interface { |
| 12 | 12 | Add(float64) |
| 12 | 12 | // behavior of progress pool, if passed to mpb.New(...ProgressOption). |
| 13 | 13 | type ProgressOption func(*pState) |
| 14 | 14 | |
| 15 | // WithWaitGroup provides means to have a single joint point. If | |
| 15 | // WithWaitGroup provides means to have a single joint point. If | |
| 16 | 16 | // *sync.WaitGroup is provided, you can safely call just p.Wait() |
| 17 | // without calling Wait() on provided *sync.WaitGroup. Makes sense | |
| 17 | // without calling Wait() on provided *sync.WaitGroup. Makes sense | |
| 18 | 18 | // when there are more than one bar to render. |
| 19 | 19 | func WithWaitGroup(wg *sync.WaitGroup) ProgressOption { |
| 20 | 20 | return func(s *pState) { |
| 22 | 22 | } |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | // WithWidth overrides default width 80. | |
| 25 | // WithWidth sets container width. Default is 80. Bars inherit this | |
| 26 | // width, as long as no BarWidth is applied. | |
| 26 | 27 | func WithWidth(w int) ProgressOption { |
| 27 | 28 | return func(s *pState) { |
| 28 | 29 | if w >= 0 { |
| 49 | 49 | debugOut io.Writer |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | // New creates new Progress instance, which orchestrates bars rendering process. | |
| 53 | // Accepts mpb.ProgressOption funcs for customization. | |
| 52 | // New creates new Progress instance, which orchestrates bars rendering | |
| 53 | // process. Accepts mpb.ProgressOption funcs for customization. | |
| 54 | 54 | func New(options ...ProgressOption) *Progress { |
| 55 | 55 | pq := make(priorityQueue, 0) |
| 56 | 56 | heap.Init(&pq) |
| 119 | 119 | } |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | // Abort is only effective while bar progress is running, | |
| 123 | // it means remove bar now without waiting for its completion. | |
| 124 | // If bar is already completed, there is nothing to abort. | |
| 125 | // If you need to remove bar after completion, use BarRemoveOnComplete BarOption. | |
| 122 | // Abort is only effective while bar progress is running, it means | |
| 123 | // remove bar now without waiting for its completion. If bar is already | |
| 124 | // completed, there is nothing to abort. If you need to remove bar | |
| 125 | // after completion, use BarRemoveOnComplete BarOption. | |
| 126 | 126 | func (p *Progress) Abort(b *Bar, remove bool) { |
| 127 | 127 | select { |
| 128 | 128 | case p.operateState <- func(s *pState) { |
| 158 | 158 | } |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | // Wait first waits for user provided *sync.WaitGroup, if any, | |
| 162 | // then waits far all bars to complete and finally shutdowns master goroutine. | |
| 163 | // After this method has been called, there is no way to reuse *Progress instance. | |
| 161 | // Wait first waits for user provided *sync.WaitGroup, if any, then | |
| 162 | // waits far all bars to complete and finally shutdowns master goroutine. | |
| 163 | // After this method has been called, there is no way to reuse *Progress | |
| 164 | // instance. | |
| 164 | 165 | func (p *Progress) Wait() { |
| 165 | 166 | if p.uwg != nil { |
| 166 | 167 | p.uwg.Wait() |
| 218 | 219 | defer func() { |
| 219 | 220 | if frameReader.toShutdown { |
| 220 | 221 | // shutdown at next flush, in other words decrement underlying WaitGroup |
| 221 | // only after the bar with completed state has been flushed. | |
| 222 | // this ensures no bar ends up with less than 100% rendered. | |
| 222 | // only after the bar with completed state has been flushed. this | |
| 223 | // ensures no bar ends up with less than 100% rendered. | |
| 223 | 224 | s.shutdownPending = append(s.shutdownPending, bar) |
| 224 | 225 | if replacementBar, ok := s.waitBars[bar]; ok { |
| 225 | 226 | heap.Push(s.bHeap, replacementBar) |