Last active
August 8, 2018 12:12
-
-
Save Ellrion/91de2d600c94ce4ffb26c438338e5664 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package progressbar | |
import ( | |
"strings" | |
"fmt" | |
) | |
type ProgressBar struct { | |
total float32 | |
curr float32 | |
len int | |
} | |
func New(total float32, len int) *ProgressBar { | |
if len <= 0 { | |
len = 10 | |
} | |
return &ProgressBar{total:total,len:len} | |
} | |
func (pb *ProgressBar) State(v float32) { | |
pb.curr = v | |
} | |
func (pb *ProgressBar) Add(v float32) { | |
pb.curr += v | |
} | |
func (pb *ProgressBar) String() string { | |
pct := pb.curr / pb.total * 100 | |
fill := int(pct / 100 * float32(pb.len)) | |
if fill > pb.len { | |
fill = pb.len | |
} | |
bar := strings.Repeat("=", fill) | |
if fill < pb.len { | |
bar += ">" | |
} | |
bar += strings.Repeat(".", pb.len - fill) | |
return fmt.Sprintf("\r[%s] %3.2f%%", bar, pct) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment