Skip to content

Commit

Permalink
DOC: add ioutil.ProgressWriter comments
Browse files Browse the repository at this point in the history
  • Loading branch information
whoisnian committed Apr 22, 2023
1 parent 8ad5d9f commit 1d761ff
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions util/ioutil/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,30 +31,37 @@ 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)
}
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
close(pw.status)
}
}

// 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)
Expand Down

0 comments on commit 1d761ff

Please sign in to comment.