package pkg import ( "fmt" "io" "strings" "sync" "time" ) const ( progressWidth = 30 renderInterval = time.Second minRateElapsed = 2 * time.Second ) // ProgressReader counts the bytes read from an underlying reader and renders a // progress bar. The total is an estimate (the sum of the input file sizes); // MPEG-TS overhead means the number of bytes read can exceed it. type ProgressReader struct { r io.Reader total int64 out io.Writer mu sync.Mutex start time.Time read int64 lastPercent int64 lastRender time.Time lastLen int rendered bool } // NewProgressReader wraps r and renders progress to out. A non-positive total // is treated as unknown. func NewProgressReader(r io.Reader, total int64, out io.Writer) *ProgressReader { return &ProgressReader{r: r, total: total, out: out, start: time.Now(), lastPercent: -1} } // Read reads from the underlying reader and updates the progress display. func (p *ProgressReader) Read(b []byte) (int, error) { n, err := p.r.Read(b) p.mu.Lock() p.read += int64(n) p.renderLocked() p.mu.Unlock() return n, err } // Finish completes the progress line. It should be called once no more reads // are expected. func (p *ProgressReader) Finish() { p.mu.Lock() defer p.mu.Unlock() if !p.rendered { return } p.lastPercent = -1 p.renderLocked() _, _ = fmt.Fprintln(p.out) } func (p *ProgressReader) renderLocked() { now := time.Now() read := p.read percent := int64(0) if p.total > 0 { percent = min(read*100/p.total, 100) } if percent == p.lastPercent && now.Sub(p.lastRender) < renderInterval { return } p.lastPercent = percent p.lastRender = now p.rendered = true displayRead := read if p.total > 0 && displayRead > p.total { displayRead = p.total } filled := int(percent) * progressWidth / 100 bar := strings.Repeat("=", filled) + strings.Repeat(" ", progressWidth-filled) var line string if p.total > 0 { line = fmt.Sprintf("[%s] %3d%% %s / %s", bar, percent, formatBytes(displayRead), formatBytes(p.total)) } else { line = fmt.Sprintf("[%s] %s", bar, formatBytes(displayRead)) } elapsed := now.Sub(p.start) if elapsed >= minRateElapsed { rate := float64(read) / elapsed.Seconds() line += fmt.Sprintf(" %s/s", formatBytes(int64(rate))) if p.total > 0 && read < p.total { line += " ETA " + formatETA(p.total-read, rate) } } // Carriage return moves the cursor but does not erase, so pad the line to // the width of the previous one. Otherwise a shorter line (rate and ETA // shrink all the time) leaves stale characters at the end. if pad := p.lastLen - len(line); pad > 0 { line += strings.Repeat(" ", pad) } p.lastLen = len(line) _, _ = fmt.Fprintf(p.out, "\r%s", line) } func formatETA(remaining int64, rate float64) string { if remaining <= 0 || rate <= 0 { return "?" } seconds := float64(remaining) / rate return time.Duration(seconds * float64(time.Second)).Round(time.Second).String() } func formatBytes(n int64) string { const unit = 1024 if n < unit { return fmt.Sprintf("%d B", n) } div, exp := int64(unit), 0 for m := n / unit; m >= unit; m /= unit { div *= unit exp++ } return fmt.Sprintf("%.1f %ciB", float64(n)/float64(div), "KMGTPE"[exp]) }