diff --git a/util/ioutil/progress.go b/util/ioutil/progress.go index d128d1d..4b469bb 100644 --- a/util/ioutil/progress.go +++ b/util/ioutil/progress.go @@ -12,6 +12,7 @@ type ProgressWriter struct { status chan int } +// NewProgressWriter wraps io.Writer with total written size and a status channel. func NewProgressWriter(w io.Writer) *ProgressWriter { return &ProgressWriter{ wr: w, @@ -30,10 +31,13 @@ func (pw *ProgressWriter) sum(n int) { } } +// Size returns total written size of io.Writer. func (pw *ProgressWriter) Size() int { return pw.size } +// Status returns the status channel. +// Total written size will be sent to the status channel without blocking after every write operation. func (pw *ProgressWriter) Status() chan int { if pw.status == nil { pw.status = make(chan int) @@ -41,6 +45,8 @@ func (pw *ProgressWriter) Status() chan int { return pw.status } +// Close sends total written size to the blocking channel and then closes the channel. +// Only the sender should close a channel, never the receiver. func (pw *ProgressWriter) Close() { if pw.status != nil { pw.status <- pw.size @@ -48,12 +54,14 @@ func (pw *ProgressWriter) Close() { } } +// Write implements the standard io.Writer interface. func (pw *ProgressWriter) Write(p []byte) (n int, err error) { n, err = pw.wr.Write(p) pw.sum(n) return n, err } +// WriteString implements the standard io.StringWriter interface. func (pw *ProgressWriter) WriteString(s string) (n int, err error) { if sw, ok := pw.wr.(io.StringWriter); ok { n, err = sw.WriteString(s)