| 1 |
# uiprogress [](https://godoc.org/github.com/gosuri/uiprogress) [](https://travis-ci.org/gosuri/uiprogress) |
| 2 |
|
| 3 |
A Go library to render progress bars in terminal applications. It provides a set of flexible features with a customizable API. |
| 4 |
|
| 5 |
 |
| 6 |
|
| 7 |
Progress bars improve readability for terminal applications with long outputs by providing a concise feedback loop. |
| 8 |
|
| 9 |
## Features |
| 10 |
|
| 11 |
* __Multiple Bars__: uiprogress can render multiple progress bars that can be tracked concurrently |
| 12 |
* __Dynamic Addition__: Add additional progress bars any time, even after the progress tracking has started |
| 13 |
* __Prepend and Append Functions__: Append or prepend completion percent and time elapsed to the progress bars |
| 14 |
* __Custom Decorator Functions__: Add custom functions around the bar along with helper functions |
| 15 |
|
| 16 |
## Usage |
| 17 |
|
| 18 |
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](example/simple/simple.go) |
| 19 |
|
| 20 |
```go |
| 21 |
uiprogress.Start() // start rendering |
| 22 |
bar := uiprogress.AddBar(100) // Add a new bar |
| 23 |
|
| 24 |
// optionally, append and prepend completion and elapsed time |
| 25 |
bar.AppendCompleted() |
| 26 |
bar.PrependElapsed() |
| 27 |
|
| 28 |
for bar.Incr() { |
| 29 |
time.Sleep(time.Millisecond * 20) |
| 30 |
} |
| 31 |
``` |
| 32 |
|
| 33 |
This will render the below in the terminal |
| 34 |
|
| 35 |
 |
| 36 |
|
| 37 |
### Using Custom Decorators |
| 38 |
|
| 39 |
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](example/full/full.go) |
| 40 |
|
| 41 |
```go |
| 42 |
var steps = []string{"downloading source", "installing deps", "compiling", "packaging", "seeding database", "deploying", "staring servers"} |
| 43 |
bar := uiprogress.AddBar(len(steps)) |
| 44 |
|
| 45 |
// prepend the current step to the bar |
| 46 |
bar.PrependFunc(func(b *uiprogress.Bar) string { |
| 47 |
return "app: " + steps[b.Current()-1] |
| 48 |
}) |
| 49 |
|
| 50 |
for bar.Incr() { |
| 51 |
time.Sleep(time.Millisecond * 10) |
| 52 |
} |
| 53 |
``` |
| 54 |
|
| 55 |
### Rendering Multiple bars |
| 56 |
|
| 57 |
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](example/multi/multi.go) |
| 58 |
|
| 59 |
```go |
| 60 |
waitTime := time.Millisecond * 100 |
| 61 |
uiprogress.Start() |
| 62 |
|
| 63 |
// start the progress bars in go routines |
| 64 |
var wg sync.WaitGroup |
| 65 |
|
| 66 |
bar1 := uiprogress.AddBar(20).AppendCompleted().PrependElapsed() |
| 67 |
wg.Add(1) |
| 68 |
go func() { |
| 69 |
defer wg.Done() |
| 70 |
for bar1.Incr() { |
| 71 |
time.Sleep(waitTime) |
| 72 |
} |
| 73 |
}() |
| 74 |
|
| 75 |
bar2 := uiprogress.AddBar(40).AppendCompleted().PrependElapsed() |
| 76 |
wg.Add(1) |
| 77 |
go func() { |
| 78 |
defer wg.Done() |
| 79 |
for bar2.Incr() { |
| 80 |
time.Sleep(waitTime) |
| 81 |
} |
| 82 |
}() |
| 83 |
|
| 84 |
time.Sleep(time.Second) |
| 85 |
bar3 := uiprogress.AddBar(20).PrependElapsed().AppendCompleted() |
| 86 |
wg.Add(1) |
| 87 |
go func() { |
| 88 |
defer wg.Done() |
| 89 |
for i := 1; i <= bar3.Total; i++ { |
| 90 |
bar3.Set(i) |
| 91 |
time.Sleep(waitTime) |
| 92 |
} |
| 93 |
}() |
| 94 |
|
| 95 |
// wait for all the go routines to finish |
| 96 |
wg.Wait() |
| 97 |
``` |
| 98 |
|
| 99 |
This will produce |
| 100 |
|
| 101 |
 |
| 102 |
|
| 103 |
### `Incr` counter |
| 104 |
|
| 105 |
[Bar.Incr()](https://godoc.org/github.com/gosuri/uiprogress#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](example/incr/incr.go) |
| 106 |
|
| 107 |
```go |
| 108 |
runtime.GOMAXPROCS(runtime.NumCPU()) // use all available cpu cores |
| 109 |
|
| 110 |
// create a new bar and prepend the task progress to the bar and fanout into 1k go routines |
| 111 |
count := 1000 |
| 112 |
bar := uiprogress.AddBar(count).AppendCompleted().PrependElapsed() |
| 113 |
bar.PrependFunc(func(b *uiprogress.Bar) string { |
| 114 |
return fmt.Sprintf("Task (%d/%d)", b.Current(), count) |
| 115 |
}) |
| 116 |
|
| 117 |
uiprogress.Start() |
| 118 |
var wg sync.WaitGroup |
| 119 |
|
| 120 |
// fanout into go routines |
| 121 |
for i := 0; i < count; i++ { |
| 122 |
wg.Add(1) |
| 123 |
go func() { |
| 124 |
defer wg.Done() |
| 125 |
time.Sleep(time.Millisecond * time.Duration(rand.Intn(500))) |
| 126 |
bar.Incr() |
| 127 |
}() |
| 128 |
} |
| 129 |
time.Sleep(time.Second) // wait for a second for all the go routines to finish |
| 130 |
wg.Wait() |
| 131 |
uiprogress.Stop() |
| 132 |
``` |
| 133 |
|
| 134 |
## Installation |
| 135 |
|
| 136 |
```sh |
| 137 |
$ go get -v github.com/gosuri/uiprogress |
| 138 |
``` |
| 139 |
## Todos |
| 140 |
|
| 141 |
- [ ] Resize bars and decorators by auto detecting window's dimensions |
| 142 |
- [ ] Handle more progress bars than vertical screen allows |
| 143 |
|
| 144 |
## License |
| 145 |
|
| 146 |
uiprogress is released under the MIT License. See [LICENSE](https://github.com/gosuri/uiprogress/blob/master/LICENSE). |