Hi, I’m @Luo Zhu

This article was first published on luo Zhu’s official website

This article synchronizes in the public account “luo Zhu early teahouse”, reprint please contact the author.

Creation is not easy, form a habit, quality three even!

Go terminal progress bar

The installation

Go get github.com/cheggaaa/pb/v3.Copy the code

The v1 documentation is here

Quick start

package main

import (
	"time"

	"github.com/cheggaaa/pb/v3"
)

func main(a) {
	count := 100000
  // Create and open a new progress bar
	bar := pb.StartNew(count)

  // Enable a progress bar based on the Default template
	// bar := pb.Default.Start(count)

  // Start a progress bar based on the Simple template
	// bar := pb.Simple.Start(count)

  // Start a progress bar based on the Full template
	// bar := pb.Full.Start(count)

	for i := 0; i < count; i++ {
		bar.Increment()
		time.Sleep(time.Millisecond)
	}
	bar.Finish()
}
Copy the code

The result will be something like this.

> go run the test. Go 37158/100000 [= = = = = = = = = = = = = = = = > _______________________________] 37.16% 1 m11sCopy the code

Set up the

// Create a progress bar
bar := pb.New(count)

// Refresh once per second (default: 200ms)
bar.SetRefreshRate(time.Second)

// Force 'IO.Writer', default is' os.stderr '
bar.SetWriter(os.Stdout)

// bar will format numbers as bytes (B, KiB, MiB, etc)
// The progress bar internally converts numbers to bytes (B, KiB, MiB, etc.)
bar.Set(pb.Bytes, true)

// bar use SI bytes prefix names (B, kB) instead of IEC (B, KiB)
// Progress bar uses SI byte prefix (B, KB) instead of IEC (B, KiB)
bar.Set(pb.SIBytesPrefix, true)

// Set a custom progress bar template
bar.SetTemplateString(myTemplate)

// Check that the template is correct
iferr = bar.Err(); err ! =nil {
    return
}

// Start a progress bar
bar.Start()
Copy the code

Progress bar of I/O operations

package main

import (
	"crypto/rand"
	"io"
	"io/ioutil"

	"github.com/cheggaaa/pb/v3"
)

func main(a) {

	var limit int64 = 1024 * 1024 * 500
	// we will copy 200 Mb from /dev/rand to /dev/null
       // We will copy 200 Mb file from '/dev/rand' to '/dev/null/'
	reader := io.LimitReader(rand.Reader, limit)
	writer := ioutil.Discard

	// Open a new progress bar
	bar := pb.Full.Start64(limit)
	// Create proxy Reader
	barReader := bar.NewProxyReader(reader)
  // Copy from proxy Reader
	io.Copy(writer, barReader)
	// Complete the progress bar
	bar.Finish()
}
Copy the code

Customize a progress bar template

Render based on the built-in text/template package. You can use existing petabytes of elements or create your own.

All available elements are described in the element.go file.

All elements in an example.

tmpl := `{{ red "With funcs:" }} {{ bar . "<" "-" (cycle . "↖" "↗" "↘" "↙" ) "." ">"}} {{speed . | rndcolor }} {{percent .}} {{string . "my_green_string" | green}} {{string . "my_blue_string" | blue}}`
// Start a progress bar for pB-based templates
bar := pb.ProgressBarTemplate(tmpl).Start64(limit)
// Set the value for the string element
bar.Set("my_green_string"."green").
	Set("my_blue_string"."blue")
Copy the code