go-zero/core/filex/progressscanner.go

33 lines
631 B
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package filex
import "gopkg.in/cheggaaa/pb.v1"
type (
2021-02-19 14:30:38 +08:00
// A Scanner is used to read lines.
2020-07-26 17:09:05 +08:00
Scanner interface {
2021-02-19 14:30:38 +08:00
// Scan checks if has remaining to read.
2020-07-26 17:09:05 +08:00
Scan() bool
2021-02-19 14:30:38 +08:00
// Text returns next line.
2020-07-26 17:09:05 +08:00
Text() string
}
progressScanner struct {
Scanner
bar *pb.ProgressBar
}
)
2021-02-19 14:30:38 +08:00
// NewProgressScanner returns a Scanner with progress indicator.
2020-07-26 17:09:05 +08:00
func NewProgressScanner(scanner Scanner, bar *pb.ProgressBar) Scanner {
return &progressScanner{
Scanner: scanner,
bar: bar,
}
}
func (ps *progressScanner) Text() string {
s := ps.Scanner.Text()
ps.bar.Add64(int64(len(s)) + 1) // take newlines into account
return s
}