spray/pkg/bar.go

46 lines
776 B
Go
Raw Normal View History

package pkg
import (
"fmt"
2022-09-26 10:21:59 +08:00
"github.com/chainreactors/go-metrics"
"github.com/gosuri/uiprogress"
)
func NewBar(u string, total int, progress *uiprogress.Progress) *Bar {
bar := &Bar{
2022-09-26 10:21:59 +08:00
Bar: progress.AddBar(total),
url: u,
m: metrics.NewMeter(),
}
2022-09-26 10:21:59 +08:00
metrics.Register(bar.url, bar.m)
bar.PrependCompleted()
bar.PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf("%f/s %d/%d", bar.m.Rate1(), bar.m.Count(), bar.Bar.Total)
})
bar.PrependFunc(func(b *uiprogress.Bar) string {
return u
})
2022-09-26 10:21:59 +08:00
bar.AppendElapsed()
return bar
}
type Bar struct {
2022-09-20 18:09:06 +08:00
url string
2022-09-26 10:21:59 +08:00
total int
2022-09-20 18:09:06 +08:00
close bool
*uiprogress.Bar
2022-09-26 10:21:59 +08:00
m metrics.Meter
}
func (bar *Bar) Done() {
2022-09-26 10:21:59 +08:00
bar.m.Mark(1)
bar.Incr()
}
func (bar *Bar) Close() {
2022-09-26 10:21:59 +08:00
metrics.Unregister(bar.url)
bar.close = true
}