Codebase list golang-github-vbauerster-mpb / 6ce2373
proxyreader for io operations Vladimir Bauer 9 years ago
2 changed file(s) with 28 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
11
22 import (
33 "fmt"
4 "io"
45 "strconv"
56 "sync"
67 "time"
124125 func (b *Bar) SetEtaAlpha(a float64) *Bar {
125126 b.alpha = a
126127 return b
128 }
129
130 func (b *Bar) ProxyReader(r io.Reader) *Reader {
131 return &Reader{r, b}
127132 }
128133
129134 // String returns the string representation of the bar
0 package mpb
1
2 import "io"
3
4 // Reader is io.Reader wrapper, for proxy read bytes
5 type Reader struct {
6 io.Reader
7 bar *Bar
8 }
9
10 func (r *Reader) Read(p []byte) (int, error) {
11 n, err := r.Reader.Read(p)
12 r.bar.Incr(n)
13 return n, err
14 }
15
16 // Close the reader when it implements io.Closer
17 func (r *Reader) Close() error {
18 if closer, ok := r.Reader.(io.Closer); ok {
19 return closer.Close()
20 }
21 return nil
22 }