minprogress
A progress bar for Go that can also track the speed of progress.
Example of it used in conjuction with minterm.
Usage
###Import
import "github.com/MinoMino/minprogress"
###Simple Use
Make 5 units of progress every second while printing the bar every second:
const total = 50
pb := minprogress.NewProgressBar(total)
tick := time.Tick(time.Millisecond * 200)
go func() {
for {
<-tick
pb.Progress(1)
}
}()
for {
if pb.Progress(0) == total {
fmt.Println("Done!")
break
} else {
fmt.Println(pb.String())
time.Sleep(time.Second)
}
}
Output:
[...]
60% █████████████████░░░░░░░░░░░░ (30/50)
70% ████████████████████░░░░░░░░░ (35/50)
80% ███████████████████████░░░░░░ (40/50)
90% ██████████████████████████░░░ (45/50)
Done!
###Advanced Use
Simulate concurrent file downloads and track download speed:
const total = 50
const workerCount = 10
pb := minprogress.NewProgressBar(total)
pb.SpeedUnits = minprogress.DataUnits
pb.Unit = "file"
pb.Units = "files"
go func() {
workerQueue := make(chan struct{}, workerCount)
for i := 0; i < total; i++ {
workerQueue <- struct{}{}
go func(uid int) {
size := rand.Intn(800000) + 200000
downloaded := 0
for downloaded < size {
time.Sleep(time.Millisecond*time.Duration(rand.Intn(100)) + 100)
got := rand.Intn(9000) + 1000
pb.Report(uid, got)
downloaded += got
}
pb.Done(uid)
pb.Progress(1)
<-workerQueue
}(i)
}
}()
for {
if pb.Progress(0) == total {
fmt.Println("Done!")
break
} else {
fmt.Println(pb.String())
time.Sleep(time.Second)
}
}
Output:
[...]
82% ████████████████████████░░░░░ (41/50) files [3.4 MiB/s]
88% ██████████████████████████░░░ (44/50) files [1.9 MiB/s]
92% ███████████████████████████░░ (46/50) files [1.3 MiB/s]
94% ███████████████████████████░░ (47/50) files [730.5 KiB/s]
94% ███████████████████████████░░ (47/50) files [704.7 KiB/s]
96% ████████████████████████████░ (48/50) files [764.3 KiB/s]
98% ████████████████████████████░ (49/50) files [356.9 KiB/s]
98% ████████████████████████████░ (49/50) files [259.5 KiB/s]
Done!
License
MIT. See LICENSE
for details.