Skip to content

Commit

Permalink
Implement io.Seeker in progress.Reader
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jun 12, 2024
1 parent e72a5f4 commit 722763b
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions progress/reader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package progress

import "io"
import (
"fmt"
"io"
)

// Reader is an [io.ReadCloser] that reports the number of bytes read from it
// via a callback. The callback is called at most every "updateInterval" bytes.
Expand Down Expand Up @@ -51,4 +54,19 @@ func (r *Reader) Close() error {
return nil
}

var _ io.ReadCloser = (*Reader)(nil)
func (r *Reader) Seek(offset int64, whence int) (int64, error) {
seeker, ok := r.inner.(io.ReadSeeker)
if !ok {
return 0, fmt.Errorf("progress.Reader: source reader (%T) is not an io.ReadSeeker", r.inner)
}
n, err := seeker.Seek(offset, whence)
if err != nil {
return 0, err
}
r.readBytes = int(n)
r.progressFn(r.readBytes)
r.lastUpdate = r.readBytes
return n, nil
}

var _ io.ReadSeekCloser = (*Reader)(nil)

0 comments on commit 722763b

Please sign in to comment.