progress

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: May 12, 2023 License: MIT Imports: 5 Imported by: 4

README

progress

Build Status Coverage Status GoDevDoc Time Tracker Code lines Comments

This library instruments io.Reader/io.Writer with progress printer.

Usage

See catp for an example.

var pr = &progress.Progress{
	Interval: 5 * time.Second,
	Print: func(status progress.ProgressStatus) {
		println(Status(status))
	},
}

func cat(filename string) {
	file, err := os.Open(filename)
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()

	st, err := file.Stat()
	if err != nil {
		log.Fatalf("failed to read file stats %s: %s", filename, err)
	}

	cr := &progress.CountingReader{Reader: file}

	pr.Start(st.Size, cr.Bytes, cr.Lines, filename)
	defer pr.Stop()

	readFile(cr)
}

Documentation

Index

Constants

View Source
const (
	ProgressBytes    = ProgressType("bytes")
	ProgressDuration = ProgressType("duration")
	ProgressGauge    = ProgressType("gauge")
)

ProgressType values.

Variables

This section is empty.

Functions

func DefaultStatus

func DefaultStatus(s ProgressStatus) string

DefaultStatus renders ProgressStatus as a string.

func MetricsStatus

func MetricsStatus(s ProgressStatus) string

MetricsStatus renders ProgressStatus metrics as a string.

Types

type CountingReader

type CountingReader struct {
	Reader io.Reader
	// contains filtered or unexported fields
}

CountingReader wraps io.Reader to count bytes.

func (*CountingReader) Bytes

func (cr *CountingReader) Bytes() int64

Bytes returns number of read bytes.

func (*CountingReader) Lines

func (cr *CountingReader) Lines() int64

Lines returns number of read lines.

func (*CountingReader) Read

func (cr *CountingReader) Read(p []byte) (n int, err error)

Read reads and counts bytes.

type CountingWriter

type CountingWriter struct {
	Writer io.Writer
	// contains filtered or unexported fields
}

CountingWriter wraps io.Writer to count bytes.

func (*CountingWriter) Bytes

func (cr *CountingWriter) Bytes() int64

Bytes returns number of written bytes.

func (*CountingWriter) Lines

func (cr *CountingWriter) Lines() int64

Lines returns number of written bytes.

func (*CountingWriter) Write

func (cr *CountingWriter) Write(p []byte) (n int, err error)

Write writes and counts bytes.

type MetricsExposer

type MetricsExposer interface {
	Metrics() []ProgressMetric
}

MetricsExposer provides metric counters.

type Progress

type Progress struct {
	Interval       time.Duration
	Print          func(status ProgressStatus)
	ShowHeapStats  bool
	ShowLinesStats bool
	// contains filtered or unexported fields
}

Progress reports reading performance.

func (*Progress) AddMetrics

func (p *Progress) AddMetrics(metrics ...ProgressMetric)

AddMetrics adds more metrics to progress status message.

func (*Progress) Start

func (p *Progress) Start(options ...func(t *Task))

Start spawns background progress reporter.

func (*Progress) Stop

func (p *Progress) Stop()

Stop stops progress reporting.

type ProgressMetric

type ProgressMetric struct {
	Name  string
	Type  ProgressType
	Value *int64
}

ProgressMetric is an operation metric.

type ProgressStatus

type ProgressStatus struct {
	Task           string
	DonePercent    float64
	LinesCompleted int64
	SpeedMBPS      float64
	SpeedLPS       float64
	Elapsed        time.Duration
	Remaining      time.Duration
	Metrics        []ProgressMetric
}

ProgressStatus describes current progress.

type ProgressType

type ProgressType string

ProgressType describes metric value.

type Task

type Task struct {
	TotalBytes   func() int64
	CurrentBytes func() int64
	CurrentLines func() int64
	Task         string
	Continue     bool
}

Directories

Path Synopsis
cmd module

Jump to

Keyboard shortcuts

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