pax_global_header00006660000000000000000000000064122473245220014515gustar00rootroot0000000000000052 comment=b8d19dcad3682f95c26d82bdfea08138b0394662 golang-pb-0.0~git20131219/000077500000000000000000000000001224732452200147475ustar00rootroot00000000000000golang-pb-0.0~git20131219/LICENSE000066400000000000000000000027021224732452200157550ustar00rootroot00000000000000Copyright (c) 2012, Sergey Cherepanov All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.golang-pb-0.0~git20131219/README.md000066400000000000000000000030241224732452200162250ustar00rootroot00000000000000## Terminal progress bar for Go Simple progress bar for console programms. ### Installation ``` go get github.com/cheggaaa/pb ``` ### Usage ```Go package main import ( "github.com/cheggaaa/pb" "time" ) func main() { count := 100000 bar := pb.StartNew(count) for i := 0; i < count; i++ { bar.Increment() time.Sleep(time.Millisecond) } bar.FinishPrint("The End!") } ``` Result will be like this: ``` > go run test.go 37158 / 100000 [================>_______________________________] 37.16% 1m11s ``` More functions? ```Go // create bar bar := pb.New(count) // refresh info every second (default 200ms) bar.RefreshRate = time.Second // show percents (by default already true) bar.ShowPercent = true // show bar (by default already true) bar.ShowBar = true // no need counters bar.ShowCounters = false // show "time left" bar.ShowTimeLeft = true // show average speed bar.ShowSpeed = true // convert output to readable format (like KB, MB) bar.Units = pb.U_BYTES // and start bar.Start() ``` Want handle progress of io operations? ```Go // create and start bar bar := pb.New(myDataLen) // for output like MB, KB bar.Units = pb.U_BYTES bar.Start() // my io.Reader r := myReader // my io.Writer w := myWriter // create multi writer writer := io.MultiWriter(w, bar) // and copy io.Copy(writer, r) // show example/copy/copy.go for advanced example ``` Not like the looks? ```Go // insert before usage pb.BarStart = "<" pb.BarEnd = ">" pb.Empty = " " pb.Current = "-" pb.CurrentN = "." ``` golang-pb-0.0~git20131219/example/000077500000000000000000000000001224732452200164025ustar00rootroot00000000000000golang-pb-0.0~git20131219/example/copy/000077500000000000000000000000001224732452200173545ustar00rootroot00000000000000golang-pb-0.0~git20131219/example/copy/copy.go000066400000000000000000000030701224732452200206550ustar00rootroot00000000000000package main import ( "github.com/cheggaaa/pb" "os" "fmt" "io" "time" "strings" "net/http" "strconv" ) func main() { // check args if len(os.Args) < 3 { printUsage() return } sourceName, destName := os.Args[1], os.Args[2] // check source var source io.Reader var sourceSize int64 if strings.HasPrefix(sourceName, "http://") { // open as url resp, err := http.Get(sourceName) if err != nil { fmt.Printf("Can't get %s: %v\n", sourceName, err) return } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { fmt.Printf("Server return non-200 status: %v\n", resp.Status) return } i, _ := strconv.Atoi(resp.Header.Get("Content-Length")) sourceSize = int64(i) source = resp.Body } else { // open as file source, err := os.Open(sourceName) if err != nil { fmt.Printf("Can't open %s: %v\n", sourceName, err) return } defer source.Close() // get source size sourceStat, err := source.Stat() if err != nil { fmt.Printf("Can't stat %s: %v\n", sourceName, err) return } sourceSize = sourceStat.Size() } // create dest dest, err := os.Create(destName) if err != nil { fmt.Printf("Can't create %s: %v\n", destName, err) return } defer dest.Close() // create bar bar := pb.New(int(sourceSize)) bar.ShowSpeed = true bar.RefreshRate = time.Millisecond * 20 bar.Units = pb.U_BYTES bar.Start() // create multi writer writer := io.MultiWriter(dest, bar) // and copy io.Copy(writer, source) bar.Finish() } func printUsage() { fmt.Println("copy [source file or url] [dest file]") }golang-pb-0.0~git20131219/example/pb.go000066400000000000000000000007121224732452200173320ustar00rootroot00000000000000package main import ( "github.com/cheggaaa/pb" "time" ) func main() { count := 5000 bar := pb.New(count) // show percents (by default already true) bar.ShowPercent = true // show bar (by default already true) bar.ShowPercent = true // no need counters bar.ShowCounters = true bar.ShowTimeLeft = true // and start bar.Start() for i := 0; i < count; i++ { bar.Increment() time.Sleep(time.Millisecond) } bar.FinishPrint("The End!") } golang-pb-0.0~git20131219/format.go000066400000000000000000000016241224732452200165710ustar00rootroot00000000000000package pb import ( "fmt" "strings" "strconv" ) const ( // By default, without type handle U_NO = 0 // Handle as b, Kb, Mb, etc U_BYTES = 1 ) // Format integer func Format(i int64, units int) string { switch units { case U_BYTES: return FormatBytes(i) } // by default just convert to string return strconv.Itoa(int(i)) } // Convert bytes to human readable string. Like a 2 MiB, 64.2 KiB, 52 B func FormatBytes(i int64) (result string) { switch { case i > (1024 * 1024 * 1024 * 1024): result = fmt.Sprintf("%#.02f TB", float64(i)/1024/1024/1024/1024) case i > (1024 * 1024 * 1024): result = fmt.Sprintf("%#.02f GB", float64(i)/1024/1024/1024) case i > (1024 * 1024): result = fmt.Sprintf("%#.02f MB", float64(i)/1024/1024) case i > 1024: result = fmt.Sprintf("%#.02f KB", float64(i)/1024) default: result = fmt.Sprintf("%d B", i) } result = strings.Trim(result, " ") return } golang-pb-0.0~git20131219/pb.go000066400000000000000000000107551224732452200157070ustar00rootroot00000000000000package pb import ( "fmt" "io" "strings" "sync/atomic" "time" ) var ( // Default refresh rate - 200ms DefaultRefreshRate = time.Millisecond * 200 BarStart = "[" BarEnd = "]" Empty = "_" Current = "=" CurrentN = ">" ) // Create new progress bar object func New(total int) *ProgressBar { return &ProgressBar{ Total: int64(total), RefreshRate: DefaultRefreshRate, ShowPercent: true, ShowCounters: true, ShowBar: true, ShowTimeLeft: true, } } // Create new object and start func StartNew(total int) (pb *ProgressBar) { pb = New(total) pb.Start() return } // Callback for custom output // For example: // bar.Callback = func(s string) { // mySuperPrint(s) // } // type Callback func(out string) type ProgressBar struct { current int64 // current must be first member of struct (https://code.google.com/p/go/issues/detail?id=5278) Total int64 RefreshRate time.Duration ShowPercent, ShowCounters bool ShowSpeed, ShowTimeLeft, ShowBar bool Output io.Writer Callback Callback NotPrint bool Units int isFinish bool startTime time.Time } // Start print func (pb *ProgressBar) Start() { pb.startTime = time.Now() if pb.Total == 0 { pb.ShowBar = false pb.ShowTimeLeft = false pb.ShowPercent = false } go pb.writer() } // Increment current value func (pb *ProgressBar) Increment() int { return pb.Add(1) } // Set current value func (pb *ProgressBar) Set(current int) { atomic.StoreInt64(&pb.current, int64(current)) } // Add to current value func (pb *ProgressBar) Add(add int) int { return int(atomic.AddInt64(&pb.current, int64(add))) } // End print func (pb *ProgressBar) Finish() { pb.isFinish = true pb.write(atomic.LoadInt64(&pb.current)) if !pb.NotPrint { fmt.Println() } } // End print and write string 'str' func (pb *ProgressBar) FinishPrint(str string) { pb.Finish() fmt.Println(str) } // implement io.Writer func (pb *ProgressBar) Write(p []byte) (n int, err error) { n = len(p) pb.Add(n) return } // implement io.Reader func (pb *ProgressBar) Read(p []byte) (n int, err error) { n = len(p) pb.Add(n) return } func (pb *ProgressBar) write(current int64) { width, _ := terminalWidth() var percentBox, countersBox, timeLeftBox, speedBox, barBox, end, out string // percents if pb.ShowPercent { percent := float64(current) / (float64(pb.Total) / float64(100)) percentBox = fmt.Sprintf(" %#.02f %% ", percent) } // counters if pb.ShowCounters { if pb.Total > 0 { countersBox = fmt.Sprintf("%s / %s ", Format(current, pb.Units), Format(pb.Total, pb.Units)) } else { countersBox = Format(current, pb.Units) + " " } } // time left if pb.ShowTimeLeft && current > 0 { fromStart := time.Now().Sub(pb.startTime) perEntry := fromStart / time.Duration(current) left := time.Duration(pb.Total-current) * perEntry left = (left / time.Second) * time.Second if left > 0 { timeLeftBox = left.String() } } // speed if pb.ShowSpeed && current > 0 { fromStart := time.Now().Sub(pb.startTime) speed := float64(current) / (float64(fromStart) / float64(time.Second)) speedBox = Format(int64(speed), pb.Units) + "/s " } // bar if pb.ShowBar { size := width - len(countersBox+BarStart+BarEnd+percentBox+timeLeftBox+speedBox) if size > 0 { curCount := int(float64(current) / (float64(pb.Total) / float64(size))) emptCount := size - curCount barBox = BarStart if emptCount < 0 { emptCount = 0 } if curCount > size { curCount = size } if emptCount <= 0 { barBox += strings.Repeat(Current, curCount) } else if curCount > 0 { barBox += strings.Repeat(Current, curCount-1) + CurrentN } barBox += strings.Repeat(Empty, emptCount) + BarEnd } } // check len out = countersBox + barBox + percentBox + speedBox + timeLeftBox if len(out) < width { end = strings.Repeat(" ", width-len(out)) } out = countersBox + barBox + percentBox + speedBox + timeLeftBox // and print! switch { case pb.Output != nil: fmt.Fprint(pb.Output, out+end) case pb.Callback != nil: pb.Callback(out + end) case !pb.NotPrint: fmt.Print("\r" + out + end) } } func (pb *ProgressBar) writer() { var c, oc int64 for { if pb.isFinish { break } c = atomic.LoadInt64(&pb.current) if c != oc { pb.write(c) oc = c } time.Sleep(pb.RefreshRate) } } type window struct { Row uint16 Col uint16 Xpixel uint16 Ypixel uint16 } golang-pb-0.0~git20131219/pb_nix.go000066400000000000000000000010511224732452200165520ustar00rootroot00000000000000// +build linux darwin freebsd package pb import ( "runtime" "syscall" "unsafe" ) const ( TIOCGWINSZ = 0x5413 TIOCGWINSZ_OSX = 1074295912 ) func bold(str string) string { return "\033[1m" + str + "\033[0m" } func terminalWidth() (int, error) { w := new(window) tio := syscall.TIOCGWINSZ if runtime.GOOS == "darwin" { tio = TIOCGWINSZ_OSX } res, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(syscall.Stdin), uintptr(tio), uintptr(unsafe.Pointer(w)), ) if int(res) == -1 { return 0, err } return int(w.Col), nil } golang-pb-0.0~git20131219/pb_win.go000066400000000000000000000005211224732452200165520ustar00rootroot00000000000000// +build windows package pb import ( "syscall" "github.com/AllenDang/w32" ) func bold(str string) string { return str } func terminalWidth() (int, error) { screenBufInfo := w32.GetConsoleScreenBufferInfo(w32.HANDLE(syscall.Stdout)) if screenBufInfo == nil { return 79, nil } return int(screenBufInfo.DwSize.X) - 1, nil }