progress

package module
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 5 Imported by: 3

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 NewCountingReader added in v0.3.0

func NewCountingReader(r io.Reader) *CountingReader

NewCountingReader wraps an io.Reader with counters of bytes and lines.

func (*CountingReader) Bytes

func (cr *CountingReader) Bytes() int64

Bytes returns number of processed bytes.

func (*CountingReader) Close added in v0.3.0

func (cr *CountingReader) Close()

func (*CountingReader) Lines

func (cr *CountingReader) Lines() int64

Lines returns number of processed lines.

func (*CountingReader) Read

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

Read reads and counts bytes.

func (*CountingReader) SetBytes added in v0.3.0

func (cr *CountingReader) SetBytes(bytes *int64)

func (*CountingReader) SetLines added in v0.3.0

func (cr *CountingReader) SetLines(lines *int64)

type CountingWriter

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

CountingWriter wraps io.Writer to count bytes.

func NewCountingWriter added in v0.3.0

func NewCountingWriter(w io.Writer) *CountingWriter

NewCountingWriter wraps an io.Writer with counters of bytes and lines.

func (*CountingWriter) Bytes

func (cr *CountingWriter) Bytes() int64

Bytes returns number of processed bytes.

func (*CountingWriter) Close added in v0.3.0

func (cr *CountingWriter) Close()

func (*CountingWriter) Lines

func (cr *CountingWriter) Lines() int64

Lines returns number of processed lines.

func (*CountingWriter) SetBytes added in v0.3.0

func (cr *CountingWriter) SetBytes(bytes *int64)

func (*CountingWriter) SetLines added in v0.3.0

func (cr *CountingWriter) SetLines(lines *int64)

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) Bytes added in v0.2.5

func (p *Progress) Bytes() int64

Bytes returns current number of bytes.

func (*Progress) Lines added in v0.2.1

func (p *Progress) Lines() int64

Lines returns current number of lines.

func (*Progress) Reset added in v0.2.5

func (p *Progress) Reset()

Reset drops continued counters.

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        `json:"task"`
	DonePercent    float64       `json:"done_percent"`
	LinesCompleted int64         `json:"lines_completed"`
	SpeedMBPS      float64       `json:"speed_mbps"`
	SpeedLPS       float64       `json:"speed_lps"`
	Elapsed        time.Duration `json:"-"`
	Remaining      time.Duration `json:"-"`
	Metrics        []Metric      `json:"-"`
}

Status describes current progress.

type Task

type Task struct {
	TotalBytes   func() int64
	CurrentBytes func() int64
	CurrentLines func() int64
	Task         string
	Continue     bool
	PrintOnStart 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
Package main provides catp CLI tool.
Package main provides catp CLI tool.
catp/catp
Package catp provides catp CLI tool as importable package.
Package catp provides catp CLI tool as importable package.

Jump to

Keyboard shortcuts

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