reader.go 400 B

12345678910111213141516171819202122232425
  1. package pb
  2. import (
  3. "io"
  4. )
  5. // It's proxy reader, implement io.Reader
  6. type Reader struct {
  7. io.Reader
  8. bar *ProgressBar
  9. }
  10. func (r *Reader) Read(p []byte) (n int, err error) {
  11. n, err = r.Reader.Read(p)
  12. r.bar.Add(n)
  13. return
  14. }
  15. // Close the reader when it implements io.Closer
  16. func (r *Reader) Close() (err error) {
  17. if closer, ok := r.Reader.(io.Closer); ok {
  18. return closer.Close()
  19. }
  20. return
  21. }