https://github.com/machinebox/progress
io.Reader and io.Writer with progress and remaining time estimation
https://github.com/machinebox/progress
golang io machinebox
Last synced: 6 months ago
JSON representation
io.Reader and io.Writer with progress and remaining time estimation
- Host: GitHub
- URL: https://github.com/machinebox/progress
- Owner: machinebox
- License: apache-2.0
- Created: 2017-12-31T11:36:58.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-10-28T16:54:29.000Z (over 4 years ago)
- Last Synced: 2024-06-18T12:47:29.779Z (11 months ago)
- Topics: golang, io, machinebox
- Language: Go
- Homepage: https://blog.machinebox.io/measuring-the-progress-of-long-running-io-reader-and-io-writer-operations-in-go-ba26b204a507
- Size: 44.9 KB
- Stars: 409
- Watchers: 7
- Forks: 28
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `progress` [](http://godoc.org/github.com/machinebox/progress) [](https://travis-ci.org/machinebox/progress) [](https://goreportcard.com/report/github.com/machinebox/progress)
`io.Reader` and `io.Writer` with progress and remaining time estimation.
## Usage
```go
ctx := context.Background()// get a reader and the total expected number of bytes
s := `Now that's what I call progress`
size := len(s)
r := progress.NewReader(strings.NewReader(s))// Start a goroutine printing progress
go func() {
ctx := context.Background()
progressChan := progress.NewTicker(ctx, r, size, 1*time.Second)
for p := range progressChan {
fmt.Printf("\r%v remaining...", p.Remaining().Round(time.Second))
}
fmt.Println("\rdownload is completed")
}()// use the Reader as normal
if _, err := io.Copy(dest, r); err != nil {
log.Fatalln(err)
}
```1. Wrap an `io.Reader` or `io.Writer` with `NewReader` and `NewWriter` respectively
1. Capture the total number of expected bytes
1. Use `progress.NewTicker` to get a channel on which progress updates will be sent
1. Start a Goroutine to periodically check the progress, and do something with it - like log it
1. Use the readers and writers as normal