| 1 | package progress |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | type progressAt struct { |
| 10 | when time.Time |
| 11 | offset uint64 |
| 12 | } |
| 13 | |
| 14 | type Printer struct { |
| 15 | // config |
| 16 | out io.Writer |
| 17 | now func() time.Time |
| 18 | |
| 19 | // state |
| 20 | first bool |
| 21 | size uint64 |
| 22 | history [5]progressAt |
| 23 | oldest int // index into history |
| 24 | } |
| 25 | |
| 26 | func NewPrinter(out io.Writer, now func() time.Time) Printer { |
| 27 | p := Printer{ |
| 28 | out: out, |
| 29 | now: now, |
| 30 | } |
| 31 | n := now() |
| 32 | for i := range 5 { |
| 33 | p.history[i] = progressAt{ |
| 34 | when: n, |
| 35 | offset: 0, |
| 36 | } |
| 37 | } |
| 38 | return p |
| 39 | } |
| 40 | |
| 41 | func (p *Printer) Reset(size uint64) { |
| 42 | now := p.now() |
| 43 | p.size = size |
| 44 | p.first = true |
| 45 | for i := range 5 { |
| 46 | p.history[i] = progressAt{ |
| 47 | when: now, |
| 48 | offset: 0, |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func (p *Printer) MaybeShow(offset uint64, last bool) { |
| 54 | newest := p.oldest |
| 55 | if newest == 0 { |
| 56 | newest = 4 |
| 57 | } else { |
| 58 | newest-- |
| 59 | } |
| 60 | now := p.now() |
| 61 | if !last && now.Sub(p.history[newest].when) < 1*time.Second { |
| 62 | return |
| 63 | } |
| 64 | p.Show(offset, last) |
| 65 | } |
| 66 | |
| 67 | func (p *Printer) Show(offset uint64, last bool) { |
| 68 | now := p.now() |
| 69 | newest := p.oldest |
| 70 | p.oldest = (p.oldest + 1) % 5 |
| 71 | p.history[newest] = progressAt{ |
| 72 | when: now, |
| 73 | offset: offset, |
| 74 | } |
| 75 | |
| 76 | pct := int(float64(offset) / float64(p.size) * 100) |
| 77 | |
| 78 | oldestOffset := p.history[p.oldest].offset |
| 79 | diff := now.Sub(p.history[p.oldest].when).Seconds() |
| 80 | if diff == 0 { |
| 81 | diff = 1 |
| 82 | } |
| 83 | rate := float64(offset-oldestOffset) / diff |
| 84 | var remainSec float64 // seconds |
| 85 | if rate > 0 { |
| 86 | remainSec = float64(p.size-offset) / rate |
| 87 | } |
| 88 | |
| 89 | rate /= 1024 |
| 90 | unit := "kB/s" |
| 91 | switch { |
| 92 | case rate > 1024*1024: |
| 93 | rate /= 1024 * 1024 |
| 94 | unit = "GB/s" |
| 95 | case rate > 1024: |
| 96 | rate /= 1024 |
| 97 | unit = "MB/s" |
| 98 | } |
| 99 | |
| 100 | remaining := " ??:??:??" |
| 101 | if remainSec >= 0 && remainSec <= 9999*3600 { |
| 102 | remaining = fmt.Sprintf("%4d:%02d:%02d", |
| 103 | int(remainSec/3600), |
| 104 | int(remainSec/60)%60, |
| 105 | int(remainSec)%60) |
| 106 | } |
| 107 | |
| 108 | if p.first { |
| 109 | p.first = false |
| 110 | } else { |
| 111 | p.out.Write([]byte{'\r'}) |
| 112 | } |
| 113 | fmt.Fprintf(p.out, "%15d %3d%% %7.2f%s %s", offset, pct, rate, unit, remaining) |
| 114 | if last { |
| 115 | // TODO: show where we are within the file list |
| 116 | // (number of files transferred vs. number of files total) |
| 117 | p.out.Write([]byte{'\n'}) |
| 118 | } |
| 119 | } |