Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

pb

> 编程语言
Open source

Console progress bar for Golang

3.7K stars0 likes0 views
WebsiteGitHub

About

Console progress bar for Golang

Terminal progress bar for Go

Installation

go get github.com/cheggaaa/pb/v3

Documentation for v1 bar available here.

Quick start

package main

import (
	"time"

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

func main() {
	count := 100000

	// create and start new bar
	bar := pb.StartNew(count)

	// start bar from 'default' template
	// bar := pb.Default.Start(count)

	// start bar from 'simple' template
	// bar := pb.Simple.Start(count)

	// start bar from 'full' template
	// bar := pb.Full.Start(count)

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

	// finish bar
	bar.Finish()
}

Result will be like this:

> go run test.go
37158 / 100000 [---------------->_______________________________] 37.16% 916 p/s

Settings

// create bar
bar := pb.New(count)

// refresh info every second (default 200ms)
bar.SetRefreshRate(time.Second)

// force set io.Writer, by default it's os.Stderr
bar.SetWriter(os.Stdout)

// bar will format numbers as bytes (B, KiB, MiB, etc)
bar.Set(pb.Bytes, true)

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

// set custom bar template
bar.SetTemplateString(myTemplate)

// check for error after template set
if err := bar.Err(); err != nil {
    return
}

// start bar
bar.Start()

Progress bar for IO Operations

package main

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

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

func main() {
	var limit int64 = 1024 * 1024 * 500

	// we will copy 500 MiB from /dev/rand to /dev/null
	reader := io.LimitReader(rand.Reader, limit)
	writer := ioutil.Discard

	// start new bar
	bar := pb.Full.Start64(limit)

	// create proxy reader
	barReader := bar.NewProxyReader(reader)

	// copy from proxy reader
	io.Copy(writer, barReader)

	// finish bar
	bar.Finish()
}

Custom Progress Bar templates

Rendering based on builtin text/template package. You can use existing pb's elements or create you own.

All available elements are described in the element.go file. The bar element accepts five standard parts: left border, fill, current, empty, and right border. It also accepts optional sixth and seventh parts for the empty left border and finished right border.

Set UNICODE_PROGRESS_BAR=true to use the built-in Unicode progress bar glyphs for bars that use the default bar elements when the active terminal font supports them.

All in one example:

tmpl := `{{ red "With funcs:" }} {{ bar . "<" "-" (cycle . "↖" "↗" "↘" "↙" ) "." ">"}} {{speed . | rndcolor }} {{percent .}} {{string . "my_green_string" | green}} {{string . "my_blue_string" | blue}}`

// start bar based on our template
bar := pb.ProgressBarTemplate(tmpl).Start64(limit)

// set values for string elements
bar.Set("my_green_string", "green").Set("my_blue_string", "blue")

GitHub Issues· 24 open

View all on GitHub
  • #110

    How to print text when bar is running?

    Updated Oct 30, 2024
  • #223

    `CleanOnFinish` doesn't work as intended in pool

    Updated Sep 28, 2024
  • #219

    How to display a progress w/ speed that slower than one item per second?

    Updated Feb 27, 2024
  • #193

    [Question]: How to use more colors?

    Updated Feb 1, 2023
  • #195

    possibility to update ProgressBar from worker pool

    Updated Feb 1, 2023
  • #196

    [QUESTION] How to get multiple/single progressbars for concurrent processes ?

    Updated Jan 31, 2023
  • #194

    Incorrect values of downloaded and percentage

    Updated Jul 10, 2022
  • #164

    [Question] How can i use speed about MiB/s and not p/s

    Updated Apr 15, 2021
  • #173

    feature req: ProxyReadSeeker

    Updated Dec 15, 2020
  • #171

    Progress bars rendering is disturbed when terminal screen size is small

    Updated Nov 7, 2020

Highlights

  • •Go
  • •go
  • •progress-bar
  • •terminal

> Tags

Gogoprogress-barterminal

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言