• Stars
    star
    2,062
  • Rank 21,488 (Top 0.5 %)
  • Language
    Go
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A go library to render progress bars in terminal applications

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.

More Repositories

1

uilive

uilive is a go library for updating terminal output in realtime
Go
1,648
star
2

uitable

A go library to improve readability in terminal apps using tabular data
Go
721
star
3

vagrant-env

Vagrant plugin to load environment variables from .env into ENV
Ruby
144
star
4

go-store

A simple and fast Redis backed key-value store library for Go
Go
112
star
5

cmdns

cmdns is a go library for namespacing a command tree
Go
25
star
6

aws-rds-cookbook

Development repository for chef cookbook aws-rds
Ruby
16
star
7

dotfiles

Greg Osuri's dotfiles
Shell
10
star
8

terraform-exec-provider

Go
9
star
9

git-url-sub

git remote url substitution utility
Shell
7
star
10

awesome-sysops

Curated list of awesome tools for systems operators
6
star
11

rails-app-cookbook

Example cookbook using aws-rds cookbook to install rails_app
Ruby
6
star
12

dsky

dsky is a UI framework for terminal applications
Go
5
star
13

monocle

a go library for advanced command line help
Go
5
star
14

multibar

[DEPRICATED] MultiBar is go library to render progress bars in the terminal
Go
5
star
15

Pylearn

Greg Osuri's Python Journey
4
star
16

uiutil

uiutil is a go library that provides a set of helpers to improve readbility in terminal applications
Go
4
star
17

handshake-containers

Makefile
4
star
18

ctxexec

ctxexec is go library that provides helper functions for running context-aware external commands
Go
3
star
19

grumpy

Grumpy cat is a blockchain to honor Grumpy Cat
TypeScript
2
star
20

gosuri.github.io

Source for gregosuri.com
SCSS
2
star
21

aws-vpc-terraform-post

Support files for my airpair post
1
star
22

petit

a simple, fast, rack-based url routing server
Ruby
1
star
23

test-runner

Linux container based test running tools for ruby applications
Shell
1
star
24

ap-workshop-cookbooks

Chef cookbooks for my Airpair containers workshop
Ruby
1
star
25

vanitypkg

vanitypkg provides a server that hosts vanity package names for Go package hosted on github
Go
1
star
26

akt20

1
star
27

hostnamer

Cluster member discovery and registration tool for Route 53
Ruby
1
star
28

knife-ec2-ssh-config

Knife plugin to update ssh client config with ec2 instance information
Ruby
1
star
29

chef-ebs-directory

Chef cookbook for moving a directory to a ebs volume
Ruby
1
star
30

noop

An empty repo used for testing git ops
1
star
31

build-compiler

Container based build tools for Ubuntu 14.04
Shell
1
star
32

nextjs-notes

JavaScript
1
star
33

eve

eve fork
Go
1
star