progressio

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2019 License: MIT Imports: 3 Imported by: 0

README

progressio

Build Status GoDoc

Make progress of io.Reader and io.Writer observable.

Features

  • easy to use: NewProgressReader(...) or NewProgressWriter(...), and get progress reported!
  • progress report while transferring bytes
    • progress in percentage
    • total transferred
    • transfer speed within a duration
  • cancellation
  • user defined report interval

Installation

$ go get -u github.com/kulukyo/progressio

Usage

    // r is a normal io.Reader
    data := "some very looooooooooooooooooong data"
    r := strings.NewReader(data)

    // ticker for progress report
    t := time.NewTicker(100 * time.Millisecond)

    // wrap the normal reader "r" with context, data length
    // and a ticker, returns a wrapped reader and
    // a progress channel to receive progress
    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()
    pr, progress := progressio.NewProgressReader(ctx, r, int64(len(data)), t)

    // consumes progress in another goroutine
    go func() {
       for p := range readProgress {
           fmt.Printf("reading: %v\n", p)
       }
    }()
    
    // use pr as a normal io.Reader,
    // for example in io.Copy, 
    io.Copy(ioutil.Discard, pr)
    // or pr.Read()
    //pr.Read(make([]byte, 20))

For detailed usage, go and check examples/main.go.

Run Tests

$ go test -v

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewProgressReader

func NewProgressReader(ctx context.Context, r io.Reader, total int64, t *time.Ticker) (reader io.Reader, progress <-chan Progress)

NewProgressReader wraps a reader reports progress and speed via channels while reading.

func NewProgressWriter

func NewProgressWriter(ctx context.Context, w io.Writer, total int64, t *time.Ticker) (writer io.Writer, progress <-chan Progress)

NewProgressWriter wraps a writer reports progress and speed via channels while writing.

Types

type Progress

type Progress struct {
	Progress    float32       // progress from 0 ~ 100
	Speed       float32       // bytes per second
	Transferred int64         // total transferred bytes
	Duration    time.Duration // duration since last progress
}

Progress reports progress of transferring.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL