Rename example to examples
Vladimir Bauer
9 years ago
| 0 | //+build go1.7 | |
| 1 | ||
| 2 | package main | |
| 3 | ||
| 4 | import ( | |
| 5 | "context" | |
| 6 | "fmt" | |
| 7 | "math/rand" | |
| 8 | "sync" | |
| 9 | "time" | |
| 10 | ||
| 11 | "github.com/vbauerster/mpb" | |
| 12 | "github.com/vbauerster/mpb/decor" | |
| 13 | ) | |
| 14 | ||
| 15 | const ( | |
| 16 | maxBlockSize = 12 | |
| 17 | ) | |
| 18 | ||
| 19 | func main() { | |
| 20 | ||
| 21 | ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) | |
| 22 | defer cancel() | |
| 23 | ||
| 24 | p := mpb.New(mpb.WithContext(ctx)) | |
| 25 | ||
| 26 | var wg sync.WaitGroup | |
| 27 | total := 100 | |
| 28 | numBars := 3 | |
| 29 | wg.Add(numBars) | |
| 30 | ||
| 31 | for i := 0; i < numBars; i++ { | |
| 32 | name := fmt.Sprintf("Bar#%d:", i) | |
| 33 | bar := p.AddBar(int64(total), mpb.BarID(i), | |
| 34 | mpb.PrependDecorators( | |
| 35 | decor.Name(name, 0, decor.DwidthSync|decor.DidentRight), | |
| 36 | decor.ETA(4, decor.DSyncSpace), | |
| 37 | ), | |
| 38 | mpb.AppendDecorators( | |
| 39 | decor.Percentage(5, 0), | |
| 40 | ), | |
| 41 | ) | |
| 42 | go func() { | |
| 43 | defer wg.Done() | |
| 44 | blockSize := rand.Intn(maxBlockSize) + 1 | |
| 45 | for i := 0; i < total; i++ { | |
| 46 | select { | |
| 47 | case <-ctx.Done(): | |
| 48 | return | |
| 49 | default: | |
| 50 | } | |
| 51 | sleep(blockSize) | |
| 52 | bar.Incr(1) | |
| 53 | blockSize = rand.Intn(maxBlockSize) + 1 | |
| 54 | } | |
| 55 | }() | |
| 56 | } | |
| 57 | ||
| 58 | wg.Wait() | |
| 59 | p.Stop() | |
| 60 | fmt.Println("stop") | |
| 61 | } | |
| 62 | ||
| 63 | func sleep(blockSize int) { | |
| 64 | time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) | |
| 65 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "io" | |
| 5 | "log" | |
| 6 | "net/http" | |
| 7 | "os" | |
| 8 | "path/filepath" | |
| 9 | "sync" | |
| 10 | ||
| 11 | "github.com/vbauerster/mpb" | |
| 12 | "github.com/vbauerster/mpb/decor" | |
| 13 | ) | |
| 14 | ||
| 15 | func main() { | |
| 16 | log.SetOutput(os.Stderr) | |
| 17 | ||
| 18 | url1 := "https://homebrew.bintray.com/bottles/youtube-dl-2016.12.12.sierra.bottle.tar.gz" | |
| 19 | url2 := "https://homebrew.bintray.com/bottles/libtiff-4.0.7.sierra.bottle.tar.gz" | |
| 20 | ||
| 21 | var wg sync.WaitGroup | |
| 22 | p := mpb.New(mpb.WithWidth(64)) | |
| 23 | ||
| 24 | for i, url := range [...]string{url1, url2} { | |
| 25 | wg.Add(1) | |
| 26 | name := fmt.Sprintf("url%d:", i+1) | |
| 27 | go download(&wg, p, name, url) | |
| 28 | } | |
| 29 | ||
| 30 | wg.Wait() | |
| 31 | p.Stop() | |
| 32 | fmt.Println("Finished") | |
| 33 | } | |
| 34 | ||
| 35 | func download(wg *sync.WaitGroup, p *mpb.Progress, name, url string) { | |
| 36 | defer wg.Done() | |
| 37 | resp, err := http.Get(url) | |
| 38 | if err != nil { | |
| 39 | log.Printf("%s: %v", name, err) | |
| 40 | return | |
| 41 | } | |
| 42 | defer resp.Body.Close() | |
| 43 | ||
| 44 | if resp.StatusCode != http.StatusOK { | |
| 45 | err = fmt.Errorf("non-200 status: %s", resp.Status) | |
| 46 | log.Printf("%s: %v", name, err) | |
| 47 | return | |
| 48 | } | |
| 49 | ||
| 50 | size := resp.ContentLength | |
| 51 | ||
| 52 | // create dest | |
| 53 | destName := filepath.Base(url) | |
| 54 | dest, err := os.Create(destName) | |
| 55 | if err != nil { | |
| 56 | err = fmt.Errorf("Can't create %s: %v", destName, err) | |
| 57 | log.Printf("%s: %v", name, err) | |
| 58 | return | |
| 59 | } | |
| 60 | ||
| 61 | // create bar with appropriate decorators | |
| 62 | bar := p.AddBar(size, | |
| 63 | mpb.PrependDecorators( | |
| 64 | decor.Name(name, 0, 0), | |
| 65 | decor.Counters("%3s / %3s", decor.Unit_KiB, 18, 0), | |
| 66 | ), | |
| 67 | mpb.AppendDecorators(decor.ETA(5, decor.DwidthSync)), | |
| 68 | ) | |
| 69 | ||
| 70 | // create proxy reader | |
| 71 | reader := bar.ProxyReader(resp.Body) | |
| 72 | // and copy from reader | |
| 73 | _, err = io.Copy(dest, reader) | |
| 74 | ||
| 75 | if closeErr := dest.Close(); err == nil { | |
| 76 | err = closeErr | |
| 77 | } | |
| 78 | if err != nil { | |
| 79 | log.Printf("%s: %v", name, err) | |
| 80 | } | |
| 81 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "io" | |
| 5 | "net/http" | |
| 6 | "os" | |
| 7 | "path/filepath" | |
| 8 | ||
| 9 | "github.com/vbauerster/mpb" | |
| 10 | "github.com/vbauerster/mpb/decor" | |
| 11 | ) | |
| 12 | ||
| 13 | func main() { | |
| 14 | url := "https://homebrew.bintray.com/bottles/libtiff-4.0.7.sierra.bottle.tar.gz" | |
| 15 | ||
| 16 | resp, err := http.Get(url) | |
| 17 | if err != nil { | |
| 18 | panic(err) | |
| 19 | } | |
| 20 | defer resp.Body.Close() | |
| 21 | ||
| 22 | if resp.StatusCode != http.StatusOK { | |
| 23 | fmt.Printf("Server return non-200 status: %s\n", resp.Status) | |
| 24 | return | |
| 25 | } | |
| 26 | ||
| 27 | size := resp.ContentLength | |
| 28 | ||
| 29 | // create dest | |
| 30 | destName := filepath.Base(url) | |
| 31 | dest, err := os.Create(destName) | |
| 32 | if err != nil { | |
| 33 | fmt.Printf("Can't create %s: %v\n", destName, err) | |
| 34 | return | |
| 35 | } | |
| 36 | defer dest.Close() | |
| 37 | ||
| 38 | p := mpb.New(mpb.WithWidth(64)) | |
| 39 | ||
| 40 | bar := p.AddBar(size, | |
| 41 | mpb.PrependDecorators( | |
| 42 | decor.Counters("%3s / %3s", decor.Unit_KiB, 18, 0), | |
| 43 | )) | |
| 44 | ||
| 45 | // create proxy reader | |
| 46 | reader := bar.ProxyReader(resp.Body) | |
| 47 | ||
| 48 | // and copy from reader, ignoring errors | |
| 49 | io.Copy(dest, reader) | |
| 50 | ||
| 51 | p.Stop() // if you omit this line, rendering bars goroutine will quit early | |
| 52 | fmt.Println("Finished") | |
| 53 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "sync" | |
| 6 | "time" | |
| 7 | ||
| 8 | "github.com/vbauerster/mpb" | |
| 9 | "github.com/vbauerster/mpb/decor" | |
| 10 | ) | |
| 11 | ||
| 12 | const ( | |
| 13 | maxBlockSize = 12 | |
| 14 | ) | |
| 15 | ||
| 16 | func main() { | |
| 17 | ||
| 18 | p := mpb.New(mpb.WithWidth(64)) | |
| 19 | ||
| 20 | total := 100 | |
| 21 | numBars := 3 | |
| 22 | var wg sync.WaitGroup | |
| 23 | wg.Add(numBars) | |
| 24 | ||
| 25 | for i := 0; i < numBars; i++ { | |
| 26 | var name string | |
| 27 | if i != 1 { | |
| 28 | name = fmt.Sprintf("Bar#%d:", i) | |
| 29 | } | |
| 30 | b := p.AddBar(int64(total), | |
| 31 | mpb.PrependDecorators( | |
| 32 | decor.Name(name, 0, decor.DwidthSync|decor.DidentRight), | |
| 33 | decor.ETA(4, decor.DSyncSpace), | |
| 34 | ), | |
| 35 | mpb.AppendDecorators( | |
| 36 | decor.Percentage(5, 0), | |
| 37 | ), | |
| 38 | ) | |
| 39 | go func() { | |
| 40 | defer wg.Done() | |
| 41 | blockSize := rand.Intn(maxBlockSize) + 1 | |
| 42 | for i := 0; i < total; i++ { | |
| 43 | sleep(blockSize) | |
| 44 | b.Incr(1) | |
| 45 | blockSize = rand.Intn(maxBlockSize) + 1 | |
| 46 | } | |
| 47 | }() | |
| 48 | } | |
| 49 | ||
| 50 | wg.Wait() | |
| 51 | p.Stop() | |
| 52 | fmt.Println("stop") | |
| 53 | } | |
| 54 | ||
| 55 | func sleep(blockSize int) { | |
| 56 | time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) | |
| 57 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "sync" | |
| 6 | "time" | |
| 7 | ||
| 8 | "github.com/vbauerster/mpb" | |
| 9 | "github.com/vbauerster/mpb/decor" | |
| 10 | ) | |
| 11 | ||
| 12 | const ( | |
| 13 | maxBlockSize = 12 | |
| 14 | ) | |
| 15 | ||
| 16 | func main() { | |
| 17 | ||
| 18 | p := mpb.New(mpb.WithWidth(64)) | |
| 19 | ||
| 20 | total := 100 | |
| 21 | numBars := 3 | |
| 22 | var wg sync.WaitGroup | |
| 23 | wg.Add(numBars) | |
| 24 | ||
| 25 | for i := 0; i < numBars; i++ { | |
| 26 | var name string | |
| 27 | if i != 1 { | |
| 28 | name = fmt.Sprintf("Bar#%d:", i) | |
| 29 | } | |
| 30 | b := p.AddBar(int64(total), | |
| 31 | mpb.PrependDecorators( | |
| 32 | decor.Name(name, 0, decor.DwidthSync|decor.DidentRight), | |
| 33 | decor.Elapsed(3, decor.DSyncSpace), | |
| 34 | ), | |
| 35 | mpb.AppendDecorators( | |
| 36 | decor.Percentage(5, 0), | |
| 37 | ), | |
| 38 | ) | |
| 39 | go func() { | |
| 40 | defer wg.Done() | |
| 41 | blockSize := rand.Intn(maxBlockSize) + 1 | |
| 42 | for i := 0; i < total; i++ { | |
| 43 | sleep(blockSize) | |
| 44 | b.Incr(1) | |
| 45 | blockSize = rand.Intn(maxBlockSize) + 1 | |
| 46 | } | |
| 47 | }() | |
| 48 | } | |
| 49 | ||
| 50 | wg.Wait() | |
| 51 | p.Stop() | |
| 52 | fmt.Println("stop") | |
| 53 | } | |
| 54 | ||
| 55 | func sleep(blockSize int) { | |
| 56 | time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) | |
| 57 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "sync" | |
| 6 | "time" | |
| 7 | ||
| 8 | "github.com/vbauerster/mpb" | |
| 9 | "github.com/vbauerster/mpb/decor" | |
| 10 | ) | |
| 11 | ||
| 12 | const ( | |
| 13 | maxBlockSize = 12 | |
| 14 | ) | |
| 15 | ||
| 16 | func main() { | |
| 17 | ||
| 18 | p := mpb.New(mpb.WithWidth(64)) | |
| 19 | ||
| 20 | total := 100 | |
| 21 | numBars := 3 | |
| 22 | var wg sync.WaitGroup | |
| 23 | wg.Add(numBars) | |
| 24 | ||
| 25 | for i := 0; i < numBars; i++ { | |
| 26 | var name string | |
| 27 | if i != 1 { | |
| 28 | name = fmt.Sprintf("Bar#%d:", i) | |
| 29 | } | |
| 30 | b := p.AddBar(int64(total), mpb.BarID(i), | |
| 31 | mpb.PrependDecorators( | |
| 32 | decor.Name(name, 0, decor.DwidthSync|decor.DidentRight), | |
| 33 | decor.ETA(4, decor.DSyncSpace), | |
| 34 | ), | |
| 35 | mpb.AppendDecorators( | |
| 36 | decor.Percentage(5, 0), | |
| 37 | ), | |
| 38 | ) | |
| 39 | go func() { | |
| 40 | defer wg.Done() | |
| 41 | blockSize := rand.Intn(maxBlockSize) + 1 | |
| 42 | for i := 0; i < total; i++ { | |
| 43 | sleep(blockSize) | |
| 44 | b.Incr(1) | |
| 45 | blockSize = rand.Intn(maxBlockSize) + 1 | |
| 46 | if b.ID() == 1 && i >= 42 { | |
| 47 | p.RemoveBar(b) | |
| 48 | return | |
| 49 | } | |
| 50 | } | |
| 51 | }() | |
| 52 | } | |
| 53 | ||
| 54 | wg.Wait() | |
| 55 | p.Stop() | |
| 56 | fmt.Println("stop") | |
| 57 | } | |
| 58 | ||
| 59 | func sleep(blockSize int) { | |
| 60 | time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) | |
| 61 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "sync" | |
| 6 | "time" | |
| 7 | ||
| 8 | "github.com/vbauerster/mpb" | |
| 9 | "github.com/vbauerster/mpb/decor" | |
| 10 | ) | |
| 11 | ||
| 12 | func main() { | |
| 13 | p := mpb.New() | |
| 14 | total := 100 | |
| 15 | numBars := 3 | |
| 16 | var wg sync.WaitGroup | |
| 17 | wg.Add(numBars) | |
| 18 | ||
| 19 | for i := 0; i < numBars; i++ { | |
| 20 | name := fmt.Sprintf("Bar#%d:", i) | |
| 21 | bar := p.AddBar(int64(total), | |
| 22 | mpb.PrependDecorators( | |
| 23 | // Name decorator with minWidth and no width sync | |
| 24 | decor.Name(name, len(name), 0), | |
| 25 | // Percentage decorator with DwidthSync and DextraSpace | |
| 26 | decor.Percentage(3, decor.DSyncSpace), | |
| 27 | ), | |
| 28 | mpb.AppendDecorators( | |
| 29 | // ETA decorator, with no width sync | |
| 30 | decor.ETA(2, 0), | |
| 31 | ), | |
| 32 | ) | |
| 33 | ||
| 34 | go func() { | |
| 35 | defer wg.Done() | |
| 36 | for i := 0; i < total; i++ { | |
| 37 | time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) | |
| 38 | bar.Incr(1) | |
| 39 | } | |
| 40 | }() | |
| 41 | } | |
| 42 | wg.Wait() // Wait for goroutines to finish | |
| 43 | p.Stop() // Stop mpb's rendering goroutine | |
| 44 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "time" | |
| 6 | ||
| 7 | "github.com/vbauerster/mpb" | |
| 8 | "github.com/vbauerster/mpb/decor" | |
| 9 | ) | |
| 10 | ||
| 11 | func main() { | |
| 12 | p := mpb.New( | |
| 13 | // Override default (80) width | |
| 14 | mpb.WithWidth(100), | |
| 15 | // Override default "[=>-]" format | |
| 16 | mpb.WithFormat("╢▌▌░╟"), | |
| 17 | // Override default 100ms refresh rate | |
| 18 | mpb.WithRefreshRate(120*time.Millisecond), | |
| 19 | ) | |
| 20 | ||
| 21 | // Add a bar. You're not limited to just one bar, add many if you need. | |
| 22 | bar := p.AddBar(100, | |
| 23 | mpb.PrependDecorators(decor.Name("Single Bar:", 0, 0)), | |
| 24 | mpb.AppendDecorators(decor.Percentage(5, 0)), | |
| 25 | ) | |
| 26 | ||
| 27 | for i := 0; i < 100; i++ { | |
| 28 | bar.Incr(1) // increment progress bar | |
| 29 | time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) | |
| 30 | } | |
| 31 | ||
| 32 | // Don't forget to stop mpb's rendering goroutine | |
| 33 | p.Stop() | |
| 34 | fmt.Println("Stop") | |
| 35 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "sort" | |
| 6 | "sync" | |
| 7 | "time" | |
| 8 | ||
| 9 | "github.com/vbauerster/mpb" | |
| 10 | "github.com/vbauerster/mpb/decor" | |
| 11 | ) | |
| 12 | ||
| 13 | const ( | |
| 14 | maxBlockSize = 12 | |
| 15 | ) | |
| 16 | ||
| 17 | type barSlice []*mpb.Bar | |
| 18 | ||
| 19 | func (bs barSlice) Len() int { return len(bs) } | |
| 20 | ||
| 21 | func (bs barSlice) Less(i, j int) bool { | |
| 22 | ip := decor.CalcPercentage(bs[i].Total(), bs[i].Current(), 100) | |
| 23 | jp := decor.CalcPercentage(bs[j].Total(), bs[j].Current(), 100) | |
| 24 | return ip < jp | |
| 25 | } | |
| 26 | ||
| 27 | func (bs barSlice) Swap(i, j int) { bs[i], bs[j] = bs[j], bs[i] } | |
| 28 | ||
| 29 | func sortByProgressFunc() mpb.BeforeRender { | |
| 30 | return func(bars []*mpb.Bar) { | |
| 31 | sort.Sort(sort.Reverse(barSlice(bars))) | |
| 32 | } | |
| 33 | } | |
| 34 | ||
| 35 | func main() { | |
| 36 | ||
| 37 | p := mpb.New(mpb.WithWidth(64), mpb.WithBeforeRenderFunc(sortByProgressFunc())) | |
| 38 | ||
| 39 | total := 100 | |
| 40 | numBars := 3 | |
| 41 | var wg sync.WaitGroup | |
| 42 | wg.Add(numBars) | |
| 43 | ||
| 44 | for i := 0; i < numBars; i++ { | |
| 45 | var name string | |
| 46 | if i != 1 { | |
| 47 | name = fmt.Sprintf("Bar#%d:", i) | |
| 48 | } | |
| 49 | b := p.AddBar(int64(total), | |
| 50 | mpb.PrependDecorators( | |
| 51 | decor.Name(name, 0, decor.DwidthSync), | |
| 52 | decor.Counters("%3s/%3s", 0, 10, decor.DSyncSpace), | |
| 53 | ), | |
| 54 | mpb.AppendDecorators( | |
| 55 | decor.ETA(3, 0), | |
| 56 | ), | |
| 57 | ) | |
| 58 | go func() { | |
| 59 | defer wg.Done() | |
| 60 | blockSize := rand.Intn(maxBlockSize) + 1 | |
| 61 | for i := 0; i < total; i++ { | |
| 62 | sleep(blockSize) | |
| 63 | b.Incr(1) | |
| 64 | blockSize = rand.Intn(maxBlockSize) + 1 | |
| 65 | } | |
| 66 | }() | |
| 67 | } | |
| 68 | ||
| 69 | wg.Wait() | |
| 70 | p.Stop() | |
| 71 | fmt.Println("stop") | |
| 72 | } | |
| 73 | ||
| 74 | func sleep(blockSize int) { | |
| 75 | time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) | |
| 76 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "sync" | |
| 6 | "time" | |
| 7 | ||
| 8 | "github.com/vbauerster/mpb" | |
| 9 | "github.com/vbauerster/mpb/decor" | |
| 10 | ) | |
| 11 | ||
| 12 | const ( | |
| 13 | totalBars = 32 | |
| 14 | maxBlockSize = 8 | |
| 15 | ) | |
| 16 | ||
| 17 | func main() { | |
| 18 | ||
| 19 | var wg sync.WaitGroup | |
| 20 | p := mpb.New() | |
| 21 | wg.Add(totalBars) | |
| 22 | ||
| 23 | for i := 0; i < totalBars; i++ { | |
| 24 | name := fmt.Sprintf("Bar#%02d: ", i) | |
| 25 | total := rand.Intn(120) + 10 | |
| 26 | bar := p.AddBar(int64(total), | |
| 27 | mpb.PrependDecorators( | |
| 28 | decor.Name(name, len(name), 0), | |
| 29 | decor.ETA(4, decor.DSyncSpace), | |
| 30 | ), | |
| 31 | mpb.AppendDecorators( | |
| 32 | decor.Percentage(5, 0), | |
| 33 | ), | |
| 34 | ) | |
| 35 | ||
| 36 | go func() { | |
| 37 | defer wg.Done() | |
| 38 | blockSize := rand.Intn(maxBlockSize) + 1 | |
| 39 | for i := 0; i < total; i++ { | |
| 40 | sleep(blockSize) | |
| 41 | bar.Incr(1) | |
| 42 | blockSize = rand.Intn(maxBlockSize) + 1 | |
| 43 | } | |
| 44 | }() | |
| 45 | } | |
| 46 | ||
| 47 | wg.Wait() | |
| 48 | p.Stop() | |
| 49 | fmt.Println("stop") | |
| 50 | } | |
| 51 | ||
| 52 | func sleep(blockSize int) { | |
| 53 | time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) | |
| 54 | } |
| 0 | //+build go1.7 | |
| 1 | ||
| 2 | package main | |
| 3 | ||
| 4 | import ( | |
| 5 | "context" | |
| 6 | "fmt" | |
| 7 | "math/rand" | |
| 8 | "sync" | |
| 9 | "time" | |
| 10 | ||
| 11 | "github.com/vbauerster/mpb" | |
| 12 | "github.com/vbauerster/mpb/decor" | |
| 13 | ) | |
| 14 | ||
| 15 | const ( | |
| 16 | maxBlockSize = 12 | |
| 17 | ) | |
| 18 | ||
| 19 | func main() { | |
| 20 | ||
| 21 | ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) | |
| 22 | defer cancel() | |
| 23 | ||
| 24 | p := mpb.New(mpb.WithContext(ctx)) | |
| 25 | ||
| 26 | var wg sync.WaitGroup | |
| 27 | total := 100 | |
| 28 | numBars := 3 | |
| 29 | wg.Add(numBars) | |
| 30 | ||
| 31 | for i := 0; i < numBars; i++ { | |
| 32 | name := fmt.Sprintf("Bar#%d:", i) | |
| 33 | bar := p.AddBar(int64(total), mpb.BarID(i), | |
| 34 | mpb.PrependDecorators( | |
| 35 | decor.Name(name, 0, decor.DwidthSync|decor.DidentRight), | |
| 36 | decor.ETA(4, decor.DSyncSpace), | |
| 37 | ), | |
| 38 | mpb.AppendDecorators( | |
| 39 | decor.Percentage(5, 0), | |
| 40 | ), | |
| 41 | ) | |
| 42 | go func() { | |
| 43 | defer wg.Done() | |
| 44 | blockSize := rand.Intn(maxBlockSize) + 1 | |
| 45 | for i := 0; i < total; i++ { | |
| 46 | select { | |
| 47 | case <-ctx.Done(): | |
| 48 | return | |
| 49 | default: | |
| 50 | } | |
| 51 | sleep(blockSize) | |
| 52 | bar.Incr(1) | |
| 53 | blockSize = rand.Intn(maxBlockSize) + 1 | |
| 54 | } | |
| 55 | }() | |
| 56 | } | |
| 57 | ||
| 58 | wg.Wait() | |
| 59 | p.Stop() | |
| 60 | fmt.Println("stop") | |
| 61 | } | |
| 62 | ||
| 63 | func sleep(blockSize int) { | |
| 64 | time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) | |
| 65 | } |
Binary diff not shown
Binary diff not shown
Binary diff not shown
Binary diff not shown
Binary diff not shown
Binary diff not shown
Binary diff not shown
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "io" | |
| 5 | "log" | |
| 6 | "net/http" | |
| 7 | "os" | |
| 8 | "path/filepath" | |
| 9 | "sync" | |
| 10 | ||
| 11 | "github.com/vbauerster/mpb" | |
| 12 | "github.com/vbauerster/mpb/decor" | |
| 13 | ) | |
| 14 | ||
| 15 | func main() { | |
| 16 | log.SetOutput(os.Stderr) | |
| 17 | ||
| 18 | url1 := "https://homebrew.bintray.com/bottles/youtube-dl-2016.12.12.sierra.bottle.tar.gz" | |
| 19 | url2 := "https://homebrew.bintray.com/bottles/libtiff-4.0.7.sierra.bottle.tar.gz" | |
| 20 | ||
| 21 | var wg sync.WaitGroup | |
| 22 | p := mpb.New(mpb.WithWidth(64)) | |
| 23 | ||
| 24 | for i, url := range [...]string{url1, url2} { | |
| 25 | wg.Add(1) | |
| 26 | name := fmt.Sprintf("url%d:", i+1) | |
| 27 | go download(&wg, p, name, url) | |
| 28 | } | |
| 29 | ||
| 30 | wg.Wait() | |
| 31 | p.Stop() | |
| 32 | fmt.Println("Finished") | |
| 33 | } | |
| 34 | ||
| 35 | func download(wg *sync.WaitGroup, p *mpb.Progress, name, url string) { | |
| 36 | defer wg.Done() | |
| 37 | resp, err := http.Get(url) | |
| 38 | if err != nil { | |
| 39 | log.Printf("%s: %v", name, err) | |
| 40 | return | |
| 41 | } | |
| 42 | defer resp.Body.Close() | |
| 43 | ||
| 44 | if resp.StatusCode != http.StatusOK { | |
| 45 | err = fmt.Errorf("non-200 status: %s", resp.Status) | |
| 46 | log.Printf("%s: %v", name, err) | |
| 47 | return | |
| 48 | } | |
| 49 | ||
| 50 | size := resp.ContentLength | |
| 51 | ||
| 52 | // create dest | |
| 53 | destName := filepath.Base(url) | |
| 54 | dest, err := os.Create(destName) | |
| 55 | if err != nil { | |
| 56 | err = fmt.Errorf("Can't create %s: %v", destName, err) | |
| 57 | log.Printf("%s: %v", name, err) | |
| 58 | return | |
| 59 | } | |
| 60 | ||
| 61 | // create bar with appropriate decorators | |
| 62 | bar := p.AddBar(size, | |
| 63 | mpb.PrependDecorators( | |
| 64 | decor.Name(name, 0, 0), | |
| 65 | decor.Counters("%3s / %3s", decor.Unit_KiB, 18, 0), | |
| 66 | ), | |
| 67 | mpb.AppendDecorators(decor.ETA(5, decor.DwidthSync)), | |
| 68 | ) | |
| 69 | ||
| 70 | // create proxy reader | |
| 71 | reader := bar.ProxyReader(resp.Body) | |
| 72 | // and copy from reader | |
| 73 | _, err = io.Copy(dest, reader) | |
| 74 | ||
| 75 | if closeErr := dest.Close(); err == nil { | |
| 76 | err = closeErr | |
| 77 | } | |
| 78 | if err != nil { | |
| 79 | log.Printf("%s: %v", name, err) | |
| 80 | } | |
| 81 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "io" | |
| 5 | "net/http" | |
| 6 | "os" | |
| 7 | "path/filepath" | |
| 8 | ||
| 9 | "github.com/vbauerster/mpb" | |
| 10 | "github.com/vbauerster/mpb/decor" | |
| 11 | ) | |
| 12 | ||
| 13 | func main() { | |
| 14 | url := "https://homebrew.bintray.com/bottles/libtiff-4.0.7.sierra.bottle.tar.gz" | |
| 15 | ||
| 16 | resp, err := http.Get(url) | |
| 17 | if err != nil { | |
| 18 | panic(err) | |
| 19 | } | |
| 20 | defer resp.Body.Close() | |
| 21 | ||
| 22 | if resp.StatusCode != http.StatusOK { | |
| 23 | fmt.Printf("Server return non-200 status: %s\n", resp.Status) | |
| 24 | return | |
| 25 | } | |
| 26 | ||
| 27 | size := resp.ContentLength | |
| 28 | ||
| 29 | // create dest | |
| 30 | destName := filepath.Base(url) | |
| 31 | dest, err := os.Create(destName) | |
| 32 | if err != nil { | |
| 33 | fmt.Printf("Can't create %s: %v\n", destName, err) | |
| 34 | return | |
| 35 | } | |
| 36 | defer dest.Close() | |
| 37 | ||
| 38 | p := mpb.New(mpb.WithWidth(64)) | |
| 39 | ||
| 40 | bar := p.AddBar(size, | |
| 41 | mpb.PrependDecorators( | |
| 42 | decor.Counters("%3s / %3s", decor.Unit_KiB, 18, 0), | |
| 43 | )) | |
| 44 | ||
| 45 | // create proxy reader | |
| 46 | reader := bar.ProxyReader(resp.Body) | |
| 47 | ||
| 48 | // and copy from reader, ignoring errors | |
| 49 | io.Copy(dest, reader) | |
| 50 | ||
| 51 | p.Stop() // if you omit this line, rendering bars goroutine will quit early | |
| 52 | fmt.Println("Finished") | |
| 53 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "sync" | |
| 6 | "time" | |
| 7 | ||
| 8 | "github.com/vbauerster/mpb" | |
| 9 | "github.com/vbauerster/mpb/decor" | |
| 10 | ) | |
| 11 | ||
| 12 | const ( | |
| 13 | maxBlockSize = 12 | |
| 14 | ) | |
| 15 | ||
| 16 | func main() { | |
| 17 | ||
| 18 | p := mpb.New(mpb.WithWidth(64)) | |
| 19 | ||
| 20 | total := 100 | |
| 21 | numBars := 3 | |
| 22 | var wg sync.WaitGroup | |
| 23 | wg.Add(numBars) | |
| 24 | ||
| 25 | for i := 0; i < numBars; i++ { | |
| 26 | var name string | |
| 27 | if i != 1 { | |
| 28 | name = fmt.Sprintf("Bar#%d:", i) | |
| 29 | } | |
| 30 | b := p.AddBar(int64(total), | |
| 31 | mpb.PrependDecorators( | |
| 32 | decor.Name(name, 0, decor.DwidthSync|decor.DidentRight), | |
| 33 | decor.ETA(4, decor.DSyncSpace), | |
| 34 | ), | |
| 35 | mpb.AppendDecorators( | |
| 36 | decor.Percentage(5, 0), | |
| 37 | ), | |
| 38 | ) | |
| 39 | go func() { | |
| 40 | defer wg.Done() | |
| 41 | blockSize := rand.Intn(maxBlockSize) + 1 | |
| 42 | for i := 0; i < total; i++ { | |
| 43 | sleep(blockSize) | |
| 44 | b.Incr(1) | |
| 45 | blockSize = rand.Intn(maxBlockSize) + 1 | |
| 46 | } | |
| 47 | }() | |
| 48 | } | |
| 49 | ||
| 50 | wg.Wait() | |
| 51 | p.Stop() | |
| 52 | fmt.Println("stop") | |
| 53 | } | |
| 54 | ||
| 55 | func sleep(blockSize int) { | |
| 56 | time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) | |
| 57 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "sync" | |
| 6 | "time" | |
| 7 | ||
| 8 | "github.com/vbauerster/mpb" | |
| 9 | "github.com/vbauerster/mpb/decor" | |
| 10 | ) | |
| 11 | ||
| 12 | const ( | |
| 13 | maxBlockSize = 12 | |
| 14 | ) | |
| 15 | ||
| 16 | func main() { | |
| 17 | ||
| 18 | p := mpb.New(mpb.WithWidth(64)) | |
| 19 | ||
| 20 | total := 100 | |
| 21 | numBars := 3 | |
| 22 | var wg sync.WaitGroup | |
| 23 | wg.Add(numBars) | |
| 24 | ||
| 25 | for i := 0; i < numBars; i++ { | |
| 26 | var name string | |
| 27 | if i != 1 { | |
| 28 | name = fmt.Sprintf("Bar#%d:", i) | |
| 29 | } | |
| 30 | b := p.AddBar(int64(total), | |
| 31 | mpb.PrependDecorators( | |
| 32 | decor.Name(name, 0, decor.DwidthSync|decor.DidentRight), | |
| 33 | decor.Elapsed(3, decor.DSyncSpace), | |
| 34 | ), | |
| 35 | mpb.AppendDecorators( | |
| 36 | decor.Percentage(5, 0), | |
| 37 | ), | |
| 38 | ) | |
| 39 | go func() { | |
| 40 | defer wg.Done() | |
| 41 | blockSize := rand.Intn(maxBlockSize) + 1 | |
| 42 | for i := 0; i < total; i++ { | |
| 43 | sleep(blockSize) | |
| 44 | b.Incr(1) | |
| 45 | blockSize = rand.Intn(maxBlockSize) + 1 | |
| 46 | } | |
| 47 | }() | |
| 48 | } | |
| 49 | ||
| 50 | wg.Wait() | |
| 51 | p.Stop() | |
| 52 | fmt.Println("stop") | |
| 53 | } | |
| 54 | ||
| 55 | func sleep(blockSize int) { | |
| 56 | time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) | |
| 57 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "sync" | |
| 6 | "time" | |
| 7 | ||
| 8 | "github.com/vbauerster/mpb" | |
| 9 | "github.com/vbauerster/mpb/decor" | |
| 10 | ) | |
| 11 | ||
| 12 | const ( | |
| 13 | maxBlockSize = 12 | |
| 14 | ) | |
| 15 | ||
| 16 | func main() { | |
| 17 | ||
| 18 | p := mpb.New(mpb.WithWidth(64)) | |
| 19 | ||
| 20 | total := 100 | |
| 21 | numBars := 3 | |
| 22 | var wg sync.WaitGroup | |
| 23 | wg.Add(numBars) | |
| 24 | ||
| 25 | for i := 0; i < numBars; i++ { | |
| 26 | var name string | |
| 27 | if i != 1 { | |
| 28 | name = fmt.Sprintf("Bar#%d:", i) | |
| 29 | } | |
| 30 | b := p.AddBar(int64(total), mpb.BarID(i), | |
| 31 | mpb.PrependDecorators( | |
| 32 | decor.Name(name, 0, decor.DwidthSync|decor.DidentRight), | |
| 33 | decor.ETA(4, decor.DSyncSpace), | |
| 34 | ), | |
| 35 | mpb.AppendDecorators( | |
| 36 | decor.Percentage(5, 0), | |
| 37 | ), | |
| 38 | ) | |
| 39 | go func() { | |
| 40 | defer wg.Done() | |
| 41 | blockSize := rand.Intn(maxBlockSize) + 1 | |
| 42 | for i := 0; i < total; i++ { | |
| 43 | sleep(blockSize) | |
| 44 | b.Incr(1) | |
| 45 | blockSize = rand.Intn(maxBlockSize) + 1 | |
| 46 | if b.ID() == 1 && i >= 42 { | |
| 47 | p.RemoveBar(b) | |
| 48 | return | |
| 49 | } | |
| 50 | } | |
| 51 | }() | |
| 52 | } | |
| 53 | ||
| 54 | wg.Wait() | |
| 55 | p.Stop() | |
| 56 | fmt.Println("stop") | |
| 57 | } | |
| 58 | ||
| 59 | func sleep(blockSize int) { | |
| 60 | time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) | |
| 61 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "sync" | |
| 6 | "time" | |
| 7 | ||
| 8 | "github.com/vbauerster/mpb" | |
| 9 | "github.com/vbauerster/mpb/decor" | |
| 10 | ) | |
| 11 | ||
| 12 | func main() { | |
| 13 | p := mpb.New() | |
| 14 | total := 100 | |
| 15 | numBars := 3 | |
| 16 | var wg sync.WaitGroup | |
| 17 | wg.Add(numBars) | |
| 18 | ||
| 19 | for i := 0; i < numBars; i++ { | |
| 20 | name := fmt.Sprintf("Bar#%d:", i) | |
| 21 | bar := p.AddBar(int64(total), | |
| 22 | mpb.PrependDecorators( | |
| 23 | // Name decorator with minWidth and no width sync | |
| 24 | decor.Name(name, len(name), 0), | |
| 25 | // Percentage decorator with DwidthSync and DextraSpace | |
| 26 | decor.Percentage(3, decor.DSyncSpace), | |
| 27 | ), | |
| 28 | mpb.AppendDecorators( | |
| 29 | // ETA decorator, with no width sync | |
| 30 | decor.ETA(2, 0), | |
| 31 | ), | |
| 32 | ) | |
| 33 | ||
| 34 | go func() { | |
| 35 | defer wg.Done() | |
| 36 | for i := 0; i < total; i++ { | |
| 37 | time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) | |
| 38 | bar.Incr(1) | |
| 39 | } | |
| 40 | }() | |
| 41 | } | |
| 42 | wg.Wait() // Wait for goroutines to finish | |
| 43 | p.Stop() // Stop mpb's rendering goroutine | |
| 44 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "time" | |
| 6 | ||
| 7 | "github.com/vbauerster/mpb" | |
| 8 | "github.com/vbauerster/mpb/decor" | |
| 9 | ) | |
| 10 | ||
| 11 | func main() { | |
| 12 | p := mpb.New( | |
| 13 | // Override default (80) width | |
| 14 | mpb.WithWidth(100), | |
| 15 | // Override default "[=>-]" format | |
| 16 | mpb.WithFormat("╢▌▌░╟"), | |
| 17 | // Override default 100ms refresh rate | |
| 18 | mpb.WithRefreshRate(120*time.Millisecond), | |
| 19 | ) | |
| 20 | ||
| 21 | // Add a bar. You're not limited to just one bar, add many if you need. | |
| 22 | bar := p.AddBar(100, | |
| 23 | mpb.PrependDecorators(decor.Name("Single Bar:", 0, 0)), | |
| 24 | mpb.AppendDecorators(decor.Percentage(5, 0)), | |
| 25 | ) | |
| 26 | ||
| 27 | for i := 0; i < 100; i++ { | |
| 28 | bar.Incr(1) // increment progress bar | |
| 29 | time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) | |
| 30 | } | |
| 31 | ||
| 32 | // Don't forget to stop mpb's rendering goroutine | |
| 33 | p.Stop() | |
| 34 | fmt.Println("Stop") | |
| 35 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "sort" | |
| 6 | "sync" | |
| 7 | "time" | |
| 8 | ||
| 9 | "github.com/vbauerster/mpb" | |
| 10 | "github.com/vbauerster/mpb/decor" | |
| 11 | ) | |
| 12 | ||
| 13 | const ( | |
| 14 | maxBlockSize = 12 | |
| 15 | ) | |
| 16 | ||
| 17 | type barSlice []*mpb.Bar | |
| 18 | ||
| 19 | func (bs barSlice) Len() int { return len(bs) } | |
| 20 | ||
| 21 | func (bs barSlice) Less(i, j int) bool { | |
| 22 | ip := decor.CalcPercentage(bs[i].Total(), bs[i].Current(), 100) | |
| 23 | jp := decor.CalcPercentage(bs[j].Total(), bs[j].Current(), 100) | |
| 24 | return ip < jp | |
| 25 | } | |
| 26 | ||
| 27 | func (bs barSlice) Swap(i, j int) { bs[i], bs[j] = bs[j], bs[i] } | |
| 28 | ||
| 29 | func sortByProgressFunc() mpb.BeforeRender { | |
| 30 | return func(bars []*mpb.Bar) { | |
| 31 | sort.Sort(sort.Reverse(barSlice(bars))) | |
| 32 | } | |
| 33 | } | |
| 34 | ||
| 35 | func main() { | |
| 36 | ||
| 37 | p := mpb.New(mpb.WithWidth(64), mpb.WithBeforeRenderFunc(sortByProgressFunc())) | |
| 38 | ||
| 39 | total := 100 | |
| 40 | numBars := 3 | |
| 41 | var wg sync.WaitGroup | |
| 42 | wg.Add(numBars) | |
| 43 | ||
| 44 | for i := 0; i < numBars; i++ { | |
| 45 | var name string | |
| 46 | if i != 1 { | |
| 47 | name = fmt.Sprintf("Bar#%d:", i) | |
| 48 | } | |
| 49 | b := p.AddBar(int64(total), | |
| 50 | mpb.PrependDecorators( | |
| 51 | decor.Name(name, 0, decor.DwidthSync), | |
| 52 | decor.Counters("%3s/%3s", 0, 10, decor.DSyncSpace), | |
| 53 | ), | |
| 54 | mpb.AppendDecorators( | |
| 55 | decor.ETA(3, 0), | |
| 56 | ), | |
| 57 | ) | |
| 58 | go func() { | |
| 59 | defer wg.Done() | |
| 60 | blockSize := rand.Intn(maxBlockSize) + 1 | |
| 61 | for i := 0; i < total; i++ { | |
| 62 | sleep(blockSize) | |
| 63 | b.Incr(1) | |
| 64 | blockSize = rand.Intn(maxBlockSize) + 1 | |
| 65 | } | |
| 66 | }() | |
| 67 | } | |
| 68 | ||
| 69 | wg.Wait() | |
| 70 | p.Stop() | |
| 71 | fmt.Println("stop") | |
| 72 | } | |
| 73 | ||
| 74 | func sleep(blockSize int) { | |
| 75 | time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) | |
| 76 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "sync" | |
| 6 | "time" | |
| 7 | ||
| 8 | "github.com/vbauerster/mpb" | |
| 9 | "github.com/vbauerster/mpb/decor" | |
| 10 | ) | |
| 11 | ||
| 12 | const ( | |
| 13 | totalBars = 32 | |
| 14 | maxBlockSize = 8 | |
| 15 | ) | |
| 16 | ||
| 17 | func main() { | |
| 18 | ||
| 19 | var wg sync.WaitGroup | |
| 20 | p := mpb.New() | |
| 21 | wg.Add(totalBars) | |
| 22 | ||
| 23 | for i := 0; i < totalBars; i++ { | |
| 24 | name := fmt.Sprintf("Bar#%02d: ", i) | |
| 25 | total := rand.Intn(120) + 10 | |
| 26 | bar := p.AddBar(int64(total), | |
| 27 | mpb.PrependDecorators( | |
| 28 | decor.Name(name, len(name), 0), | |
| 29 | decor.ETA(4, decor.DSyncSpace), | |
| 30 | ), | |
| 31 | mpb.AppendDecorators( | |
| 32 | decor.Percentage(5, 0), | |
| 33 | ), | |
| 34 | ) | |
| 35 | ||
| 36 | go func() { | |
| 37 | defer wg.Done() | |
| 38 | blockSize := rand.Intn(maxBlockSize) + 1 | |
| 39 | for i := 0; i < total; i++ { | |
| 40 | sleep(blockSize) | |
| 41 | bar.Incr(1) | |
| 42 | blockSize = rand.Intn(maxBlockSize) + 1 | |
| 43 | } | |
| 44 | }() | |
| 45 | } | |
| 46 | ||
| 47 | wg.Wait() | |
| 48 | p.Stop() | |
| 49 | fmt.Println("stop") | |
| 50 | } | |
| 51 | ||
| 52 | func sleep(blockSize int) { | |
| 53 | time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) | |
| 54 | } |