Convert Figma logo to code with AI

cheggaaa logopb

Console progress bar for Golang

3,634
270
3,634
20

Top Related Projects

A really basic thread-safe progress bar for Golang applications

2,290

multi progress bar for Go cli applications

A go library to render progress bars in terminal applications

Quick Overview

cheggaaa/pb is a Go library for creating progress bars in terminal applications. It provides a simple and flexible way to display progress for long-running tasks, file downloads, or any other process that needs visual feedback.

Pros

  • Easy to use with a clean API
  • Supports customizable styles and formats
  • Offers concurrent-safe progress bars
  • Includes support for IO operations like file downloads

Cons

  • Limited to terminal-based applications
  • May not be suitable for complex GUI applications
  • Documentation could be more comprehensive
  • Some advanced features might require additional configuration

Code Examples

Basic progress bar:

bar := pb.New(100)
bar.Start()
for i := 0; i < 100; i++ {
    bar.Increment()
    time.Sleep(time.Millisecond * 10)
}
bar.Finish()

Progress bar with custom template:

tmpl := `{{ red "Custom progress:" }} {{ bar . "[" "=" (cycle . "↖" "↗" "↘" "↙" ) "." "]"}} {{percent . }}`
bar := pb.New(100).SetTemplate(tmpl)
bar.Start()
for i := 0; i < 100; i++ {
    bar.Increment()
    time.Sleep(time.Millisecond * 10)
}
bar.Finish()

Progress bar for file copy:

reader, _ := os.Open("source.file")
writer, _ := os.Create("destination.file")
defer reader.Close()
defer writer.Close()

bar := pb.Full.Start64(fileInfo.Size())
barReader := bar.NewProxyReader(reader)
io.Copy(writer, barReader)
bar.Finish()

Getting Started

To use cheggaaa/pb in your Go project, follow these steps:

  1. Install the library:

    go get github.com/cheggaaa/pb/v3
    
  2. Import the library in your Go code:

    import "github.com/cheggaaa/pb/v3"
    
  3. Create and use a progress bar:

    bar := pb.New(100)
    bar.Start()
    for i := 0; i < 100; i++ {
        bar.Increment()
        // Your task here
    }
    bar.Finish()
    

This will create a basic progress bar that increments from 0 to 100. Customize the bar's appearance and behavior using the various methods provided by the library.

Competitor Comparisons

A really basic thread-safe progress bar for Golang applications

Pros of progressbar

  • More customizable appearance with support for themes and colors
  • Built-in spinners and automatic adaptive width
  • Simpler API with fewer methods for basic use cases

Cons of progressbar

  • Less feature-rich compared to pb (e.g., no multi-bar support)
  • Fewer options for fine-grained control over bar behavior
  • Less mature project with potentially fewer community contributions

Code Comparison

progressbar:

bar := progressbar.Default(100)
for i := 0; i < 100; i++ {
    bar.Add(1)
    time.Sleep(40 * time.Millisecond)
}

pb:

bar := pb.New(100)
bar.Start()
for i := 0; i < 100; i++ {
    bar.Increment()
    time.Sleep(40 * time.Millisecond)
}
bar.Finish()

Both libraries offer straightforward ways to create and update progress bars. progressbar has a slightly more concise API, while pb requires explicit start and finish calls. progressbar automatically handles width adaptation and provides a more modern look out of the box, whereas pb offers more flexibility for advanced use cases and multi-bar scenarios.

2,290

multi progress bar for Go cli applications

Pros of mpb

  • Supports multiple progress bars concurrently
  • Offers more customization options and decorators
  • Provides better support for dynamic terminal width

Cons of mpb

  • Slightly more complex API compared to pb
  • May have a steeper learning curve for beginners
  • Larger codebase and potentially higher memory usage

Code Comparison

pb:

bar := pb.New(100)
bar.Start()
for i := 0; i < 100; i++ {
    bar.Increment()
    time.Sleep(time.Millisecond * 10)
}
bar.Finish()

mpb:

p := mpb.New(mpb.WithWidth(64))
bar := p.AddBar(100)
for i := 0; i < 100; i++ {
    bar.Increment()
    time.Sleep(time.Millisecond * 10)
}
p.Wait()

Both libraries provide simple ways to create and update progress bars, but mpb offers more flexibility with its concurrent bar support and customization options. pb has a slightly simpler API, making it easier for beginners to get started. However, mpb's additional features make it more suitable for complex progress tracking scenarios, especially when dealing with multiple concurrent operations or requiring advanced customization.

A go library to render progress bars in terminal applications

Pros of uiprogress

  • More customizable and flexible UI options
  • Supports multiple progress bars simultaneously
  • Includes additional features like spinners and decorators

Cons of uiprogress

  • Less actively maintained (last update in 2019)
  • Slightly more complex API for basic use cases
  • Fewer stars and contributors compared to pb

Code Comparison

pb:

bar := pb.New(100)
bar.Start()
for i := 0; i < 100; i++ {
    bar.Increment()
    time.Sleep(time.Millisecond * 10)
}
bar.Finish()

uiprogress:

uiprogress.Start()
bar := uiprogress.AddBar(100)
for i := 0; i <= 100; i++ {
    bar.Incr()
    time.Sleep(time.Millisecond * 20)
}

Both libraries offer simple ways to create and update progress bars. pb has a more straightforward API for basic use cases, while uiprogress provides more flexibility and options for customization. pb is generally more popular and actively maintained, making it a safer choice for long-term projects. However, uiprogress offers more advanced features like multiple progress bars and decorators, which may be beneficial for complex CLI applications.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Terminal progress bar for Go

Coverage Status

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.

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")