forked from asahasrabuddhe/progress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress.go
87 lines (68 loc) · 1.61 KB
/
progress.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package progress // import go.ajitem.com/progress
import (
"io"
)
type ProgressFunc func(n int)
type Writer struct {
writer io.Writer
progress ProgressFunc
}
func NewWriter(writer io.Writer, progress ProgressFunc) *Writer {
return &Writer{writer: writer, progress: progress}
}
func (w Writer) Write(p []byte) (int, error) {
n, err := w.writer.Write(p)
w.progress(n)
return n, err
}
type WriteCloser struct {
closer io.WriteCloser
progress ProgressFunc
}
func NewWriteCloser(closer io.WriteCloser, progress ProgressFunc) *WriteCloser {
return &WriteCloser{closer: closer, progress: progress}
}
func (w WriteCloser) Write(p []byte) (int, error) {
n, err := w.closer.Write(p)
w.progress(n)
return n, err
}
func (w WriteCloser) Close() error {
return w.closer.Close()
}
type Reader struct {
reader io.Reader
progress ProgressFunc
}
func NewReader(reader io.Reader, progress ProgressFunc) *Reader {
return &Reader{reader: reader, progress: progress}
}
func (r Reader) Read(p []byte) (int, error) {
n, err := r.reader.Read(p)
r.progress(n)
return n, err
}
type ReadCloser struct {
closer io.ReadCloser
progress ProgressFunc
}
func NewReadCloser(closer io.ReadCloser, progress ProgressFunc) *ReadCloser {
return &ReadCloser{closer: closer, progress: progress}
}
func (r ReadCloser) Read(p []byte) (int, error) {
n, err := r.closer.Read(p)
r.progress(n)
return n, err
}
func (r ReadCloser) Close() error {
return r.closer.Close()
}
type discardCloser struct {
io.Writer
}
func (dc *discardCloser) Close() error {
return nil
}
func DiscardCloser(w io.Writer) io.WriteCloser {
return &discardCloser{w}
}