Refactor: examples
Vladimir Bauer
8 years ago
| 43 | 43 |
|
| 44 | 44 |
go func() {
|
| 45 | 45 |
defer wg.Done()
|
|
46 |
max := 200 * time.Millisecond
|
| 46 | 47 |
for !bar.Completed() {
|
| 47 | |
time.Sleep(randomDuration(200 * time.Millisecond))
|
|
48 |
time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
|
| 48 | 49 |
bar.Increment()
|
| 49 | 50 |
}
|
| 50 | 51 |
}()
|
|
| 53 | 54 |
p.Stop()
|
| 54 | 55 |
fmt.Println("stop")
|
| 55 | 56 |
}
|
| 56 | |
|
| 57 | |
func randomDuration(max time.Duration) time.Duration {
|
| 58 | |
return time.Duration(rand.Intn(10)+1) * max / 10
|
| 59 | |
}
|
| 36 | 36 |
)
|
| 37 | 37 |
go func() {
|
| 38 | 38 |
defer wg.Done()
|
|
39 |
max := 200 * time.Millisecond
|
| 39 | 40 |
for i := 0; i < total; i++ {
|
| 40 | |
time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10)
|
|
41 |
time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
|
| 41 | 42 |
b.Increment()
|
| 42 | 43 |
if b.ID() == 1 && i == 42 {
|
| 43 | 44 |
b.Complete()
|
| 39 | 39 |
bar.SetTotal(300, true)
|
| 40 | 40 |
}()
|
| 41 | 41 |
|
|
42 |
max := 200 * time.Millisecond
|
| 42 | 43 |
for i := 0; i < 300; i++ {
|
| 43 | 44 |
if i == 140 {
|
| 44 | 45 |
close(totalUpd1)
|
|
| 46 | 47 |
if i == 250 {
|
| 47 | 48 |
close(totalUpd2)
|
| 48 | 49 |
}
|
| 49 | |
time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10)
|
|
50 |
time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
|
| 50 | 51 |
bar.Increment()
|
| 51 | 52 |
}
|
| 52 | 53 |
|
| 20 | 20 |
name := fmt.Sprintf("b#%02d:", i)
|
| 21 | 21 |
bar := p.AddBar(100, mpb.BarID(i), mpb.PrependDecorators(
|
| 22 | 22 |
func(s *decor.Statistics, _ chan<- int, _ <-chan int) string {
|
| 23 | |
if s.ID == 2 && s.Current >= 42 {
|
|
23 |
if s.ID == 2 && s.Current == 42 {
|
| 24 | 24 |
panic(wantPanic)
|
| 25 | 25 |
}
|
| 26 | 26 |
return name
|
| 36 | 36 |
)
|
| 37 | 37 |
go func() {
|
| 38 | 38 |
defer wg.Done()
|
|
39 |
max := 200 * time.Millisecond
|
| 39 | 40 |
for i := 0; i < total; i++ {
|
| 40 | |
time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10)
|
|
41 |
time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
|
| 41 | 42 |
b.Increment()
|
| 42 | 43 |
}
|
| 43 | 44 |
}()
|
| 37 | 37 |
)
|
| 38 | 38 |
go func() {
|
| 39 | 39 |
defer wg.Done()
|
|
40 |
max := 200 * time.Millisecond
|
| 40 | 41 |
for i := 0; i < total; i++ {
|
| 41 | |
time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10)
|
|
42 |
time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
|
| 42 | 43 |
b.Increment()
|
| 43 | 44 |
}
|
| 44 | 45 |
}()
|
| 36 | 36 |
)
|
| 37 | 37 |
go func() {
|
| 38 | 38 |
defer wg.Done()
|
|
39 |
max := 200 * time.Millisecond
|
| 39 | 40 |
for i := 0; i < total; i++ {
|
| 40 | |
time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10)
|
|
41 |
time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
|
| 41 | 42 |
b.Increment()
|
| 42 | 43 |
if b.ID() == 1 && i == 42 {
|
| 43 | 44 |
p.RemoveBar(b)
|
| 36 | 36 |
)
|
| 37 | 37 |
go func() {
|
| 38 | 38 |
defer wg.Done()
|
|
39 |
max := 200 * time.Millisecond
|
| 39 | 40 |
for i := 0; i < total; i++ {
|
| 40 | |
time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10)
|
|
41 |
time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
|
| 41 | 42 |
if i&1 == 1 {
|
| 42 | 43 |
priority := total - int(b.Current())
|
| 43 | 44 |
p.UpdateBarPriority(b, priority)
|
| 38 | 38 |
|
| 39 | 39 |
go func() {
|
| 40 | 40 |
defer wg.Done()
|
|
41 |
max := 300 * time.Millisecond
|
| 41 | 42 |
for i := 0; i < total; i++ {
|
| 42 | |
time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10)
|
|
43 |
time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
|
| 43 | 44 |
bar.Increment()
|
| 44 | 45 |
}
|
| 45 | 46 |
}()
|