PrependETA
Vladimir Bauer
9 years ago
| 105 | 105 | b.currentIncrCh <- n |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | func (b *Bar) PrependFunc(f DecoratorFunc) *Bar { | |
| 109 | b.decoratorCh <- &decorator{decoratorPrepend, f} | |
| 110 | return b | |
| 111 | } | |
| 112 | ||
| 108 | 113 | func (b *Bar) AppendFunc(f DecoratorFunc) *Bar { |
| 109 | 114 | b.decoratorCh <- &decorator{decoratorAppend, f} |
| 115 | return b | |
| 116 | } | |
| 117 | ||
| 118 | func (b *Bar) PrependETA() *Bar { | |
| 119 | b.PrependFunc(func(s *Statistics) string { | |
| 120 | eta := time.Duration(s.Total-s.Completed) * s.TimePerItemEstimate | |
| 121 | return fmt.Sprintf("ETA %-5v", time.Duration(eta.Seconds())*time.Second) | |
| 122 | }) | |
| 110 | 123 | return b |
| 111 | 124 | } |
| 112 | 125 | |
| 180 | 193 | } |
| 181 | 194 | |
| 182 | 195 | // render prepend functions to the left of the bar |
| 183 | // for _, f := range b.prependFuncs { | |
| 184 | // args := []byte(f(b)) | |
| 185 | // args = append(args, ' ') | |
| 186 | // pb = append(args, pb...) | |
| 187 | // } | |
| 196 | for _, f := range prependFuncs { | |
| 197 | args := []byte(f(s)) | |
| 198 | args = append(args, ' ') | |
| 199 | buf = append(args, buf...) | |
| 200 | } | |
| 188 | 201 | return buf |
| 189 | 202 | } |
| 190 | 203 | |
| 193 | 206 | lastItemEstimate := float64(lastBlockTime) / float64(items) |
| 194 | 207 | b.timePerItemEstimate = time.Duration((b.Alpha * lastItemEstimate) + (1-b.Alpha)*float64(b.timePerItemEstimate)) |
| 195 | 208 | } |
| 196 | ||
| 197 | // func nextTimePerItemEstimate(d time.Duration, blockStartTime time.Time, alpha float64, items int) time.Duration { | |
| 198 | // lastBlockTime := time.Since(blockStartTime) | |
| 199 | // lastItemEstimate := float64(lastBlockTime) / float64(items) | |
| 200 | // return time.Duration((alpha * lastItemEstimate) + (1-alpha)*float64(d)) | |
| 201 | // } | |