2022-09-20 04:01:38 +08:00
|
|
|
|
package pkg
|
|
|
|
|
|
|
|
|
|
import (
|
2022-09-26 10:21:59 +08:00
|
|
|
|
"github.com/chainreactors/go-metrics"
|
2024-03-07 00:24:30 +08:00
|
|
|
|
"github.com/vbauerster/mpb/v8"
|
|
|
|
|
"github.com/vbauerster/mpb/v8/decor"
|
2022-09-20 04:01:38 +08:00
|
|
|
|
)
|
|
|
|
|
|
2024-03-07 00:24:30 +08:00
|
|
|
|
func NewBar(u string, total int, stat *Statistor, p *mpb.Progress) *Bar {
|
|
|
|
|
m := metrics.NewMeter()
|
|
|
|
|
metrics.Register(u, m)
|
2022-09-20 04:01:38 +08:00
|
|
|
|
|
2024-03-07 00:24:30 +08:00
|
|
|
|
// 在mpb v8中,Name装饰器的使用方式略有不同
|
|
|
|
|
bar := p.AddBar(int64(total),
|
|
|
|
|
mpb.BarFillerClearOnComplete(),
|
|
|
|
|
mpb.BarRemoveOnComplete(),
|
|
|
|
|
mpb.PrependDecorators(
|
|
|
|
|
// 显示自定义的信息,比如下载速度和进度
|
|
|
|
|
decor.Name(u, decor.WC{W: len(u) + 1, C: decor.DindentRight}), // 这里调整了装饰器的参数
|
|
|
|
|
decor.Counters(0, "% d/% d"),
|
|
|
|
|
),
|
|
|
|
|
mpb.AppendDecorators(
|
|
|
|
|
// 显示经过的时间
|
|
|
|
|
decor.Elapsed(decor.ET_STYLE_GO, decor.WC{W: 4}),
|
|
|
|
|
),
|
|
|
|
|
)
|
2022-09-20 04:01:38 +08:00
|
|
|
|
|
2024-03-07 00:24:30 +08:00
|
|
|
|
return &Bar{
|
|
|
|
|
url: u,
|
|
|
|
|
bar: bar,
|
|
|
|
|
m: m,
|
|
|
|
|
}
|
2022-09-20 04:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Bar struct {
|
2024-03-07 00:24:30 +08:00
|
|
|
|
url string
|
|
|
|
|
bar *mpb.Bar
|
|
|
|
|
m metrics.Meter
|
2022-09-20 04:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (bar *Bar) Done() {
|
2022-09-26 10:21:59 +08:00
|
|
|
|
bar.m.Mark(1)
|
2024-03-07 00:24:30 +08:00
|
|
|
|
bar.bar.Increment()
|
2022-09-20 04:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (bar *Bar) Close() {
|
2024-03-07 00:24:30 +08:00
|
|
|
|
//metrics.Unregister(bar.url)
|
|
|
|
|
// 标记进度条为完成状态
|
|
|
|
|
//bar.bar.Abort(false)
|
2022-09-20 04:01:38 +08:00
|
|
|
|
}
|