Documentation
¶
Overview ¶
Package progress provides io.Reader and io.Writer with progress and remaining time estimation.
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 { select { case progress, ok := <-progressChan: if !ok { fmt.Println("\rdownload is completed") return } fmt.Printf("\r%v remaining...", progress.Remaining().Round(time.Second)) } } }() // use the Reader as normal if _, err := io.Copy(dest, r); err != nil { log.Fatalln(err) }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewTicker ¶
NewTicker gets a channel on which ticks of Progress are sent at duration d intervals until the operation is complete at which point the channel is closed. The counter is either a Reader or Writer (or any type that can report its progress). The size is the total number of expected bytes being read or written. If the context cancels the operation, the channel is closed.
Types ¶
type Counter ¶
type Counter interface { // N gets the current count value. // For readers and writers, this is the number of bytes // read or written. // For other contexts, the number may be anything. N() int64 }
Counter counts bytes. Both Reader and Writer are Counter types.
type Progress ¶
type Progress struct {
// contains filtered or unexported fields
}
Progress represents a moment of progress.
func (Progress) Estimated ¶
Estimated gets the time at which the operation is expected to finish. Use Remaining to get a Duration. Estimated().IsZero() is true if no estimate is available.
func (Progress) Remaining ¶
Remaining gets the amount of time until the operation is expected to be finished. Use Estimated to get a fixed completion time. Returns -1 if no estimate is available.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader counts the bytes read through it.