You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
star/mget/process.go

105 lines
2.5 KiB
Go

3 months ago
package mget
import (
"fmt"
"github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor"
"io"
"strings"
"time"
)
func (m *Mget) processMiddleware(base mpb.BarFiller) mpb.BarFiller {
fn := func(w io.Writer, st decor.Statistics) error {
var res string
count := 0
2 months ago
fmt.Fprintf(w, "\nSpeed:%v AvgSpeed:%v\n", m.Redo.FormatSpeed("MB"), m.Redo.FormatAvgSpeed("MB"))
_, err := fmt.Fprintf(w, "Finished:%s Total Write:%d\n\n", m.Redo.FormatPercent(), m.Redo.Total())
3 months ago
for k := range m.threads {
v := m.threads[len(m.threads)-1-k]
if v != nil {
count++
2 months ago
percent := v.FormatPercent()
if m.Redo.Total() == m.Redo.ContentLength {
percent = "100.00%"
}
res = fmt.Sprintf("Thread %v: %s %s\t", len(m.threads)-k, v.FormatSpeed("MB"), percent) + res
3 months ago
if count%3 == 0 {
res = strings.TrimRight(res, "\t")
fmt.Fprintf(w, "%s\n", res)
res = ""
}
}
}
if res != "" {
res = strings.TrimRight(res, "\t")
fmt.Fprintf(w, "%s\n", res)
}
return err
}
if base == nil {
return mpb.BarFillerFunc(fn)
}
return mpb.BarFillerFunc(func(w io.Writer, st decor.Statistics) error {
err := fn(w, st)
if err != nil {
return err
}
return base.Fill(w, st)
})
}
func (w *Mget) Process() {
w.processEnable = true
defer func() {
w.processEnable = false
}()
fmt.Println()
p := mpb.New()
var filler mpb.BarFiller
filler = w.processMiddleware(filler)
bar := p.New(int64(w.ContentLength),
mpb.BarStyle().Rbound("|"),
mpb.BarExtender(filler, true), // all bars share same extender filler
mpb.PrependDecorators(
decor.Counters(decor.SizeB1024(0), "% .2f / % .2f"),
),
mpb.AppendDecorators(
2 months ago
decor.AverageETA(decor.ET_STYLE_GO),
3 months ago
decor.Name(" ] "),
2 months ago
decor.AverageSpeed(decor.SizeB1024(0), "% .2f "),
3 months ago
),
)
defer p.Wait()
2 months ago
lastTime := time.Now()
bar.SetRefill(int64(w.Redo.Total()))
bar.DecoratorAverageAdjust(time.Now().Add(time.Millisecond * time.Duration(-w.TimeCost)))
3 months ago
for {
select {
case <-w.ctx.Done():
bar.SetCurrent(int64(w.Redo.Total()))
if w.dynLength {
bar.SetTotal(int64(w.Redo.ContentLength), true)
}
bar.Abort(false)
return
case <-time.After(time.Second):
if !w.writeEnable {
bar.SetCurrent(int64(w.Redo.Total()))
if w.dynLength {
bar.SetTotal(int64(w.Redo.ContentLength), true)
}
bar.Abort(true)
return
}
now := w.Redo.Total()
2 months ago
date := time.Now()
bar.EwmaSetCurrent(int64(now), date.Sub(lastTime))
lastTime = date
3 months ago
if w.dynLength {
bar.SetTotal(int64(w.Redo.ContentLength), false)
}
}
}
}