| 1 |
package uiprogress_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math/rand" |
| 6 |
"runtime" |
| 7 |
"sync" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/gosuri/uiprogress" |
| 11 |
) |
| 12 |
|
| 13 |
func Example() { |
| 14 |
uiprogress.Start() // start rendering |
| 15 |
bar := uiprogress.AddBar(100) // Add a new bar |
| 16 |
|
| 17 |
// optionally, append and prepend completion and elapsed time |
| 18 |
bar.AppendCompleted() |
| 19 |
bar.PrependElapsed() |
| 20 |
|
| 21 |
for bar.Incr() { |
| 22 |
time.Sleep(time.Millisecond * 20) |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
func ExampleProgress_AddBar() { |
| 27 |
waitTime := time.Millisecond * 100 |
| 28 |
uiprogress.Start() |
| 29 |
// start the progress bars in go routines |
| 30 |
var wg sync.WaitGroup |
| 31 |
|
| 32 |
bar1 := uiprogress.AddBar(20).AppendCompleted().PrependElapsed() |
| 33 |
wg.Add(1) |
| 34 |
go func() { |
| 35 |
defer wg.Done() |
| 36 |
for bar1.Incr() { |
| 37 |
time.Sleep(waitTime) |
| 38 |
} |
| 39 |
}() |
| 40 |
|
| 41 |
bar2 := uiprogress.AddBar(40).AppendCompleted().PrependElapsed() |
| 42 |
wg.Add(1) |
| 43 |
go func() { |
| 44 |
defer wg.Done() |
| 45 |
for bar2.Incr() { |
| 46 |
time.Sleep(waitTime) |
| 47 |
} |
| 48 |
}() |
| 49 |
|
| 50 |
time.Sleep(time.Second) |
| 51 |
bar3 := uiprogress.AddBar(20).PrependElapsed().AppendCompleted() |
| 52 |
wg.Add(1) |
| 53 |
go func() { |
| 54 |
defer wg.Done() |
| 55 |
for i := 1; i <= bar3.Total; i++ { |
| 56 |
bar3.Set(i) |
| 57 |
time.Sleep(waitTime) |
| 58 |
} |
| 59 |
}() |
| 60 |
// wait for all the go routines to finish |
| 61 |
wg.Wait() |
| 62 |
} |
| 63 |
|
| 64 |
func ExampleDecoratorFunc() { |
| 65 |
var steps = []string{"downloading source", "installing deps", "compiling", "packaging", "seeding database", "deploying", "staring servers"} |
| 66 |
bar := uiprogress.AddBar(len(steps)) |
| 67 |
|
| 68 |
// prepend the current step to the bar |
| 69 |
bar.PrependFunc(func(b *uiprogress.Bar) string { |
| 70 |
return "app: " + steps[b.Current()-1] |
| 71 |
}) |
| 72 |
|
| 73 |
for bar.Incr() { |
| 74 |
time.Sleep(time.Millisecond) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func ExampleBar_Incr() { |
| 79 |
runtime.GOMAXPROCS(runtime.NumCPU()) // use all available cpu cores |
| 80 |
|
| 81 |
// create a new bar and prepend the task progress to the bar |
| 82 |
count := 1000 |
| 83 |
bar := uiprogress.AddBar(count).AppendCompleted().PrependElapsed() |
| 84 |
bar.PrependFunc(func(b *uiprogress.Bar) string { |
| 85 |
return fmt.Sprintf("Task (%d/%d)", b.Current(), count) |
| 86 |
}) |
| 87 |
|
| 88 |
uiprogress.Start() |
| 89 |
var wg sync.WaitGroup |
| 90 |
|
| 91 |
// fanout into 1k go routines |
| 92 |
for i := 0; i < count; i++ { |
| 93 |
wg.Add(1) |
| 94 |
go func() { |
| 95 |
defer wg.Done() |
| 96 |
time.Sleep(time.Millisecond * time.Duration(rand.Intn(500))) |
| 97 |
bar.Incr() |
| 98 |
}() |
| 99 |
} |
| 100 |
time.Sleep(time.Second) // wait for a second for all the go routines to finish |
| 101 |
wg.Wait() |
| 102 |
uiprogress.Stop() |
| 103 |
} |