package main
import (
"fmt"
"os"
"time"
)
func (d *Downloader) progress(stop <-chan struct{}) {
t := time.NewTicker(400 * time.Millisecond)
defer t.Stop()
var last int64
lastT := time.Now()
for {
select {
case <-t.C:
done := d.Done()
now := time.Now()
dt := now.Sub(lastT).Seconds()
var speed float64
if dt > 0 {
speed = float64(done-last) / dt
}
last, lastT = done, now
if d.Size > 0 {
pct := float64(done) / float64(d.Size) * 100
eta := "-"
if speed > 0 {
eta = fmtDur(time.Duration(float64(d.Size-done) / speed * float64(time.Second)))
}
fmt.Fprintf(os.Stderr, "\r%5.1f% %s / %s %s/s ETA %s ",
pct, fmtBytes(done), fmtBytes(d.Size), fmtBytes(int64(speed)), eta)
} else {
fmt.Fprintf(os.Stderr, "\r%s %s/s ", fmtBytes(done), fmtBytes(int64(speed)))
}
case <-stop:
fmt.Fprintln(os.Stderr)
return
}
}
}