2022-09-20 04:01:38 +08:00
|
|
|
package pkg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/gosuri/uiprogress"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewBar(u string, total int, progress *uiprogress.Progress) *Bar {
|
|
|
|
bar := &Bar{
|
2022-09-20 18:09:06 +08:00
|
|
|
Bar: progress.AddBar(total),
|
|
|
|
url: u,
|
|
|
|
spend: 1,
|
2022-09-20 04:01:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bar.AppendCompleted()
|
|
|
|
bar.PrependElapsed()
|
|
|
|
bar.PrependFunc(func(b *uiprogress.Bar) string {
|
|
|
|
return fmt.Sprintf("%v/s", bar.Current()/bar.spend)
|
|
|
|
})
|
|
|
|
|
|
|
|
bar.PrependFunc(func(b *uiprogress.Bar) string {
|
|
|
|
return u
|
|
|
|
})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for !bar.close {
|
|
|
|
select {
|
|
|
|
case <-time.After(time.Duration(250) * time.Millisecond):
|
|
|
|
bar.spend++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return bar
|
|
|
|
}
|
|
|
|
|
|
|
|
type Bar struct {
|
2022-09-20 18:09:06 +08:00
|
|
|
spend int
|
|
|
|
url string
|
|
|
|
close bool
|
2022-09-20 04:01:38 +08:00
|
|
|
*uiprogress.Bar
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bar *Bar) Done() {
|
|
|
|
bar.Incr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bar *Bar) Close() {
|
|
|
|
bar.close = true
|
|
|
|
}
|