Convert Figma logo to code with AI

schollz logoprogressbar

A really basic thread-safe progress bar for Golang applications

4,016
219
4,016
33

Top Related Projects

1,731

A Go library for implementing command-line interfaces.

3,634

Console progress bar for Golang

2,290

multi progress bar for Go cli applications

A go library to render progress bars in terminal applications

Quick Overview

schollz/progressbar is a Go library for creating customizable progress bars in terminal applications. It offers a simple yet flexible API for displaying progress during long-running operations, with support for various styles, colors, and dynamic updates.

Pros

  • Easy to use with a straightforward API
  • Highly customizable with multiple styles and color options
  • Supports concurrent operations with multiple progress bars
  • Automatically adapts to terminal width

Cons

  • Limited to terminal-based applications
  • May not be suitable for very complex progress tracking scenarios
  • Requires manual updating of progress, which could be error-prone
  • Limited cross-platform support for some advanced features

Code Examples

Basic usage:

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

Custom style:

bar := progressbar.NewOptions(1000,
    progressbar.OptionSetWidth(15),
    progressbar.OptionSetDescription("Downloading"),
    progressbar.OptionSetTheme(progressbar.Theme{
        Saucer:        "=",
        SaucerHead:    ">",
        SaucerPadding: " ",
        BarStart:      "[",
        BarEnd:        "]",
    }))
for i := 0; i < 1000; i++ {
    bar.Add(1)
    time.Sleep(5 * time.Millisecond)
}

Multiple concurrent progress bars:

var wg sync.WaitGroup
for i := 0; i < 3; i++ {
    wg.Add(1)
    go func(i int) {
        defer wg.Done()
        bar := progressbar.Default(100, fmt.Sprintf("Task %d", i+1))
        for j := 0; j < 100; j++ {
            bar.Add(1)
            time.Sleep(50 * time.Millisecond)
        }
    }(i)
}
wg.Wait()

Getting Started

To use schollz/progressbar in your Go project:

  1. Install the library:

    go get -u github.com/schollz/progressbar/v3
    
  2. Import the library in your Go code:

    import "github.com/schollz/progressbar/v3"
    
  3. Create and use a progress bar:

    bar := progressbar.Default(100)
    for i := 0; i < 100; i++ {
        bar.Add(1)
        // Perform your task here
    }
    

Competitor Comparisons

1,731

A Go library for implementing command-line interfaces.

Pros of cli

  • Comprehensive CLI application framework with support for subcommands, flags, and arguments
  • Built-in help generation and version information
  • Extensible with custom command implementations

Cons of cli

  • More complex setup and usage compared to a simple progress bar
  • Larger codebase and potentially higher learning curve
  • May be overkill for projects that only need basic CLI functionality

Code Comparison

progressbar:

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

cli:

c := cli.NewCLI("app", "1.0.0")
c.Args = os.Args[1:]
c.Commands = map[string]cli.CommandFactory{
    "foo": fooCommandFactory,
}
c.Run()

Summary

progressbar is a lightweight library focused on creating progress bars for CLI applications, while cli is a more comprehensive framework for building full-featured command-line interfaces. progressbar is simpler to use for basic progress tracking, while cli offers more advanced features for complex CLI applications with multiple commands and options.

3,634

Console progress bar for Golang

Pros of pb

  • More feature-rich with multiple progress bar styles and customization options
  • Better support for concurrent progress bars
  • Includes a pool feature for managing multiple bars efficiently

Cons of pb

  • Slightly more complex API, which may have a steeper learning curve
  • Less frequent updates and maintenance compared to progressbar

Code Comparison

progressbar:

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

pb:

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

Summary

Both progressbar and pb are popular Go libraries for creating progress bars. progressbar offers a simpler API and is actively maintained, making it a good choice for straightforward use cases. pb provides more advanced features and customization options, which can be beneficial for complex projects requiring multiple progress bars or specific styling. The choice between the two depends on the project's requirements and the developer's preference for simplicity versus feature richness.

2,290

multi progress bar for Go cli applications

Pros of mpb

  • More feature-rich with support for multiple concurrent progress bars
  • Offers greater customization options for bar appearance and behavior
  • Provides better support for dynamic updates and real-time statistics

Cons of mpb

  • Slightly more complex API, which may require a steeper learning curve
  • Larger codebase and dependencies, potentially increasing project size

Code Comparison

mpb:

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

progressbar:

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

mpb offers more flexibility in configuring the progress bar's appearance and behavior, while progressbar provides a simpler, more straightforward API for basic use cases. mpb's support for multiple concurrent progress bars makes it better suited for complex applications, whereas progressbar is ideal for simpler scenarios where ease of use is prioritized over advanced features.

A go library to render progress bars in terminal applications

Pros of uiprogress

  • Supports multiple concurrent progress bars
  • Offers more customization options for progress bar appearance
  • Provides a wider range of progress bar types (e.g., indeterminate)

Cons of uiprogress

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

Code Comparison

uiprogress:

bar := uiprogress.AddBar(100)
bar.AppendCompleted()
bar.PrependElapsed()
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 simple ways to create and update progress bars, but uiprogress provides more built-in options for customization out of the box. progressbar has a slightly more straightforward API for basic use cases, making it easier to get started quickly. However, uiprogress's ability to handle multiple concurrent progress bars and its wider range of progress bar types make it more suitable for complex applications with diverse 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

progressbar

CI go report card coverage godocs

A very simple thread-safe progress bar which should work on every OS without problems. I needed a progressbar for croc and everything I tried had problems, so I made another one. In order to be OS agnostic I do not plan to support multi-line outputs.

Install

go get -u github.com/schollz/progressbar/v3

Usage

Basic usage

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

which looks like:

Example of basic bar

I/O operations

The progressbar implements an io.Writer so it can automatically detect the number of bytes written to a stream, so you can use it as a progressbar for an io.Reader.

req, _ := http.NewRequest("GET", "https://dl.google.com/go/go1.14.2.src.tar.gz", nil)
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()

f, _ := os.OpenFile("go1.14.2.src.tar.gz", os.O_CREATE|os.O_WRONLY, 0644)
defer f.Close()

bar := progressbar.DefaultBytes(
    resp.ContentLength,
    "downloading",
)
io.Copy(io.MultiWriter(f, bar), resp.Body)

which looks like:

Example of download bar

Progress bar with unknown length

A progressbar with unknown length is a spinner. Any bar with -1 length will automatically convert it to a spinner with a customizable spinner type. For example, the above code can be run and set the resp.ContentLength to -1.

which looks like:

Example of download bar with unknown length

Customization

There is a lot of customization that you can do - change the writer, the color, the width, description, theme, etc. See all the options.

bar := progressbar.NewOptions(1000,
    progressbar.OptionSetWriter(ansi.NewAnsiStdout()), //you should install "github.com/k0kubun/go-ansi"
    progressbar.OptionEnableColorCodes(true),
    progressbar.OptionShowBytes(true),
    progressbar.OptionSetWidth(15),
    progressbar.OptionSetDescription("[cyan][1/3][reset] Writing moshable file..."),
    progressbar.OptionSetTheme(progressbar.Theme{
        Saucer:        "[green]=[reset]",
        SaucerHead:    "[green]>[reset]",
        SaucerPadding: " ",
        BarStart:      "[",
        BarEnd:        "]",
    }))
for i := 0; i < 1000; i++ {
    bar.Add(1)
    time.Sleep(5 * time.Millisecond)
}

which looks like:

Example of customized bar

Contributing

Pull requests are welcome. Feel free to...

  • Revise documentation
  • Add new features
  • Fix bugs
  • Suggest improvements

Thanks

Thanks @Dynom for massive improvements in version 2.0!

Thanks @CrushedPixel for adding descriptions and color code support!

Thanks @MrMe42 for adding some minor features!

Thanks @tehstun for some great PRs!

Thanks @Benzammour and @haseth for helping create v3!

Thanks @briandowns for compiling the list of spinners.

License

MIT