-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprogresswriter.go
More file actions
47 lines (40 loc) · 1.29 KB
/
progresswriter.go
File metadata and controls
47 lines (40 loc) · 1.29 KB
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
package progressio
import "io"
// Copy functionality of ioutil.NopCloser, but for Writers
type nopWriteCloser struct{ io.Writer }
func (nopWriteCloser) Close() error { return nil }
func getNopWriteCloser(w io.Writer) io.WriteCloser {
return nopWriteCloser{w}
}
// ProgressWriter is a struct representing an io.WriterCloser, which sends back progress
// feedback over a channel
type ProgressWriter struct {
w io.WriteCloser
ioProgress
}
// NewProgressWriter creates a new ProgressWriter object based on the io.Writer and the
// size you specified. Specify a size <= 0 if you don't know the size.
func NewProgressWriter(w io.Writer, size int64) (*ProgressWriter, <-chan Progress) {
if w == nil {
return nil, nil
}
wc, ok := w.(io.WriteCloser)
if !ok {
wc = getNopWriteCloser(w)
}
ret := &ProgressWriter{wc, *mkIoProgress(size)}
return ret, ret.ch
}
// Write wraps the io.Writer Write function to also update the progress.
func (p *ProgressWriter) Write(b []byte) (n int, err error) {
n, err = p.w.Write(b[0:])
p.updateProgress(int64(n))
return
}
// Close wraps the io.WriterCloser Close function to clean up everything. ProgressWriter
// objects should always be closed to make sure everything is cleaned up.
func (p *ProgressWriter) Close() (err error) {
err = p.w.Close()
p.stopProgress()
return
}