proxyreader for io operations
Vladimir Bauer
9 years ago
| 1 | 1 |
|
| 2 | 2 |
import (
|
| 3 | 3 |
"fmt"
|
|
4 |
"io"
|
| 4 | 5 |
"strconv"
|
| 5 | 6 |
"sync"
|
| 6 | 7 |
"time"
|
|
| 124 | 125 |
func (b *Bar) SetEtaAlpha(a float64) *Bar {
|
| 125 | 126 |
b.alpha = a
|
| 126 | 127 |
return b
|
|
128 |
}
|
|
129 |
|
|
130 |
func (b *Bar) ProxyReader(r io.Reader) *Reader {
|
|
131 |
return &Reader{r, b}
|
| 127 | 132 |
}
|
| 128 | 133 |
|
| 129 | 134 |
// 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 |
}
|