progress

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2023 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package progress provides Reader and Writer

Index

Examples

Constants

View Source
const DefaultFlushInterval = time.Second / 30

DefaultFlushInterval is a reasonable default flush interval

Variables

This section is empty.

Functions

This section is empty.

Types

type Progress

type Progress struct {
	Rewritable
}

func (*Progress) Set

func (progress *Progress) Set(prefix string, count, total int)

type Reader

type Reader struct {
	io.Reader // Reader to read from
	Rewritable
	Bytes int64 // total number of bytes read (so far)
}

Reader consistently writes the number of bytes read to Progress.

Example
source := strings.NewReader("hello world")
var progress strings.Builder

reader := &Reader{
	Reader: source,

	Rewritable: Rewritable{
		FlushInterval: 0,
		Writer:        &progress,
	},
}
reader.Read([]byte("hello"))
reader.Read([]byte(" world"))

// replace all the '\r's with '\n's for testing
fmt.Println(strings.ReplaceAll(progress.String(), "\r", "\n"))
Output:

Read 5 B
Read 11 B

func (*Reader) Read

func (cr *Reader) Read(bytes []byte) (int, error)

type Rewritable

type Rewritable struct {
	Writer io.Writer

	FlushInterval time.Duration // minimum time between flushes of the progress
	// contains filtered or unexported fields
}

func (*Rewritable) Close

func (rw *Rewritable) Close()

Close resets any output written to the terminal. After a call to Close(), further calls to Set may re-use it.

func (*Rewritable) Flush

func (rw *Rewritable) Flush(force bool)

func (*Rewritable) Write

func (rw *Rewritable) Write(value string)

type Writer

type Writer struct {
	io.Writer // Writer to write to
	Rewritable
	Bytes int64 // Total number of bytes written
}

Writer consistently writes the number of bytes written to Progress.

Example
var progress strings.Builder

writer := &Writer{
	Writer: io.Discard,

	Rewritable: Rewritable{
		FlushInterval: 0,
		Writer:        &progress,
	},
}
writer.Write([]byte("hello"))
writer.Write([]byte(" world"))

// replace all the '\r's with '\n's for testing
fmt.Println(strings.ReplaceAll(progress.String(), "\r", "\n"))
Output:

Wrote 5 B
Wrote 11 B

func (*Writer) Write

func (cw *Writer) Write(bytes []byte) (int, error)

Jump to

Keyboard shortcuts

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