| 9 | 9 |
"unicode/utf8"
|
| 10 | 10 |
|
| 11 | 11 |
"github.com/vbauerster/mpb"
|
|
12 |
"github.com/vbauerster/mpb/decor"
|
| 12 | 13 |
)
|
| 13 | 14 |
|
| 14 | 15 |
func TestDefaultWidth(t *testing.T) {
|
| 15 | 16 |
var buf bytes.Buffer
|
| 16 | |
p := mpb.New().SetOut(&buf)
|
| 17 | |
bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace()
|
|
17 |
p := mpb.New(mpb.Output(&buf))
|
|
18 |
bar := p.AddBar(100, mpb.BarTrim())
|
|
19 |
|
| 18 | 20 |
for i := 0; i < 100; i++ {
|
| 19 | 21 |
bar.Incr(1)
|
| 20 | 22 |
}
|
|
| 30 | 32 |
func TestCustomWidth(t *testing.T) {
|
| 31 | 33 |
wantWidth := 60
|
| 32 | 34 |
var buf bytes.Buffer
|
| 33 | |
p := mpb.New().SetWidth(wantWidth).SetOut(&buf)
|
| 34 | |
bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace()
|
|
35 |
p := mpb.New(mpb.WithWidth(wantWidth), mpb.Output(&buf))
|
|
36 |
bar := p.AddBar(100, mpb.BarTrim())
|
|
37 |
|
| 35 | 38 |
for i := 0; i < 100; i++ {
|
| 36 | 39 |
bar.Incr(1)
|
| 37 | 40 |
}
|
|
| 45 | 48 |
|
| 46 | 49 |
func TestInvalidWidth(t *testing.T) {
|
| 47 | 50 |
var buf bytes.Buffer
|
| 48 | |
p := mpb.New().SetWidth(1).SetOut(&buf)
|
| 49 | |
bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace()
|
|
51 |
p := mpb.New(mpb.WithWidth(1), mpb.Output(&buf))
|
|
52 |
bar := p.AddBar(100, mpb.BarTrim())
|
|
53 |
|
| 50 | 54 |
for i := 0; i < 100; i++ {
|
| 51 | 55 |
bar.Incr(1)
|
| 52 | 56 |
}
|
|
| 62 | 66 |
func TestAddBar(t *testing.T) {
|
| 63 | 67 |
var wg sync.WaitGroup
|
| 64 | 68 |
var buf bytes.Buffer
|
| 65 | |
p := mpb.New().SetOut(&buf)
|
|
69 |
p := mpb.New(mpb.Output(&buf))
|
| 66 | 70 |
|
| 67 | 71 |
count := p.BarCount()
|
| 68 | 72 |
if count != 0 {
|
|
| 74 | 78 |
|
| 75 | 79 |
for i := 0; i < numBars; i++ {
|
| 76 | 80 |
name := fmt.Sprintf("Bar#%d:", i)
|
| 77 | |
bar := p.AddBar(100).PrependName(name, len(name), 0)
|
|
81 |
bar := p.AddBar(100, mpb.PrependDecorators(decor.Name(name, len(name), 0)))
|
| 78 | 82 |
|
| 79 | 83 |
go func() {
|
| 80 | 84 |
defer wg.Done()
|
|
| 120 | 124 |
|
| 121 | 125 |
for i := 0; i < numBars; i++ {
|
| 122 | 126 |
name := fmt.Sprintf("Bar#%d:", i)
|
| 123 | |
bar := p.AddBarWithID(i, int64(total)).PrependName(name, len(name), 0)
|
|
127 |
bar := p.AddBar(int64(total), mpb.BarID(i),
|
|
128 |
mpb.PrependDecorators(decor.Name(name, len(name), 0)))
|
| 124 | 129 |
|
| 125 | 130 |
go func() {
|
| 126 | 131 |
defer func() {
|
|
| 153 | 158 |
}
|
| 154 | 159 |
}
|
| 155 | 160 |
|
| 156 | |
func TestWithNilCancel(t *testing.T) {
|
| 157 | |
defer func() {
|
| 158 | |
if p := recover(); p != nil {
|
| 159 | |
if msg, ok := p.(string); ok && msg == "nil cancel channel" {
|
| 160 | |
return
|
| 161 | |
}
|
| 162 | |
t.Errorf("Expected nil channel panic, got: %+v", p)
|
| 163 | |
}
|
| 164 | |
}()
|
| 165 | |
_ = mpb.New().WithCancel(nil)
|
| 166 | |
}
|
| 167 | |
|
| 168 | 161 |
func TestCustomFormat(t *testing.T) {
|
| 169 | 162 |
var buf bytes.Buffer
|
| 170 | 163 |
cancel := make(chan struct{})
|
| 171 | 164 |
customFormat := "╢▌▌░╟"
|
| 172 | |
p := mpb.New().Format(customFormat).
|
| 173 | |
WithCancel(cancel).
|
| 174 | |
SetOut(&buf)
|
| 175 | |
bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace()
|
|
165 |
p := mpb.New(mpb.Output(&buf), mpb.WithCancel(cancel), mpb.WithFormat(customFormat))
|
|
166 |
bar := p.AddBar(100, mpb.BarTrim())
|
| 176 | 167 |
|
| 177 | 168 |
go func() {
|
| 178 | 169 |
for i := 0; i < 100; i++ {
|