progress

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 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

Overview

Package progress provides helpers to print progress status.

Index

Constants

View Source
const (
	Bytes    = Type("bytes")
	Duration = Type("duration")
	Gauge    = Type("gauge")
)

Type values.

Variables

This section is empty.

Functions

func DefaultStatus

func DefaultStatus(s Status) string

DefaultStatus renders Status as a string.

func MetricsStatus

func MetricsStatus(s Status) string

MetricsStatus renders Status 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 Metric added in v0.2.0

type Metric struct {
	Name  string
	Type  Type
	Value func() int64
}

Metric is an operation metric.

type MetricsExposer

type MetricsExposer interface {
	Metrics() []Metric
}

MetricsExposer provides metric counters.

type Progress

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

Progress reports reading performance.

func (*Progress) AddMetrics

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

AddMetrics adds more metrics to progress status message.

func (*Progress) Lines added in v0.2.1

func (p *Progress) Lines() int64

Lines returns current number of lines.

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 Status added in v0.2.0

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

Status describes current progress.

type Task

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

Task describes a long-running process.

type Type added in v0.2.0

type Type string

Type describes metric value.

Directories

Path Synopsis
cmd module
catp command
Package main provides catp CLI tool.
Package main provides catp CLI tool.

Jump to

Keyboard shortcuts

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