Skip to content

Instantly share code, notes, and snippets.

@olekukonko
Created February 5, 2014 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olekukonko/8824124 to your computer and use it in GitHub Desktop.
Save olekukonko/8824124 to your computer and use it in GitHub Desktop.
Simple Progress
import(
"fmt"
"io"
"os"
"time"
)
const DEFAULT_FORMAT = "\r%s % 3d %% %d kb %0.2f kb/s %v "
type ProgressBar struct {
Out io.Writer
Format string
Subject string
StartTime time.Time
Size int64
}
func NewProgressBar(subject string, size int64) ProgressBar {
return ProgressBar{os.Stdout, DEFAULT_FORMAT, subject, time.Now(), size}
}
func (pb ProgressBar) Update(tot int64) {
percent := int64(0)
if pb.Size > int64(0) {
percent = (int64(100) * tot) / pb.Size
}
totTime := time.Now().Sub(pb.StartTime)
spd := float64(tot/1000) / totTime.Seconds()
//TODO put kb size into format string
fmt.Fprintf(pb.Out, pb.Format, pb.Subject, percent, tot, spd, totTime)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment