Last active
September 20, 2020 18:08
-
-
Save smartass08/cc1ba82c2d43d7c5e5a3df0a8dfd3148 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
"math" | |
"strings" | |
) | |
type PBar struct { | |
LastDifference int | |
Difference int | |
Design []string | |
MaxSize int | |
Length int | |
} | |
func (p *PBar) Init() { | |
p.Design = append(p.Design,"▏", "▍", "▌", "▋", "▊", "▉" ) | |
p.MaxSize = 100 / 8 | |
} | |
func (p *PBar) Start(length int) { | |
p.Init() | |
p.Length = length / 8 | |
} | |
func(p *PBar) Increment(add int, status bool) string{ | |
p.Difference = p.LastDifference | |
p.Difference = add / 8 | |
if status{ | |
return p.Status() | |
} | |
return "" | |
} | |
func (p *PBar) Status() string { | |
var c float64 | |
if p.Difference == 0{ | |
c = 0 | |
}else{ | |
c = math.Round(float64(p.Difference * 100 / p.Length)) | |
} | |
c = math.Min(math.Max(c, 0), 100) | |
cFull := c / 8 | |
cPart := int(c) % 8 - 1 | |
str := strings.Repeat("█", int(cFull)) | |
if cPart >= 0{ | |
str += p.Design[int64(cPart)] | |
} | |
str += strings.Repeat("", p.MaxSize-int(cFull)) | |
str = fmt.Sprintf("[%-12v]", str) | |
return str | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment