Created
February 5, 2014 13:59
-
-
Save olekukonko/8824124 to your computer and use it in GitHub Desktop.
Simple Progress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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