progress

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: MPL-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package progress provides wrappers for io.Writer and io.Reader that report the progress of the read or write operation via a callback.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

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

Reader is an io.ReadSeekCloser that reports the number of bytes read from it via a callback.

The callback is called at most every "updateInterval" bytes. The updateInterval can be set using the Reader.WithUpdateInterval method. The callback will also be called whenever Reader.Seek is called.

The following is an example of how to use Reader to report the progress of reading from a file:

file, _ := os.Open("file.txt")
progressReader := NewReader(f, func(readBytes int) {
	fmt.Printf("Read %d bytes\n", readBytes)
})
io.ReadAll(progressReader)

func NewReader

func NewReader(r io.Reader, progressFn func(readBytes int)) *Reader

func (*Reader) Close

func (r *Reader) Close() error

func (*Reader) Read

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

func (*Reader) Seek added in v0.6.0

func (r *Reader) Seek(offset int64, whence int) (int64, error)

func (*Reader) WithUpdateInterval

func (r *Reader) WithUpdateInterval(bytes int) *Reader

type Writer

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

Writer is an io.Writer that reports the number of bytes written to it via a callback. The callback is called at most every "updateInterval" bytes. The updateInterval can be set using the Writer.WithUpdateInterval method.

The following is an example of how to use Writer to report the progress of writing to a file:

file, _ := os.Create("file.txt")
progressWriter := progress.NewWriter(func(processedBytes int) {
    fmt.Printf("Processed %d bytes\n", processedBytes)
})
writerWithProgress := io.MultiWriter(file, progressWriter)
io.Copy(writerWithProgress, bytes.NewReader(bytes.Repeat([]byte{42}, 1024*1024)))

func NewWriter

func NewWriter(progressFn func(processedBytes int)) *Writer

func (*Writer) WithUpdateInterval

func (w *Writer) WithUpdateInterval(bytes int) *Writer

func (*Writer) Write

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

Jump to

Keyboard shortcuts

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