Refactoring: Output to WithOutput
Vladimir Bauer
8 years ago
| 13 | 13 |
)
|
| 14 | 14 |
|
| 15 | 15 |
func TestBarCompleted(t *testing.T) {
|
| 16 | |
p := mpb.New(mpb.Output(ioutil.Discard))
|
|
16 |
p := mpb.New(mpb.WithOutput(ioutil.Discard))
|
| 17 | 17 |
total := 80
|
| 18 | 18 |
bar := p.AddBar(int64(total))
|
| 19 | 19 |
|
|
| 31 | 31 |
}
|
| 32 | 32 |
|
| 33 | 33 |
func TestBarID(t *testing.T) {
|
| 34 | |
p := mpb.New(mpb.Output(ioutil.Discard))
|
|
34 |
p := mpb.New(mpb.WithOutput(ioutil.Discard))
|
| 35 | 35 |
|
| 36 | 36 |
numBars := 3
|
| 37 | 37 |
bars := make([]*mpb.Bar, numBars)
|
|
| 59 | 59 |
|
| 60 | 60 |
width := 100
|
| 61 | 61 |
p := mpb.New(
|
| 62 | |
mpb.Output(&buf),
|
|
62 |
mpb.WithOutput(&buf),
|
| 63 | 63 |
mpb.WithWidth(width),
|
| 64 | 64 |
)
|
| 65 | 65 |
|
|
| 90 | 90 |
func TestBarPanics(t *testing.T) {
|
| 91 | 91 |
var wg sync.WaitGroup
|
| 92 | 92 |
var buf bytes.Buffer
|
| 93 | |
p := mpb.New(mpb.Output(&buf), mpb.WithWaitGroup(&wg))
|
|
93 |
p := mpb.New(mpb.WithOutput(&buf), mpb.WithWaitGroup(&wg))
|
| 94 | 94 |
|
| 95 | 95 |
wantPanic := "Upps!!!"
|
| 96 | 96 |
numBars := 3
|
| 5 | 5 |
)
|
| 6 | 6 |
|
| 7 | 7 |
func benchmarkSingleBar(total int) {
|
| 8 | |
p := New(Output(ioutil.Discard))
|
|
8 |
p := New(WithOutput(ioutil.Discard))
|
| 9 | 9 |
bar := p.AddBar(int64(total))
|
| 10 | 10 |
for i := 0; i < total; i++ {
|
| 11 | 11 |
bar.Increment()
|
|
| 32 | 32 |
}
|
| 33 | 33 |
|
| 34 | 34 |
func BenchmarkIncrSingleBar(b *testing.B) {
|
| 35 | |
p := New(Output(ioutil.Discard))
|
|
35 |
p := New(WithOutput(ioutil.Discard))
|
| 36 | 36 |
bar := p.AddBar(int64(b.N))
|
| 37 | 37 |
for i := 0; i < b.N; i++ {
|
| 38 | 38 |
bar.Increment()
|
| 68 | 68 |
}
|
| 69 | 69 |
}
|
| 70 | 70 |
|
| 71 | |
// Output overrides default output os.Stdout
|
| 72 | |
func Output(w io.Writer) ProgressOption {
|
|
71 |
// WithOutput overrides default output os.Stdout
|
|
72 |
func WithOutput(w io.Writer) ProgressOption {
|
| 73 | 73 |
return func(s *pState) {
|
| 74 | 74 |
if w == nil {
|
| 75 | 75 |
w = ioutil.Discard
|
|
| 78 | 78 |
}
|
| 79 | 79 |
}
|
| 80 | 80 |
|
| 81 | |
// OutputInterceptors provides a way to write to the underlying progress pool's
|
|
81 |
// WithInterceptors provides a way to write to the underlying progress pool's
|
| 82 | 82 |
// writer. Could be useful if you want to output something below the bars, while
|
| 83 | 83 |
// they're rendering.
|
| 84 | |
func OutputInterceptors(interseptors ...func(io.Writer)) ProgressOption {
|
|
84 |
func WithInterceptors(interseptors ...func(io.Writer)) ProgressOption {
|
| 85 | 85 |
return func(s *pState) {
|
| 86 | 86 |
s.interceptors = interseptors
|
| 87 | 87 |
}
|
| 14 | 14 |
ctx, cancel := context.WithCancel(context.Background())
|
| 15 | 15 |
shutdown := make(chan struct{})
|
| 16 | 16 |
p := mpb.New(
|
| 17 | |
mpb.Output(ioutil.Discard),
|
|
17 |
mpb.WithOutput(ioutil.Discard),
|
| 18 | 18 |
mpb.WithContext(ctx),
|
| 19 | 19 |
mpb.WithShutdownNotifier(shutdown),
|
| 20 | 20 |
)
|
| 17 | 17 |
}
|
| 18 | 18 |
|
| 19 | 19 |
func TestBarCount(t *testing.T) {
|
| 20 | |
p := mpb.New(mpb.Output(ioutil.Discard))
|
|
20 |
p := mpb.New(mpb.WithOutput(ioutil.Discard))
|
| 21 | 21 |
|
| 22 | 22 |
var wg sync.WaitGroup
|
| 23 | 23 |
wg.Add(1)
|
|
| 43 | 43 |
}
|
| 44 | 44 |
|
| 45 | 45 |
func TestBarAbort(t *testing.T) {
|
| 46 | |
p := mpb.New(mpb.Output(ioutil.Discard))
|
|
46 |
p := mpb.New(mpb.WithOutput(ioutil.Discard))
|
| 47 | 47 |
|
| 48 | 48 |
var wg sync.WaitGroup
|
| 49 | 49 |
wg.Add(1)
|
|
| 77 | 77 |
cancel := make(chan struct{})
|
| 78 | 78 |
shutdown := make(chan struct{})
|
| 79 | 79 |
p := mpb.New(
|
| 80 | |
mpb.Output(ioutil.Discard),
|
|
80 |
mpb.WithOutput(ioutil.Discard),
|
| 81 | 81 |
mpb.WithCancel(cancel),
|
| 82 | 82 |
mpb.WithShutdownNotifier(shutdown),
|
| 83 | 83 |
)
|
|
| 117 | 117 |
cancel := make(chan struct{})
|
| 118 | 118 |
customFormat := "╢▌▌░╟"
|
| 119 | 119 |
p := mpb.New(
|
| 120 | |
mpb.Output(&buf),
|
|
120 |
mpb.WithOutput(&buf),
|
| 121 | 121 |
mpb.WithCancel(cancel),
|
| 122 | 122 |
mpb.WithFormat(customFormat),
|
| 123 | 123 |
)
|
|
| 151 | 151 |
customWidth := 60
|
| 152 | 152 |
customFormat := "(#>=_)"
|
| 153 | 153 |
p := mpb.New(
|
| 154 | |
mpb.Output(&buf),
|
|
154 |
mpb.WithOutput(&buf),
|
| 155 | 155 |
mpb.WithWidth(customWidth),
|
| 156 | 156 |
mpb.WithFormat(customFormat),
|
| 157 | 157 |
)
|
| 21 | 21 |
|
| 22 | 22 |
func TestProxyReader(t *testing.T) {
|
| 23 | 23 |
var buf bytes.Buffer
|
| 24 | |
p := mpb.New(mpb.Output(&buf))
|
|
24 |
p := mpb.New(mpb.WithOutput(&buf))
|
| 25 | 25 |
|
| 26 | 26 |
reader := strings.NewReader(content)
|
| 27 | 27 |
|
|
| 49 | 49 |
|
| 50 | 50 |
func TestProxyReaderCloser(t *testing.T) {
|
| 51 | 51 |
var buf bytes.Buffer
|
| 52 | |
p := mpb.New(mpb.Output(&buf))
|
|
52 |
p := mpb.New(mpb.WithOutput(&buf))
|
| 53 | 53 |
|
| 54 | 54 |
ts := setupTestHttpServer(content)
|
| 55 | 55 |
defer ts.Close()
|