more examples
Vladimir Bauer
9 years ago
| 155 | 155 |
|
| 156 | 156 |
// String returns the string representation of the bar
|
| 157 | 157 |
func (b *Bar) String() string {
|
| 158 | |
// if b.IsStopped() {
|
| 159 | |
// return "bar stopped"
|
| 160 | |
// }
|
| 161 | 158 |
bufCh := make(chan []byte)
|
| 162 | 159 |
b.redrawRequestCh <- &redrawRequest{bufCh}
|
| 163 | 160 |
return string(<-bufCh)
|
|
0 |
package main
|
|
1 |
|
|
2 |
import (
|
|
3 |
"fmt"
|
|
4 |
"math/rand"
|
|
5 |
"runtime"
|
|
6 |
"time"
|
|
7 |
|
|
8 |
"github.com/vbauerster/uiprogress"
|
|
9 |
)
|
|
10 |
|
|
11 |
const (
|
|
12 |
totalItem = 100
|
|
13 |
maxBlockSize = 10
|
|
14 |
)
|
|
15 |
|
|
16 |
func main() {
|
|
17 |
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
18 |
decor := func(s *uiprogress.Statistics) string {
|
|
19 |
str := fmt.Sprintf("%d/%d", s.Completed, s.Total)
|
|
20 |
return fmt.Sprintf("%-7s", str)
|
|
21 |
}
|
|
22 |
|
|
23 |
p := uiprogress.New()
|
|
24 |
bar := p.AddBar(totalItem).AppendETA().PrependFunc(decor)
|
|
25 |
|
|
26 |
blockSize := rand.Intn(maxBlockSize) + 1
|
|
27 |
// Fallowing will hang, in order not to hang
|
|
28 |
// use !bar.IsCompleted in loop condition
|
|
29 |
// for i := 0; !bar.IsCompleted(); i += blockSize {
|
|
30 |
for i := 0; i < totalItem; i += blockSize {
|
|
31 |
time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
|
|
32 |
bar.Incr(blockSize)
|
|
33 |
blockSize = rand.Intn(maxBlockSize) + 1
|
|
34 |
}
|
|
35 |
|
|
36 |
p.WaitAndStop()
|
|
37 |
fmt.Println("stop")
|
|
38 |
}
|
| 55 | 55 |
// time.Sleep(time.Second)
|
| 56 | 56 |
// p.RemoveBar(bar2)
|
| 57 | 57 |
|
| 58 | |
p.Stop()
|
|
58 |
p.WaitAndStop()
|
|
59 |
bar2.Incr(2)
|
| 59 | 60 |
fmt.Println("stop")
|
| 60 | 61 |
// p.AddBar(1) // panic: send on closed channnel
|
| 61 | 62 |
}
|
|
0 |
package main
|
|
1 |
|
|
2 |
import (
|
|
3 |
"fmt"
|
|
4 |
"math/rand"
|
|
5 |
"runtime"
|
|
6 |
"time"
|
|
7 |
|
|
8 |
"github.com/vbauerster/uiprogress"
|
|
9 |
)
|
|
10 |
|
|
11 |
const (
|
|
12 |
totalItem = 100
|
|
13 |
maxBlockSize = 20
|
|
14 |
)
|
|
15 |
|
|
16 |
func main() {
|
|
17 |
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
18 |
decor := func(s *uiprogress.Statistics) string {
|
|
19 |
str := fmt.Sprintf("%d/%d", s.Completed, s.Total)
|
|
20 |
return fmt.Sprintf("%-7s", str)
|
|
21 |
}
|
|
22 |
|
|
23 |
p := uiprogress.New()
|
|
24 |
bar := p.AddBar(totalItem).AppendETA().PrependFunc(decor)
|
|
25 |
|
|
26 |
blockSize := rand.Intn(maxBlockSize) + 1
|
|
27 |
for i := 0; !bar.IsCompleted(); i += 1 {
|
|
28 |
time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
|
|
29 |
bar.Incr(1)
|
|
30 |
if i == 42 {
|
|
31 |
p.RemoveBar(bar)
|
|
32 |
}
|
|
33 |
blockSize = rand.Intn(maxBlockSize) + 1
|
|
34 |
}
|
|
35 |
|
|
36 |
p.WaitAndStop()
|
|
37 |
fmt.Println("stop")
|
|
38 |
}
|
| 2 | 2 |
import (
|
| 3 | 3 |
"fmt"
|
| 4 | 4 |
"math/rand"
|
|
5 |
"runtime"
|
| 5 | 6 |
"time"
|
| 6 | 7 |
|
| 7 | 8 |
"github.com/vbauerster/uiprogress"
|
| 8 | 9 |
)
|
| 9 | 10 |
|
| 10 | 11 |
const (
|
| 11 | |
totalItem = 1000
|
| 12 | |
maxBlockSize = 20
|
|
12 |
totalItem = 100
|
|
13 |
maxBlockSize = 12
|
| 13 | 14 |
)
|
| 14 | 15 |
|
| 15 | 16 |
func main() {
|
|
17 |
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
18 |
decor := func(s *uiprogress.Statistics) string {
|
|
19 |
str := fmt.Sprintf("%d/%d", s.Completed, s.Total)
|
|
20 |
return fmt.Sprintf("%-7s", str)
|
|
21 |
}
|
|
22 |
|
| 16 | 23 |
p := uiprogress.New()
|
| 17 | |
bar := p.AddBar(totalItem) // Add a new bar
|
| 18 | |
|
| 19 | |
// optionally, append and prepend completion and elapsed time
|
| 20 | |
bar.AppendETA()
|
| 21 | |
// bar.PrependElapsed()
|
|
24 |
bar := p.AddBar(totalItem).AppendETA().PrependFunc(decor)
|
| 22 | 25 |
|
| 23 | 26 |
blockSize := rand.Intn(maxBlockSize) + 1
|
| 24 | |
for i := 1; i <= totalItem; i += blockSize {
|
|
27 |
for i := 0; i < 100; i += 1 {
|
| 25 | 28 |
time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
|
| 26 | |
bar.Incr(blockSize)
|
|
29 |
bar.Incr(1)
|
| 27 | 30 |
blockSize = rand.Intn(maxBlockSize) + 1
|
| 28 | 31 |
}
|
| 29 | 32 |
|
|
33 |
p.WaitAndStop()
|
| 30 | 34 |
fmt.Println("stop")
|
| 31 | |
|
| 32 | |
p.Stop()
|
| 33 | 35 |
}
|