summaryrefslogtreecommitdiff
path: root/vendor/github.com/vbauerster/mpb/v4/proxyreader.go
blob: 0e4b51f09b19393c4b6fb10633814b41daff7009 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package mpb

import (
	"io"
	"time"
)

type proxyReader struct {
	io.ReadCloser
	bar *Bar
	iT  time.Time
}

func (prox *proxyReader) Read(p []byte) (n int, err error) {
	n, err = prox.ReadCloser.Read(p)
	if n > 0 {
		prox.bar.IncrBy(n, time.Since(prox.iT))
		prox.iT = time.Now()
	}
	if err == io.EOF {
		go prox.bar.SetTotal(0, true)
	}
	return
}

type proxyWriterTo struct {
	*proxyReader
	wt io.WriterTo
}

func (prox *proxyWriterTo) WriteTo(w io.Writer) (n int64, err error) {
	n, err = prox.wt.WriteTo(w)
	if n > 0 {
		prox.bar.IncrInt64(n, time.Since(prox.iT))
		prox.iT = time.Now()
	}
	if err == io.EOF {
		go prox.bar.SetTotal(0, true)
	}
	return
}