eta method on Statistics
Vladimir Bauer
9 years ago
| 48 | 48 | type Statistics struct { |
| 49 | 49 | Total, Completed int |
| 50 | 50 | TimePerItemEstimate time.Duration |
| 51 | } | |
| 52 | ||
| 53 | func (s *Statistics) eta() time.Duration { | |
| 54 | return time.Duration(s.Total-s.Completed) * s.TimePerItemEstimate | |
| 51 | 55 | } |
| 52 | 56 | |
| 53 | 57 | // type redrawRequest struct { |
| 182 | 186 | func (b *Bar) PrependETA(padding int) *Bar { |
| 183 | 187 | layout := "ETA%" + strconv.Itoa(padding) + "s" |
| 184 | 188 | b.PrependFunc(func(s *Statistics) string { |
| 185 | eta := time.Duration(s.Total-s.Completed) * s.TimePerItemEstimate | |
| 186 | return fmt.Sprintf(layout, time.Duration(eta.Seconds())*time.Second) | |
| 189 | return fmt.Sprintf(layout, time.Duration(s.eta().Seconds())*time.Second) | |
| 187 | 190 | }) |
| 188 | 191 | return b |
| 189 | 192 | } |
| 190 | 193 | |
| 191 | 194 | func (b *Bar) AppendETA() *Bar { |
| 192 | 195 | b.AppendFunc(func(s *Statistics) string { |
| 193 | eta := time.Duration(s.Total-s.Completed) * s.TimePerItemEstimate | |
| 194 | return fmt.Sprintf("ETA %v", time.Duration(eta.Seconds())*time.Second) | |
| 196 | return fmt.Sprintf("ETA %s", time.Duration(s.eta().Seconds())*time.Second) | |
| 195 | 197 | }) |
| 196 | 198 | return b |
| 197 | 199 | } |