core/iox/writer_progress.go

32 lines
484 B
Go
Raw Permalink Normal View History

2024-01-01 16:55:54 +00:00
package iox
import (
"io"
"time"
)
type WriteProgressFunc func(sofar int64)
type WriteProgress struct {
WriteTo io.Writer
last time.Time
count int64
Progress WriteProgressFunc
}
func (wc *WriteProgress) Done() {
wc.Progress(wc.count)
}
func (wc *WriteProgress) Write(p []byte) (int, error) {
wc.count += int64(len(p))
now := time.Now()
if now.Sub(wc.last) > time.Duration(time.Second*1) {
wc.last = now
wc.Progress(wc.count)
}
return wc.WriteTo.Write(p)
}