Convert Figma logo to code with AI

gosuri logouiprogress

A go library to render progress bars in terminal applications

2,096
126
2,096
26

Top Related Projects

A really basic thread-safe progress bar for Golang applications

3,634

Console progress bar for Golang

2,290

multi progress bar for Go cli applications

2,327

Go (golang) package with 90 configurable terminal spinner/progress indicators.

Quick Overview

gosuri/uiprogress is a Go library for rendering progress bars in terminal applications. It provides a flexible and easy-to-use API for creating and managing multiple progress bars, allowing developers to display real-time progress information for various tasks in their command-line tools.

Pros

  • Simple and intuitive API for creating and updating progress bars
  • Supports multiple concurrent progress bars
  • Customizable appearance and behavior of progress bars
  • Lightweight with minimal dependencies

Cons

  • Limited to terminal-based applications
  • May not work well in all terminal environments or with all terminal emulators
  • Lacks advanced features like colored progress bars or graphical elements

Code Examples

Creating a basic progress bar:

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

Using a custom progress bar format:

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

Creating multiple progress bars:

bar1 := uiprogress.AddBar(100).AppendCompleted()
bar2 := uiprogress.AddBar(100).AppendCompleted()

go func() {
    for i := 0; i <= 100; i++ {
        bar1.Incr()
        time.Sleep(time.Millisecond * 30)
    }
}()

go func() {
    for i := 0; i <= 100; i++ {
        bar2.Incr()
        time.Sleep(time.Millisecond * 50)
    }
}()

uiprogress.Start()
time.Sleep(time.Second * 7)

Getting Started

To use gosuri/uiprogress in your Go project, follow these steps:

  1. Install the library:

    go get -u github.com/gosuri/uiprogress
    
  2. Import the library in your Go code:

    import "github.com/gosuri/uiprogress"
    
  3. Create and use a progress bar:

    uiprogress.Start()
    bar := uiprogress.AddBar(100)
    
    for i := 0; i <= 100; i++ {
        bar.Incr()
        time.Sleep(time.Millisecond * 20)
    }
    
  4. Run your Go program to see the progress bar in action.

Competitor Comparisons

A really basic thread-safe progress bar for Golang applications

Pros of progressbar

  • More customizable appearance with themes and emoji support
  • Supports multiple concurrent progress bars
  • Includes built-in spinners and automatic ETA calculation

Cons of progressbar

  • Slightly more complex API compared to uiprogress
  • May have higher memory usage due to additional features

Code Comparison

uiprogress:

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

progressbar:

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

Both libraries offer similar basic functionality for creating and updating progress bars. uiprogress uses AddBar() to create a bar and Incr() to increment it, while progressbar uses Default() for creation and Add() for increments.

progressbar provides more customization options out of the box, such as themes and emoji support, which can be beneficial for creating visually appealing progress indicators. It also supports multiple concurrent progress bars and includes built-in spinners, making it more feature-rich.

However, uiprogress has a simpler API, which may be preferable for basic use cases. It might also have lower memory usage due to fewer features, although this would depend on specific implementation details and usage patterns.

3,634

Console progress bar for Golang

Pros of pb

  • More feature-rich with advanced options like adaptive refresh rate and custom width
  • Supports multiple concurrent progress bars
  • Offers a wider variety of progress bar styles and customizations

Cons of pb

  • Slightly more complex API, which may be overkill for simple use cases
  • Less focus on decoupled components compared to uiprogress

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:

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

Both libraries provide similar basic functionality for creating and updating progress bars. pb offers more advanced features and customization options, while uiprogress focuses on simplicity and ease of use. The choice between the two depends on the specific requirements of your project and the level of complexity you're comfortable with.

2,290

multi progress bar for Go cli applications

Pros of mpb

  • More feature-rich, offering advanced functionality like spinners, decorators, and multi-progress bars
  • Better performance, especially for large numbers of progress bars
  • More actively maintained with frequent updates and improvements

Cons of mpb

  • Steeper learning curve due to more complex API and additional features
  • Larger codebase and dependencies, potentially increasing project size

Code Comparison

mpb:

p := mpb.New(mpb.WithWidth(64))
bar := p.AddBar(100,
    mpb.PrependDecorators(
        decor.Name("Processing"),
        decor.Percentage(decor.WCSyncSpace),
    ),
    mpb.AppendDecorators(
        decor.ETA(decor.ET_STYLE_GO),
    ),
)

uiprogress:

uiprogress.Start()
bar := uiprogress.AddBar(100)
bar.AppendCompleted()
bar.PrependElapsed()

Both libraries provide progress bar functionality for Go applications, but mpb offers more advanced features and customization options at the cost of increased complexity. uiprogress is simpler to use and may be sufficient for basic progress tracking needs. The choice between the two depends on the specific requirements of your project and the level of customization desired.

2,327

Go (golang) package with 90 configurable terminal spinner/progress indicators.

Pros of spinner

  • More diverse spinner styles and customization options
  • Supports color output for enhanced visual appeal
  • Allows for custom characters and Unicode spinners

Cons of spinner

  • Focused solely on spinners, lacking progress bar functionality
  • May require more setup for complex progress tracking scenarios

Code Comparison

spinner:

s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Color("red")
s.Start()
time.Sleep(4 * time.Second)
s.Stop()

uiprogress:

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

Summary

spinner offers a wide range of spinner styles and customization options, including color support and Unicode characters. It's ideal for simple loading indicators but lacks progress bar functionality.

uiprogress provides a more comprehensive progress tracking solution with progress bars, but has fewer spinner options. It's better suited for scenarios requiring detailed progress reporting.

Choose spinner for simple, visually appealing loading indicators, and uiprogress for more complex progress tracking needs.

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

uiprogress GoDoc Build Status

A Go library to render progress bars in terminal applications. It provides a set of flexible features with a customizable API.

example

Progress bars improve readability for terminal applications with long outputs by providing a concise feedback loop.

Features

  • Multiple Bars: uiprogress can render multiple progress bars that can be tracked concurrently
  • Dynamic Addition: Add additional progress bars any time, even after the progress tracking has started
  • Prepend and Append Functions: Append or prepend completion percent and time elapsed to the progress bars
  • Custom Decorator Functions: Add custom functions around the bar along with helper functions

Usage

To start listening for progress bars, call uiprogress.Start() and add a progress bar using uiprogress.AddBar(total int). Update the progress using bar.Incr() or bar.Set(n int). Full source code for the below example is available at example/simple/simple.go

uiprogress.Start()            // start rendering
bar := uiprogress.AddBar(100) // Add a new bar

// optionally, append and prepend completion and elapsed time
bar.AppendCompleted()
bar.PrependElapsed()

for bar.Incr() {
  time.Sleep(time.Millisecond * 20)
}

This will render the below in the terminal

example

Using Custom Decorators

You can also add a custom decorator function in addition to default bar.AppendCompleted() and bar.PrependElapsed() decorators. The below example tracks the current step for an application deploy progress. Source code for the below example is available at example/full/full.go

var steps = []string{"downloading source", "installing deps", "compiling", "packaging", "seeding database", "deploying", "staring servers"}
bar := uiprogress.AddBar(len(steps))

// prepend the current step to the bar
bar.PrependFunc(func(b *uiprogress.Bar) string {
  return "app: " + steps[b.Current()-1]
})

for bar.Incr() {
  time.Sleep(time.Millisecond * 10)
}

Rendering Multiple bars

You can add multiple bars using uiprogress.AddBar(n). The below example demonstrates updating multiple bars concurrently and adding a new bar later in the pipeline. Source for this example is available at example/multi/multi.go

waitTime := time.Millisecond * 100
uiprogress.Start()

// start the progress bars in go routines
var wg sync.WaitGroup

bar1 := uiprogress.AddBar(20).AppendCompleted().PrependElapsed()
wg.Add(1)
go func() {
  defer wg.Done()
  for bar1.Incr() {
    time.Sleep(waitTime)
  }
}()

bar2 := uiprogress.AddBar(40).AppendCompleted().PrependElapsed()
wg.Add(1)
go func() {
  defer wg.Done()
  for bar2.Incr() {
    time.Sleep(waitTime)
  }
}()

time.Sleep(time.Second)
bar3 := uiprogress.AddBar(20).PrependElapsed().AppendCompleted()
wg.Add(1)
go func() {
  defer wg.Done()
  for i := 1; i <= bar3.Total; i++ {
    bar3.Set(i)
    time.Sleep(waitTime)
  }
}()

// wait for all the go routines to finish
wg.Wait()

This will produce

example

Incr counter

Bar.Incr() is an atomic counter and can be used as a general tracker, making it ideal for tracking progress of work fanned out to a lots of go routines. The source code for the below example is available at example/incr/incr.go

runtime.GOMAXPROCS(runtime.NumCPU()) // use all available cpu cores

// create a new bar and prepend the task progress to the bar and fanout into 1k go routines
count := 1000
bar := uiprogress.AddBar(count).AppendCompleted().PrependElapsed()
bar.PrependFunc(func(b *uiprogress.Bar) string {
  return fmt.Sprintf("Task (%d/%d)", b.Current(), count)
})

uiprogress.Start()
var wg sync.WaitGroup

// fanout into go routines
for i := 0; i < count; i++ {
  wg.Add(1)
  go func() {
    defer wg.Done()
    time.Sleep(time.Millisecond * time.Duration(rand.Intn(500)))
    bar.Incr()
  }()
}
time.Sleep(time.Second) // wait for a second for all the go routines to finish
wg.Wait()
uiprogress.Stop()

Installation

$ go get -v github.com/gosuri/uiprogress

Todos

  • Resize bars and decorators by auto detecting window's dimensions
  • Handle more progress bars than vertical screen allows

License

uiprogress is released under the MIT License. See LICENSE.