diff --git a/README.md b/README.md index b4c62ed..71f5cfc 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ total := 100 name := "Single Bar:" - // Adding a single bar + // adding a single bar bar := p.AddBar(int64(total), mpb.PrependDecorators( // Display our static name with one space on the right @@ -53,14 +53,14 @@ ), ) - // Simulating some work + // simulating some work max := 100 * time.Millisecond for i := 0; i < total; i++ { time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) - // Increment by 1 (there is bar.IncrBy(int) method, if needed) + // increment by 1 (there is bar.IncrBy(int) method, if needed) bar.Increment() } - // Wait for our bar to complete and flush + // wait for our bar to complete and flush p.Wait() ``` @@ -85,7 +85,7 @@ decor.OnComplete(decor.ETA(3, 0), "done!", 0, 0), ), ) - // Simulating some work + // simulating some work go func() { defer wg.Done() max := 100 * time.Millisecond @@ -95,8 +95,8 @@ } }() } - // First wait for provided wg, - // then wait for all bars to complete and flush. + // first wait for provided wg, then + // wait for all bars to complete and flush p.Wait() ``` diff --git a/example_test.go b/example_test.go index db768cf..892347a 100644 --- a/example_test.go +++ b/example_test.go @@ -1,7 +1,9 @@ package mpb_test import ( + "io" "math/rand" + "net/http" "time" "github.com/vbauerster/mpb" @@ -20,30 +22,28 @@ total := 100 name := "Single Bar:" - // Add a bar - // You're not limited to just a single bar, add as many as you need + // adding a single bar bar := p.AddBar(int64(total), - // Prepending decorators mpb.PrependDecorators( - // StaticName decorator with minWidth and no extra config - // If you need to change name while rendering, use DynamicName - decor.StaticName(name, len(name), 0), - // ETA decorator with minWidth and no extra config - decor.ETA(4, 0), + // Display our static name with one space on the right + decor.StaticName(name, len(name)+1, decor.DidentRight), + // ETA decorator with width reservation of 3 runes + decor.ETA(3, 0), ), - // Appending decorators mpb.AppendDecorators( - // Percentage decorator with minWidth and no extra config + // Percentage decorator with width reservation of 5 runes decor.Percentage(5, 0), ), ) + // simulating some work max := 100 * time.Millisecond for i := 0; i < total; i++ { time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) + // increment by 1 (there is bar.IncrBy(int) method, if needed) bar.Increment() } - // Wait for all bars to complete + // wait for our bar to complete and flush p.Wait() } @@ -59,3 +59,26 @@ p.Wait() } + +func ExampleBar_ProxyReader() { + p := mpb.New() + // make http get request + resp, _ := http.Get(url) + if err != nil { + return err + } + defer resp.Body.Close() + + // Assuming ContentLength > 0 + bar := p.AddBar(resp.ContentLength, + decor.CountersKibiByte("%6.1f / %6.1f", 12, 0), + ) + + // create proxy reader + reader := bar.ProxyReader(resp.Body) + + // and copy from reader, ignoring errors + io.Copy(dest, reader) + + p.Wait() +} diff --git a/examples/io/single/main.go b/examples/io/single/main.go index 4984012..d8f85e0 100644 --- a/examples/io/single/main.go +++ b/examples/io/single/main.go @@ -41,7 +41,8 @@ bar := p.AddBar(size, mpb.PrependDecorators( decor.CountersKibiByte("% 6.1f / % 6.1f", 18, 0), - )) + ), + ) // create proxy reader reader := bar.ProxyReader(resp.Body) @@ -49,6 +50,5 @@ // and copy from reader, ignoring errors io.Copy(dest, reader) - p.Wait() // if you omit this line, rendering bars goroutine will quit early - fmt.Println("done") + p.Wait() } diff --git a/examples/simple/main.go b/examples/simple/main.go index ac0c776..9748667 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -30,11 +30,11 @@ decor.Percentage(0, decor.DwidthSync), ), mpb.AppendDecorators( - // Replace our ETA decorator with "done!", on bar completion event + // replace our ETA decorator with "done!", on bar completion event decor.OnComplete(decor.ETA(3, 0), "done!", 0, 0), ), ) - // Simulating some work + // simulating some work go func() { defer wg.Done() max := 100 * time.Millisecond @@ -44,7 +44,7 @@ } }() } - // First wait for provided wg, - // then wait for all bars to complete and flush. + // first wait for provided wg, then + // wait for all bars to complete and flush p.Wait() } diff --git a/examples/singleBar/main.go b/examples/singleBar/main.go index 50b4b44..4b7ac39 100644 --- a/examples/singleBar/main.go +++ b/examples/singleBar/main.go @@ -20,7 +20,7 @@ total := 100 name := "Single Bar:" - // Adding a single bar + // adding a single bar bar := p.AddBar(int64(total), mpb.PrependDecorators( // Display our static name with one space on the right @@ -34,13 +34,13 @@ ), ) - // Simulating some work + // simulating some work max := 100 * time.Millisecond for i := 0; i < total; i++ { time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) // Increment by 1 (there is bar.IncrBy(int) method, if needed) bar.Increment() } - // Wait for our bar to complete and flush + // wait for our bar to complete and flush p.Wait() }