pax_global_header00006660000000000000000000000064137225503300014512gustar00rootroot0000000000000052 comment=e3dbdb97e1eaecdd80cbe8e7cc57b35654c039e4 goprogressbar-0.2.0/000077500000000000000000000000001372255033000143705ustar00rootroot00000000000000goprogressbar-0.2.0/.github/000077500000000000000000000000001372255033000157305ustar00rootroot00000000000000goprogressbar-0.2.0/.github/FUNDING.yml000066400000000000000000000000171372255033000175430ustar00rootroot00000000000000github: muesli goprogressbar-0.2.0/.github/workflows/000077500000000000000000000000001372255033000177655ustar00rootroot00000000000000goprogressbar-0.2.0/.github/workflows/build.yml000066400000000000000000000011541372255033000216100ustar00rootroot00000000000000name: build on: [push, pull_request] jobs: build: strategy: matrix: go-version: [1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x] os: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.os }} env: GO111MODULE: "on" steps: - name: Install Go uses: actions/setup-go@v2 with: go-version: ${{ matrix.go-version }} - name: Checkout code uses: actions/checkout@v1 - name: Download Go modules run: go mod download - name: Build run: go build -v ./... - name: Test run: go test ./... goprogressbar-0.2.0/.github/workflows/coverage.yml000066400000000000000000000013251372255033000223040ustar00rootroot00000000000000name: coverage on: [push, pull_request] jobs: coverage: strategy: matrix: go-version: [1.15.x] os: [ubuntu-latest] runs-on: ${{ matrix.os }} env: GO111MODULE: "on" steps: - name: Install Go uses: actions/setup-go@v2 with: go-version: ${{ matrix.go-version }} - name: Checkout code uses: actions/checkout@v1 - name: Coverage env: COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | go test -race -covermode atomic -coverprofile=profile.cov ./... GO111MODULE=off go get github.com/mattn/goveralls $(go env GOPATH)/bin/goveralls -coverprofile=profile.cov -service=github goprogressbar-0.2.0/.github/workflows/lint.yml000066400000000000000000000013361372255033000214610ustar00rootroot00000000000000name: lint on: push: pull_request: jobs: golangci: name: lint runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: golangci-lint uses: golangci/golangci-lint-action@v2 with: # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. version: v1.30 # Optional: golangci-lint command line arguments. args: --issues-exit-code=0 # Optional: working directory, useful for monorepos # working-directory: somedir # Optional: show only new issues if it's a pull request. The default value is `false`. only-new-issues: true goprogressbar-0.2.0/.gitignore000066400000000000000000000005061372255033000163610ustar00rootroot00000000000000# Compiled Object files, Static and Dynamic libs (Shared Objects) *.o *.a *.so # Folders _obj _test # Architecture specific extensions/prefixes *.[568vq] [568vq].out *.cgo1.go *.cgo2.c _cgo_defun.c _cgo_gotypes.go _cgo_export.* _testmain.go *.exe *.test *.prof *~ examples/multibar/multibar examples/singlebar/singlebar goprogressbar-0.2.0/LICENSE000066400000000000000000000020671372255033000154020ustar00rootroot00000000000000MIT License Copyright (c) 2016 Christian Muehlhaeuser Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. goprogressbar-0.2.0/README.md000066400000000000000000000056121372255033000156530ustar00rootroot00000000000000goprogressbar ============= [![Latest Release](https://img.shields.io/github/release/muesli/goprogressbar.svg)](https://github.com/muesli/goprogressbar/releases) [![Build Status](https://github.com/muesli/goprogressbar/workflows/build/badge.svg)](https://github.com/muesli/goprogressbar/actions) [![Coverage Status](https://coveralls.io/repos/github/muesli/goprogressbar/badge.svg?branch=master)](https://coveralls.io/github/muesli/goprogressbar?branch=master) [![Go ReportCard](http://goreportcard.com/badge/muesli/goprogressbar)](http://goreportcard.com/report/muesli/goprogressbar) [![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](https://pkg.go.dev/github.com/muesli/goprogressbar) Golang helper to print one or many progress bars on the console ## Installation Make sure you have a working Go environment (Go 1.9 or higher is required). See the [install instructions](http://golang.org/doc/install.html). To install goprogressbar, simply run: go get github.com/muesli/goprogressbar ## Example ```go package main import ( "fmt" "strconv" "time" "github.com/muesli/goprogressbar" ) func main() { mpb := goprogressbar.MultiProgressBar{} for i := 0; i < 10; i++ { pb := &goprogressbar.ProgressBar{ Text: "Progress " + strconv.FormatInt(int64(i+1), 10), Total: 100, Current: 0, Width: 60, } mpb.AddProgressBar(pb) } pb := &goprogressbar.ProgressBar{ Text: "Overall Progress", Total: 1000, Current: 0, Width: 60, } mpb.AddProgressBar(pb) // fill progress bars one after another for j := 0; j < 10; j++ { for i := 1; i <= 100; i++ { p := mpb.ProgressBars[j] p.Current = int64(i) p.RightAlignedText = fmt.Sprintf("%d of %d", i, p.Total) pb.Current++ mpb.LazyPrint() time.Sleep(23 * time.Millisecond) } } fmt.Println() } ``` ## What it looks like ``` Progress 1 100 of 100 [#################################################] 100.00% Progress 2 100 of 100 [#################################################] 100.00% Progress 3 89 of 100 [###########################################>-----] 89.00% Progress 4 [#>-----------------------------------------------] 0.00% Progress 5 [#>-----------------------------------------------] 0.00% Progress 6 [#>-----------------------------------------------] 0.00% Progress 7 [#>-----------------------------------------------] 0.00% Progress 8 [#>-----------------------------------------------] 0.00% Progress 9 [#>-----------------------------------------------] 0.00% Progress 10 [#>-----------------------------------------------] 0.00% Overall Progress [#############>-----------------------------------] 28.90% ``` goprogressbar-0.2.0/examples/000077500000000000000000000000001372255033000162065ustar00rootroot00000000000000goprogressbar-0.2.0/examples/multibar/000077500000000000000000000000001372255033000200255ustar00rootroot00000000000000goprogressbar-0.2.0/examples/multibar/multi.go000066400000000000000000000017421372255033000215120ustar00rootroot00000000000000/* * goprogressbar * Copyright (c) 2016-2017, Christian Muehlhaeuser * * For license see LICENSE */ package main import ( "fmt" "strconv" "time" "github.com/muesli/goprogressbar" ) func main() { mpb := goprogressbar.MultiProgressBar{} for i := 0; i < 10; i++ { pb := &goprogressbar.ProgressBar{ Text: "Progress " + strconv.FormatInt(int64(i+1), 10), Total: 100, Current: 0, Width: 60, PrependTextFunc: func(p *goprogressbar.ProgressBar) string { return fmt.Sprintf("%d of %d", p.Current, p.Total) }, } mpb.AddProgressBar(pb) } pb := &goprogressbar.ProgressBar{ Text: "Overall Progress", Total: 1000, Current: 0, Width: 60, } mpb.AddProgressBar(pb) // fill progress bars one after another for j := 0; j < 10; j++ { for i := 1; i <= 100; i++ { p := mpb.ProgressBars[j] p.Current = int64(i) pb.Current++ mpb.LazyPrint() time.Sleep(23 * time.Millisecond) } } fmt.Println() } goprogressbar-0.2.0/examples/singlebar/000077500000000000000000000000001372255033000201545ustar00rootroot00000000000000goprogressbar-0.2.0/examples/singlebar/single.go000066400000000000000000000010131372255033000217570ustar00rootroot00000000000000/* * goprogressbar * Copyright (c) 2016-2017, Christian Muehlhaeuser * * For license see LICENSE */ package main import ( "fmt" "time" "github.com/muesli/goprogressbar" ) func main() { pb := &goprogressbar.ProgressBar{ Text: "Current Progress", Total: 1000, Current: 0, Width: 60, } for i := 1; i <= 1000; i++ { pb.PrependText = fmt.Sprintf("%d of %d", i, pb.Total) pb.Current = int64(i) time.Sleep(23 * time.Millisecond) pb.LazyPrint() } fmt.Println() } goprogressbar-0.2.0/go.mod000066400000000000000000000001601372255033000154730ustar00rootroot00000000000000module github.com/muesli/goprogressbar go 1.12 require golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 goprogressbar-0.2.0/go.sum000066400000000000000000000014701372255033000155250ustar00rootroot00000000000000golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc= golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= goprogressbar-0.2.0/progressbar.go000066400000000000000000000106071372255033000172540ustar00rootroot00000000000000/* * goprogressbar * Copyright (c) 2016-2017, Christian Muehlhaeuser * * For license see LICENSE */ package goprogressbar import ( "fmt" "io" "math" "os" "strings" "syscall" "time" "unicode/utf8" "golang.org/x/crypto/ssh/terminal" ) const fps = 25 var ( // Stdout defines where output gets printed to Stdout io.Writer = os.Stdout // BarFormat defines the bar design BarFormat = "[#>-]" ) // ProgressBar is a helper for printing a progress bar type ProgressBar struct { // Text displayed on the very left Text string // Text prepending the bar PrependText string // Max value (100%) Total int64 // Current progress value Current int64 // Desired bar width Width uint // If a PrependTextFunc is set, the PrependText will be automatically // generated on every print PrependTextFunc func(p *ProgressBar) string lastPrintTime time.Time } // MultiProgressBar is a helper for printing multiple progress bars type MultiProgressBar struct { ProgressBars []*ProgressBar lastPrintTime time.Time } // percentage returns the percentage bound between 0.0 and 1.0 func (p *ProgressBar) percentage() float64 { pct := float64(p.Current) / float64(p.Total) if p.Total == 0 { if p.Current == 0 { // When both Total and Current are 0, show a full progressbar pct = 1 } else { pct = 0 } } // percentage is bound between 0 and 1 return math.Min(1, math.Max(0, pct)) } // UpdateRequired returns true when this progressbar wants an update regardless // of fps limitation func (p *ProgressBar) UpdateRequired() bool { return p.Current == 0 || p.Current == p.Total } // LazyPrint writes the progress bar to stdout if a significant update occurred func (p *ProgressBar) LazyPrint() { now := time.Now() if p.UpdateRequired() || now.Sub(p.lastPrintTime) > time.Second/fps { p.lastPrintTime = now p.Print() } } // Clear deletes everything on the current terminal line, hence removing a printed progressbar func (p *ProgressBar) Clear() { clearCurrentLine() } // Print writes the progress bar to stdout func (p *ProgressBar) Print() { if p.PrependTextFunc != nil { p.PrependText = p.PrependTextFunc(p) } pct := p.percentage() clearCurrentLine() pcts := fmt.Sprintf("%.2f%%", pct*100) for len(pcts) < 7 { pcts = " " + pcts } tiWidth, _, err := terminal.GetSize(int(syscall.Stdin)) if tiWidth <= 0 || err != nil { // we're not running inside a real terminal (e.g. CI) // we assume a width of 80 tiWidth = 80 } barWidth := uint(math.Min(float64(p.Width), float64(tiWidth)/2.0)) size := int(barWidth) - len(pcts) - 4 fill := int(math.Max(2, math.Floor((float64(size)*pct)+.5))) if size < 16 { barWidth = 0 } text := p.Text textLen := utf8.RuneCountInString(p.Text) maxTextWidth := tiWidth - 3 - int(barWidth) - utf8.RuneCountInString(p.PrependText) if maxTextWidth < 0 { maxTextWidth = 0 } if textLen > maxTextWidth { if textLen-maxTextWidth+3 < textLen { text = "..." + string([]rune(p.Text)[textLen-maxTextWidth+3:]) } else { text = "" } } // Print text s := fmt.Sprintf("%s%s %s ", text, strings.Repeat(" ", maxTextWidth-utf8.RuneCountInString(text)), p.PrependText) fmt.Fprint(Stdout, s) if barWidth > 0 { progChar := BarFormat[2] if p.Current == p.Total { progChar = BarFormat[1] } // Print progress bar fmt.Fprintf(Stdout, "%c%s%c%s%c %s", BarFormat[0], strings.Repeat(string(BarFormat[1]), fill-1), progChar, strings.Repeat(string(BarFormat[3]), size-fill), BarFormat[4], pcts) } } // AddProgressBar adds another progress bar to the multi struct func (mp *MultiProgressBar) AddProgressBar(p *ProgressBar) { mp.ProgressBars = append(mp.ProgressBars, p) if len(mp.ProgressBars) > 1 { fmt.Println() } mp.Print() } // Print writes all progress bars to stdout func (mp *MultiProgressBar) Print() { moveCursorUp(uint(len(mp.ProgressBars))) for _, p := range mp.ProgressBars { moveCursorDown(1) p.Print() } } // LazyPrint writes all progress bars to stdout if a significant update occurred func (mp *MultiProgressBar) LazyPrint() { forced := false for _, p := range mp.ProgressBars { if p.UpdateRequired() { forced = true break } } now := time.Now() if !forced { forced = now.Sub(mp.lastPrintTime) > time.Second/fps } if forced { mp.lastPrintTime = now moveCursorUp(uint(len(mp.ProgressBars))) for _, p := range mp.ProgressBars { moveCursorDown(1) p.Print() } } } goprogressbar-0.2.0/progressbar_test.go000066400000000000000000000124261372255033000203140ustar00rootroot00000000000000/* * goprogressbar * Copyright (c) 2016-2017, Christian Muehlhaeuser * * For license see LICENSE */ package goprogressbar import ( "bytes" "fmt" "testing" ) func TestPercentageBound(t *testing.T) { p := ProgressBar{Current: -1, Total: 100} if p.percentage() != 0 { t.Errorf("percentage should be bound to 0, got: %f", p.percentage()) } p = ProgressBar{Current: 200, Total: 100} if p.percentage() != 1 { t.Errorf("percentage should be bound to 1, got: %f", p.percentage()) } } func TestPercentageSpecialValues(t *testing.T) { p := ProgressBar{Current: 0, Total: 0} if p.percentage() != 1 { t.Errorf("percentage should be 1 when both current and total are 0, got: %f", p.percentage()) } p = ProgressBar{Current: 100, Total: 0} if p.percentage() != 0 { t.Errorf("percentage should be 0 when current is greater than 0 but the total is unknown (0), got: %f", p.percentage()) } } func TestProgressBarOutput(t *testing.T) { buf := &bytes.Buffer{} Stdout = buf p := ProgressBar{Text: "Test", Current: 0, Total: 100, Width: 60} p.PrependText = fmt.Sprintf("%d of %d", p.Current, p.Total) p.Print() if buf.String() != "\033[2K\rTest 0 of 100 [#>---------------------------] 0.00%" { t.Errorf("Unexpected progressbar print behaviour") } buf.Reset() p.Current = 10 p.PrependText = fmt.Sprintf("%d of %d", p.Current, p.Total) p.Print() if buf.String() != "\033[2K\rTest 10 of 100 [##>--------------------------] 10.00%" { t.Errorf("Unexpected progressbar print behaviour") } buf.Reset() p.Current = 100 p.PrependText = fmt.Sprintf("%d of %d", p.Current, p.Total) p.Print() if buf.String() != "\033[2K\rTest 100 of 100 [#############################] 100.00%" { t.Errorf("Unexpected progressbar print behaviour") } buf.Reset() } func TestMultiProgressBarOutput(t *testing.T) { buf := &bytes.Buffer{} Stdout = buf p1 := ProgressBar{Text: "Test1", Current: 23, Total: 100, Width: 60} p1.PrependText = fmt.Sprintf("%d of %d", p1.Current, p1.Total) p2 := ProgressBar{Text: "Test2", Current: 69, Total: 100, Width: 60} p2.PrependText = fmt.Sprintf("%d of %d", p2.Current, p2.Total) mp := MultiProgressBar{} mp.AddProgressBar(&p1) mp.AddProgressBar(&p2) if buf.String() != "\033[1A\033[1B\033[2K\rTest1 23 of 100 [######>----------------------] 23.00%"+ "\033[2A\033[1B\033[2K\rTest1 23 of 100 [######>----------------------] 23.00%"+ "\033[1B\033[2K\rTest2 69 of 100 [###################>---------] 69.00%" { t.Errorf("Unexpected multi progressbar print behaviour") } buf.Reset() } func TestLazyPrint(t *testing.T) { buf := &bytes.Buffer{} Stdout = buf p := ProgressBar{Text: "Test", Current: 10, Total: 100, Width: 60} p.PrependText = fmt.Sprintf("%d of %d", p.Current, p.Total) // LazyPrint should buffer prints, so we call it twice and check it // only prints once p.LazyPrint() p.LazyPrint() if buf.String() != "\033[2K\rTest 10 of 100 [##>--------------------------] 10.00%" { t.Errorf("Unexpected progressbar print behaviour") } buf.Reset() } func TestMultiLazyPrint(t *testing.T) { buf := &bytes.Buffer{} Stdout = buf p1 := ProgressBar{Text: "Test1", Current: 23, Total: 100, Width: 60} p1.PrependText = fmt.Sprintf("%d of %d", p1.Current, p1.Total) p2 := ProgressBar{Text: "Test2", Current: 69, Total: 100, Width: 60} p2.PrependText = fmt.Sprintf("%d of %d", p2.Current, p2.Total) mp := MultiProgressBar{} mp.AddProgressBar(&p1) mp.AddProgressBar(&p2) buf.Reset() // LazyPrint should buffer prints, so we call it twice and check it // only prints once mp.LazyPrint() mp.LazyPrint() if buf.String() != "\033[2A\033[1B\033[2K\rTest1 23 of 100 [######>----------------------] 23.00%"+ "\033[1B\033[2K\rTest2 69 of 100 [###################>---------] 69.00%" { t.Errorf("Unexpected multi progressbar print behaviour") } buf.Reset() } func TestPrependFunc(t *testing.T) { buf := &bytes.Buffer{} Stdout = buf p := ProgressBar{Text: "Test", Current: 10, Total: 100, Width: 60} p.PrependTextFunc = func(p *ProgressBar) string { return fmt.Sprintf("%d of %d", p.Current, p.Total) } p.Print() if buf.String() != "\033[2K\rTest 10 of 100 [##>--------------------------] 10.00%" { t.Errorf("Unexpected progressbar print behaviour") } buf.Reset() } func TestTextElide(t *testing.T) { buf := &bytes.Buffer{} Stdout = buf p := ProgressBar{Text: "ThisIsAReallyLongLongStringHere", Current: 10, Total: 100, Width: 60} p.PrependText = fmt.Sprintf("%d of %d", p.Current, p.Total) p.Print() if buf.String() != "\033[2K\r...AReallyLongLongStringHere 10 of 100 [##>--------------------------] 10.00%" { t.Errorf("Unexpected progressbar print behaviour") } buf.Reset() } func TestFormat(t *testing.T) { buf := &bytes.Buffer{} Stdout = buf BarFormat = "(+-_)" p := ProgressBar{Text: "Test", Current: 10, Total: 100, Width: 60} p.PrependText = fmt.Sprintf("%d of %d", p.Current, p.Total) p.Print() if buf.String() != "\033[2K\rTest 10 of 100 (++-__________________________) 10.00%" { t.Errorf("Unexpected progressbar print behaviour") } buf.Reset() } goprogressbar-0.2.0/terminal.go000066400000000000000000000005761372255033000165420ustar00rootroot00000000000000/* * goprogressbar * Copyright (c) 2016-2017, Christian Muehlhaeuser * * For license see LICENSE */ package goprogressbar import "fmt" func clearCurrentLine() { fmt.Fprintf(Stdout, "\033[2K\r") } func moveCursorUp(lines uint) { fmt.Fprintf(Stdout, "\033[%dA", lines) } func moveCursorDown(lines uint) { fmt.Fprintf(Stdout, "\033[%dB", lines) } goprogressbar-0.2.0/terminal_test.go000066400000000000000000000007761372255033000176030ustar00rootroot00000000000000/* * goprogressbar * Copyright (c) 2016-2017, Christian Muehlhaeuser * * For license see LICENSE */ package goprogressbar import ( "bytes" "testing" ) func TestCursorMovement(t *testing.T) { buf := &bytes.Buffer{} Stdout = buf moveCursorUp(5) if buf.String() != "\033[5A" { t.Errorf("Unexpected cursor up movement behaviour") } buf.Reset() moveCursorDown(5) if buf.String() != "\033[5B" { t.Errorf("Unexpected cursor down movement behaviour") } buf.Reset() }