badger/internal/progressbar/progressbar.go
2023-03-14 09:53:33 +01:00

77 lines
1.7 KiB
Go

package progressbar
import (
"fmt"
"velvettear/badger/internal/config"
"velvettear/badger/internal/tools"
)
// exported function(s)
func New(start, total int64) Progressbar {
return NewGraph(start, total, "")
}
func NewGraph(start, total int64, graph string) Progressbar {
if len(graph) == 0 {
graph = "#"
}
progressbar := Progressbar{
current: start,
total: total,
graph: graph,
}
progressbar.setPercentage()
progressbar.setRate()
return progressbar
}
func (progressbar *Progressbar) Step() {
progressbar.SetCurrent(progressbar.current + 1)
}
func (progressbar *Progressbar) SetCurrent(current int64) {
if config.Debug() {
return
}
progressbar.current = current
last := progressbar.percentage
progressbar.setPercentage()
if progressbar.percentage != last {
var index int64 = 0
for ; index < progressbar.percentage-last; index++ {
progressbar.rate += progressbar.graph
}
fmt.Printf("\r"+tools.GetFormattedDate()+"[info] > [%-50s] %3d%% | %d/%d", progressbar.rate, progressbar.percentage*2, progressbar.current, progressbar.total)
}
}
func (progressbar *Progressbar) Finish() {
if config.Debug() {
return
}
fmt.Println()
}
// unexported function(s)
func getPercentage(current int64, total int64) int64 {
return int64((float32(current) / float32(total)) * 50)
}
func (progressbar *Progressbar) setPercentage() {
progressbar.percentage = getPercentage(progressbar.current, progressbar.total)
}
func (progressbar *Progressbar) setRate() {
for index := 0; index < int(progressbar.percentage); index += 2 {
progressbar.rate += progressbar.graph
}
}
// struct(s)
type Progressbar struct {
percentage int64
current int64
total int64
rate string
graph string
}