1# Terminal progress bar for Go
2
3Simple progress bar for console programs.
4
5Please check the new version https://github.com/cheggaaa/pb/tree/v2 (currently, it's beta)
6
7## Installation
8
9```
10go get gopkg.in/cheggaaa/pb.v1
11```
12
13## Usage
14
15```Go
16package main
17
18import (
19	"gopkg.in/cheggaaa/pb.v1"
20	"time"
21)
22
23func main() {
24	count := 100000
25	bar := pb.StartNew(count)
26	for i := 0; i < count; i++ {
27		bar.Increment()
28		time.Sleep(time.Millisecond)
29	}
30	bar.FinishPrint("The End!")
31}
32
33```
34
35Result will be like this:
36
37```
38> go run test.go
3937158 / 100000 [================>_______________________________] 37.16% 1m11s
40```
41
42## Customization
43
44```Go
45// create bar
46bar := pb.New(count)
47
48// refresh info every second (default 200ms)
49bar.SetRefreshRate(time.Second)
50
51// show percents (by default already true)
52bar.ShowPercent = true
53
54// show bar (by default already true)
55bar.ShowBar = true
56
57// no counters
58bar.ShowCounters = false
59
60// show "time left"
61bar.ShowTimeLeft = true
62
63// show average speed
64bar.ShowSpeed = true
65
66// sets the width of the progress bar
67bar.SetWidth(80)
68
69// sets the width of the progress bar, but if terminal size smaller will be ignored
70bar.SetMaxWidth(80)
71
72// convert output to readable format (like KB, MB)
73bar.SetUnits(pb.U_BYTES)
74
75// and start
76bar.Start()
77```
78
79## Progress bar for IO Operations
80
81```go
82// create and start bar
83bar := pb.New(myDataLen).SetUnits(pb.U_BYTES)
84bar.Start()
85
86// my io.Reader
87r := myReader
88
89// my io.Writer
90w := myWriter
91
92// create proxy reader
93reader := bar.NewProxyReader(r)
94
95// and copy from pb reader
96io.Copy(w, reader)
97
98```
99
100```go
101// create and start bar
102bar := pb.New(myDataLen).SetUnits(pb.U_BYTES)
103bar.Start()
104
105// my io.Reader
106r := myReader
107
108// my io.Writer
109w := myWriter
110
111// create multi writer
112writer := io.MultiWriter(w, bar)
113
114// and copy
115io.Copy(writer, r)
116
117bar.Finish()
118```
119
120## Custom Progress Bar Look-and-feel
121
122```go
123bar.Format("<.- >")
124```
125
126## Multiple Progress Bars (experimental and unstable)
127
128Do not print to terminal while pool is active.
129
130```go
131package main
132
133import (
134    "math/rand"
135    "sync"
136    "time"
137
138    "gopkg.in/cheggaaa/pb.v1"
139)
140
141func main() {
142    // create bars
143    first := pb.New(200).Prefix("First ")
144    second := pb.New(200).Prefix("Second ")
145    third := pb.New(200).Prefix("Third ")
146    // start pool
147    pool, err := pb.StartPool(first, second, third)
148    if err != nil {
149        panic(err)
150    }
151    // update bars
152    wg := new(sync.WaitGroup)
153    for _, bar := range []*pb.ProgressBar{first, second, third} {
154        wg.Add(1)
155        go func(cb *pb.ProgressBar) {
156            for n := 0; n < 200; n++ {
157                cb.Increment()
158                time.Sleep(time.Millisecond * time.Duration(rand.Intn(100)))
159            }
160            cb.Finish()
161            wg.Done()
162        }(bar)
163    }
164    wg.Wait()
165    // close pool
166    pool.Stop()
167}
168```
169
170The result will be as follows:
171
172```
173$ go run example/multiple.go
174First  34 / 200 [=========>---------------------------------------------]  17.00% 00m08s
175Second  42 / 200 [===========>------------------------------------------]  21.00% 00m06s
176Third  36 / 200 [=========>---------------------------------------------]  18.00% 00m08s
177```
178