minprogress

package module
v0.0.0-...-ec3aa76 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2016 License: MIT Imports: 6 Imported by: 0

README

minprogress

minprogress

A progress bar for Go that can also track the speed of progress.

Example of it used in conjuction with minterm. GIF of progress bar using minterm.

Usage

###Import

import "github.com/MinoMino/minprogress"

###Simple Use Make 5 units of progress every second while printing the bar every second:

const total = 50
pb := minprogress.NewProgressBar(total)
tick := time.Tick(time.Millisecond * 200)
go func() {
  for {
    <-tick
    pb.Progress(1)
  }
}()

for {
  if pb.Progress(0) == total {
    fmt.Println("Done!")
    break
  } else {
    fmt.Println(pb.String())
    time.Sleep(time.Second)
  }
}

Output:

[...]
   60% █████████████████░░░░░░░░░░░░ (30/50)
   70% ████████████████████░░░░░░░░░ (35/50)
   80% ███████████████████████░░░░░░ (40/50)
   90% ██████████████████████████░░░ (45/50)
Done!

###Advanced Use Simulate concurrent file downloads and track download speed:

const total = 50
const workerCount = 10
pb := minprogress.NewProgressBar(total)
pb.SpeedUnits = minprogress.DataUnits
// For nicer output, tell it the singular and plural name of the units.
pb.Unit = "file"
pb.Units = "files"

go func() {
  // Keep only workerCount number of goroutines working concurrently.
  workerQueue := make(chan struct{}, workerCount)
  for i := 0; i < total; i++ {
    workerQueue <- struct{}{}
    go func(uid int) {
      // 200KiB - 1 MiB
      size := rand.Intn(800000) + 200000
      downloaded := 0
      for downloaded < size {
        // Download 1 - 10 KiB every 100 - 200 ms.
        time.Sleep(time.Millisecond*time.Duration(rand.Intn(100)) + 100)
        got := rand.Intn(9000) + 1000
        // Use worker number as UID.
        pb.Report(uid, got)
        downloaded += got
      }
      // Report we're done with this particular download.
      pb.Done(uid)
      // Since one download doesn't necessarily mean one unit of progress,
      // we must also tell the progress bar we made some progress.
      pb.Progress(1)
      // Tell queue we're done.
      <-workerQueue
    }(i)
  }
}()

for {
  if pb.Progress(0) == total {
    fmt.Println("Done!")
    break
  } else {
    fmt.Println(pb.String())
    time.Sleep(time.Second)
  }
}

Output:

[...]
   82% ████████████████████████░░░░░ (41/50) files [3.4 MiB/s]
   88% ██████████████████████████░░░ (44/50) files [1.9 MiB/s]
   92% ███████████████████████████░░ (46/50) files [1.3 MiB/s]
   94% ███████████████████████████░░ (47/50) files [730.5 KiB/s]
   94% ███████████████████████████░░ (47/50) files [704.7 KiB/s]
   96% ████████████████████████████░ (48/50) files [764.3 KiB/s]
   98% ████████████████████████████░ (49/50) files [356.9 KiB/s]
   98% ████████████████████████████░ (49/50) files [259.5 KiB/s]
Done!

License

MIT. See LICENSE for details.

Documentation

Overview

A progress bar for arbitrary units, which can also keep track of the speed of progress in terms of units/second. Safe for concurrent use.

To keep track of speeds, have each "progress maker" report how many units of progress it has done at desired intervals using Report() with a unique identifier. If such a progress maker has finished its task, have it report it with Done(). For every X amount of reports, it will sample the average speed of all the progress makers and include it as part of the formatted progress bar string.

Index

Constants

View Source
const (
	AutoWidth    = 0
	UnknownTotal = 0
)

Variables

View Source
var DataUnits = []Unit{
	Unit{1000000000000, "TiB"},
	Unit{1000000000, "GiB"},
	Unit{1000000, "MiB"},
	Unit{1000, "KiB"},
	Unit{1, "B"},
}

Data speed units. Useful for transfer speeds.

Functions

This section is empty.

Types

type ProgressBar

type ProgressBar struct {
	// The characters used for the empty and full parts of the bar itself.
	// Uses ░ and █ by default, respectively.
	Empty, Full rune
	// If desired, they can be set to words describing what a unit of progress
	// means. For instance "File" and "Files" if the bar represents how many files
	// have been processed.
	Unit, Units string
	// The speed units used for reporting speed. See DataUnits for an example
	// of units for data transfer/processing.
	SpeedUnits []Unit
	// The width of the console and the padding on the left of the bar.
	// The width is AutoWidth by default, which will automatically determine
	// the appropriate size depending on the terminal size.
	Width, Padding int
	// Number of reports that should be stored to calculate the average.
	// The higher it is, the less volatile it is. Default is 50.
	ReportCount, OverallReportCount int
	// How many calls to Report() it should take for it to sample all
	// speed and calculate an overall average speed. Default is 15.
	ReportsPerSample int
	// contains filtered or unexported fields
}

func NewProgressBar

func NewProgressBar(total int) *ProgressBar

Creates a new progress bar starting at 0 units. If total is set to UnknownTotal, no bar will be displayed, but it will still display the number of units of progress that have been made and whatnot.

func (*ProgressBar) AverageOverallSpeed

func (p *ProgressBar) AverageOverallSpeed() (avg float64)

Returns the average cumulative speed and the number of UID used to calculate it.

func (*ProgressBar) AverageSpeed

func (p *ProgressBar) AverageSpeed(uid int) (float64, error)

Returns the average speed of a particular UID. Returns an error if and only if the UID doesn't exist.

func (*ProgressBar) Done

func (p *ProgressBar) Done(uid int)

Report that the progress of a UID is done. This is important to call to keep an accurate overall average.

func (*ProgressBar) Progress

func (p *ProgressBar) Progress(n int) int

Make n amount of units in progress.

func (*ProgressBar) Report

func (p *ProgressBar) Report(uid, n int)

Report how many units of progress have been made since last call for that particular UID.

func (*ProgressBar) String

func (p *ProgressBar) String() string

Gets the whole formatted progress bar.

type SpeedInfo

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

Holds info about the speed of progress. Provides methods to get info and to report progress. Can be used uninitialized, using the default number of 50 reports.

func (*SpeedInfo) Average

func (s *SpeedInfo) Average() float64

Get the average speed.

func (*SpeedInfo) Report

func (s *SpeedInfo) Report(n int)

Report n amount of progress made since last call.

type Unit

type Unit struct {
	Size int64
	Name string
}

Jump to

Keyboard shortcuts

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