progress: github.com/machinebox/progress Index | Files

package progress

import "github.com/machinebox/progress"

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 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)
}

Index

Package Files

progress.go reader.go writer.go

func NewTicker Uses

func NewTicker(ctx context.Context, counter Counter, size int64, d time.Duration) <-chan Progress

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.

type Counter Uses

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
    // Err gets the last error from the Reader or Writer.
    // When the process is finished, this will be io.EOF.
    Err() error
}

Counter counts bytes. Both Reader and Writer are Counter types.

type Progress Uses

type Progress struct {
    // contains filtered or unexported fields
}

Progress represents a moment of progress.

func (Progress) Complete Uses

func (p Progress) Complete() bool

Complete gets whether the operation is complete or not. Always returns false if the Size is unknown (-1).

func (Progress) Estimated Uses

func (p Progress) Estimated() time.Time

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) N Uses

func (p Progress) N() int64

N gets the total number of bytes read or written so far.

func (Progress) Percent Uses

func (p Progress) Percent() float64

Percent calculates the percentage complete.

func (Progress) Remaining Uses

func (p Progress) Remaining() time.Duration

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.

func (Progress) Size Uses

func (p Progress) Size() int64

Size gets the total number of bytes that are expected to be read or written.

type Reader Uses

type Reader struct {
    // contains filtered or unexported fields
}

Reader counts the bytes read through it.

func NewReader Uses

func NewReader(r io.Reader) *Reader

NewReader makes a new Reader that counts the bytes read through it.

func (*Reader) Err Uses

func (r *Reader) Err() error

Err gets the last error from the Reader.

func (*Reader) N Uses

func (r *Reader) N() int64

N gets the number of bytes that have been read so far.

func (*Reader) Read Uses

func (r *Reader) Read(p []byte) (n int, err error)

type Writer Uses

type Writer struct {
    // contains filtered or unexported fields
}

Writer counts the bytes written through it.

func NewWriter Uses

func NewWriter(w io.Writer) *Writer

NewWriter gets a Writer that counts the number of bytes written.

func (*Writer) Err Uses

func (w *Writer) Err() error

Err gets the last error from the Writer.

func (*Writer) N Uses

func (w *Writer) N() int64

N gets the number of bytes that have been written so far.

func (*Writer) Write Uses

func (w *Writer) Write(p []byte) (n int, err error)

Package progress imports 4 packages (graph) and is imported by 7 packages. Updated 2018-01-08. Refresh now. Tools for package owners.