diff --git a/bar.go b/bar.go index 03e75188..8c645bc9 100644 --- a/bar.go +++ b/bar.go @@ -85,12 +85,20 @@ func newBar(container *Progress, bs *bState) *Bar { } // ProxyReader wraps r with metrics required for progress tracking. -// Panics if r is nil. +// If r is 'unknown total/size' reader it's mandatory to call +// (*Bar).SetTotal(-1, true) method after (Reader).Read returns io.EOF. +// Panics if r is nil. If bar is already completed or aborted, returns +// nil. func (b *Bar) ProxyReader(r io.Reader) io.ReadCloser { if r == nil { panic("expected non nil io.Reader") } - return b.newProxyReader(r) + select { + case <-b.done: + return nil + default: + return b.newProxyReader(r) + } } // ID returs id of the bar. diff --git a/proxyreader.go b/proxyreader.go index 582a55ea..b0dd89d4 100644 --- a/proxyreader.go +++ b/proxyreader.go @@ -14,9 +14,6 @@ type proxyReader struct { func (x proxyReader) Read(p []byte) (int, error) { n, err := x.ReadCloser.Read(p) x.bar.IncrBy(n) - if err == io.EOF { - go x.bar.SetTotal(-1, true) - } return n, err } @@ -28,9 +25,6 @@ type proxyWriterTo struct { func (x proxyWriterTo) WriteTo(w io.Writer) (int64, error) { n, err := x.wt.WriteTo(w) x.bar.IncrInt64(n) - if err == io.EOF { - go x.bar.SetTotal(-1, true) - } return n, err }