progressscanner.go 631 B

1234567891011121314151617181920212223242526272829303132
  1. package filex
  2. import "gopkg.in/cheggaaa/pb.v1"
  3. type (
  4. // A Scanner is used to read lines.
  5. Scanner interface {
  6. // Scan checks if has remaining to read.
  7. Scan() bool
  8. // Text returns next line.
  9. Text() string
  10. }
  11. progressScanner struct {
  12. Scanner
  13. bar *pb.ProgressBar
  14. }
  15. )
  16. // NewProgressScanner returns a Scanner with progress indicator.
  17. func NewProgressScanner(scanner Scanner, bar *pb.ProgressBar) Scanner {
  18. return &progressScanner{
  19. Scanner: scanner,
  20. bar: bar,
  21. }
  22. }
  23. func (ps *progressScanner) Text() string {
  24. s := ps.Scanner.Text()
  25. ps.bar.Add64(int64(len(s)) + 1) // take newlines into account
  26. return s
  27. }