• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.travis.ymlH A D18-Feb-201868

LICENSEH A D18-Feb-20181.4 KiB

README.mdH A D18-Feb-20183.3 KiB

example_copy_test.goH A D18-Feb-20181.6 KiB

example_multiple_test.goH A D18-Feb-2018694

example_test.goH A D18-Feb-2018441

format.goH A D18-Feb-20182.3 KiB

format_test.goH A D18-Feb-20181.8 KiB

pb.goH A D18-Feb-201810.8 KiB

pb_appengine.goH A D18-Feb-2018274

pb_test.goH A D18-Feb-20182.6 KiB

pb_win.goH A D18-Feb-20184.1 KiB

pb_x.goH A D18-Feb-20182.1 KiB

pool.goH A D18-Feb-20181.7 KiB

pool_win.goH A D18-Feb-2018691

pool_x.goH A D18-Feb-2018540

reader.goH A D18-Feb-2018400

runecount.goH A D18-Feb-2018364

runecount_test.goH A D18-Feb-2018508

termios_bsd.goH A D18-Feb-2018187

termios_sysv.goH A D18-Feb-2018325

README.md

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