Skip to content

Commit

Permalink
feature: 进度条迁移到 vbauerster/mpb
Browse files Browse the repository at this point in the history
  • Loading branch information
arrebole committed Aug 1, 2023
1 parent e66a33d commit db85cf6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 86 deletions.
3 changes: 1 addition & 2 deletions cmd/upx/upx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (

func main() {
if upx.IsVerbose {
processbar.EnableProgressbar()
defer processbar.WaitProgressbar()
processbar.ProcessBar.Enable()
}
upx.CreateUpxApp().Run(os.Args)
}
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ require (
github.com/syndtr/goleveldb v1.0.0
github.com/upyun/go-sdk/v3 v3.0.4
github.com/urfave/cli v1.22.12
github.com/vbauerster/mpb/v8 v8.5.2
golang.org/x/term v0.8.0
)

require github.com/VividCortex/ewma v1.2.0 // indirect
require (
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
)

require (
github.com/cheggaaa/pb/v3 v3.1.4
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
Expand All @@ -25,6 +28,6 @@ require (
github.com/rivo/uniseg v0.4.4 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/sys v0.10.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 6 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow=
github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4=
github.com/cheggaaa/pb/v3 v3.1.4 h1:DN8j4TVVdKu3WxVwcRKu0sG00IIU6FewoABZzXbRQeo=
github.com/cheggaaa/pb/v3 v3.1.4/go.mod h1:6wVjILNBaXMs8c21qRiaUM8BR82erfgau1DQ4iUXmSA=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -49,6 +49,8 @@ github.com/upyun/go-sdk/v3 v3.0.4 h1:2DCJa/Yi7/3ZybT9UCPATSzvU3wpPPxhXinNlb1Hi8Q
github.com/upyun/go-sdk/v3 v3.0.4/go.mod h1:P/SnuuwhrIgAVRd/ZpzDWqCsBAf/oHg7UggbAxyZa0E=
github.com/urfave/cli v1.22.12 h1:igJgVw1JdKH+trcLWLeLwZjU9fEfPesQ+9/e4MQ44S8=
github.com/urfave/cli v1.22.12/go.mod h1:sSBEIC79qR6OvcmsD4U3KABeOTxDqQtdDnaFuUN30b8=
github.com/vbauerster/mpb/v8 v8.5.2 h1:zanzt1cZpSEG5uGNYKcv43+97f0IgEnXpuBFaMxKbM0=
github.com/vbauerster/mpb/v8 v8.5.2/go.mod h1:YqKyR4ZR6Gd34yD3cDHPMmQxc+uUQMwjgO/LkxiJQ6I=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand All @@ -74,8 +76,9 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand Down
18 changes: 11 additions & 7 deletions io.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"strings"
"sync"

"github.com/upyun/upx/processbar"
"github.com/vbauerster/mpb/v8"
)

var (
IsVerbose = true
mu = &sync.Mutex{}
)

func NewFileWrappedWriter(localPath string, bar *processbar.UpxProcessBar, resume bool) (io.WriteCloser, error) {
func NewFileWrappedWriter(localPath string, bar *mpb.Bar, resume bool) (io.WriteCloser, error) {
var fd *os.File
var err error
if resume {
Expand All @@ -32,14 +32,18 @@ func NewFileWrappedWriter(localPath string, bar *processbar.UpxProcessBar, resum
return nil, err
}

if bar != nil {
bar.SetCurrent(fileinfo.Size())
if bar == nil {
return fd, nil
}
return bar.NewProxyWriter(fd), nil
bar.SetCurrent(fileinfo.Size())
return bar.ProxyWriter(fd), nil
}

func NewFileWrappedReader(bar *processbar.UpxProcessBar, fd io.Reader) io.ReadCloser {
return bar.NewProxyReader(fd)
func NewFileWrappedReader(bar *mpb.Bar, fd io.ReadCloser) io.ReadCloser {
if bar == nil {
return fd
}
return bar.ProxyReader(fd)
}

func Print(arg0 string, args ...interface{}) {
Expand Down
81 changes: 26 additions & 55 deletions processbar/bar.go
Original file line number Diff line number Diff line change
@@ -1,72 +1,43 @@
package processbar

import (
"io"
"sync"
"time"

"github.com/cheggaaa/pb/v3"
"github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor"
)

var (
enableBar bool = false
wg sync.WaitGroup
)

func EnableProgressbar() {
enableBar = true
}

type UpxProcessBar struct {
bar *pb.ProgressBar
process *mpb.Progress
enable bool
}

func (p *UpxProcessBar) SetCurrent(value int64) {
p.bar.SetCurrent(value)
var ProcessBar = &UpxProcessBar{
process: mpb.New(
mpb.WithWidth(100),
mpb.WithRefreshRate(180*time.Millisecond),
mpb.WithWaitGroup(&sync.WaitGroup{}),
),
enable: false,
}

func (p *UpxProcessBar) Start() *UpxProcessBar {
if !enableBar {
return p
}

p.bar.Set("speed", 0)
p.bar.Start()
wg.Add(1)

// 避免小文件传输过快导致最后计算速度异常, 此处进行短暂睡眠
time.Sleep(time.Millisecond * 100)
return p
func (p *UpxProcessBar) Enable() {
p.enable = true
}

func (p *UpxProcessBar) Finish() {
if !enableBar {
return
}
if !p.bar.IsFinished() {
p.bar.Finish()
func (p *UpxProcessBar) AddBar(name string, total int64) *mpb.Bar {
if !p.enable {
return nil
}
wg.Done()
}

func (p *UpxProcessBar) NewProxyWriter(r io.Writer) io.WriteCloser {
return p.bar.NewProxyWriter(r)
}

func (p *UpxProcessBar) NewProxyReader(r io.Reader) io.ReadCloser {
return p.bar.NewProxyReader(r)
}

func NewProcessBar(filename string, limit int64) *UpxProcessBar {
bar := pb.Full.New(int(limit))
bar.SetTemplateString(
`{{ with string . "filename" }}{{.}} {{end}}{{ percent . }} {{ bar . "[" ("=" | green) (cycle . "=>" | green ) "-" "]" }} ({{counters . }}, {{speed . "%s/s" "100"}})`,
)
bar.SetRefreshRate(time.Millisecond * 20)
bar.Set("filename", leftAlign(shortPath(filename, 30), 30))
return &UpxProcessBar{bar}
}

func WaitProgressbar() {
wg.Wait()
return p.process.New(total,
mpb.BarStyle().Rbound("]"),
mpb.PrependDecorators(
decor.Name(leftAlign(shortPath(name, 30), 30)),
decor.Counters(decor.SizeB1024(0), "%.2f / %.2f", decor.WCSyncSpace),
),
mpb.AppendDecorators(
decor.NewPercentage("%d", decor.WCSyncSpace),
decor.EwmaSpeed(decor.SizeB1024(0), " %.2f", 0),
))
}
22 changes: 6 additions & 16 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/upyun/upx/fsutil"
"github.com/upyun/upx/partial"
"github.com/upyun/upx/processbar"
"github.com/vbauerster/mpb/v8"
)

const (
Expand Down Expand Up @@ -351,9 +352,9 @@ func (sess *Session) getDir(upPath, localPath string, match *MatchConfig, worker
func (sess *Session) getFileWithProgress(upPath, localPath string, upInfo *upyun.FileInfo, works int, resume bool) error {
var err error

var bar *processbar.UpxProcessBar
var bar *mpb.Bar
if upInfo.Size > 0 {
bar = processbar.NewProcessBar(localPath, upInfo.Size)
bar = processbar.ProcessBar.AddBar(localPath, upInfo.Size)
}

dir := filepath.Dir(localPath)
Expand All @@ -367,9 +368,6 @@ func (sess *Session) getFileWithProgress(upPath, localPath string, upInfo *upyun
}
defer w.Close()

if bar != nil {
bar.Start()
}
downloader := partial.NewMultiPartialDownloader(
localPath,
upInfo.Size,
Expand All @@ -389,7 +387,6 @@ func (sess *Session) getFileWithProgress(upPath, localPath string, upInfo *upyun
},
)
err = downloader.Download()
bar.Finish()
return err
}

Expand Down Expand Up @@ -509,12 +506,11 @@ func (sess *Session) putFileWithProgress(localPath, upPath string, localInfo os.
Reader: fd,
}

var bar *processbar.UpxProcessBar
var bar *mpb.Bar
if IsVerbose {
if localInfo.Size() > 0 {
bar = processbar.NewProcessBar(upPath, localInfo.Size())
bar = processbar.ProcessBar.AddBar(upPath, localInfo.Size())
cfg.Reader = NewFileWrappedReader(bar, fd)
bar.Start()
}
} else {
log.Printf("file: %s, Start\n", upPath)
Expand All @@ -525,10 +521,6 @@ func (sess *Session) putFileWithProgress(localPath, upPath string, localInfo os.
}
}
err = sess.updriver.Put(cfg)
if bar != nil {
bar.Finish()
}

if !IsVerbose {
log.Printf("file: %s, Done\n", upPath)
}
Expand Down Expand Up @@ -562,9 +554,8 @@ func (sess *Session) putRemoteFileWithProgress(rawURL, upPath string) error {
}

// 创建进度条
bar := processbar.NewProcessBar(upPath, size)
bar := processbar.ProcessBar.AddBar(upPath, size)
reader := NewFileWrappedReader(bar, resp.Body)
bar.Start()

// 上传文件
err = sess.updriver.Put(&upyun.PutObjectConfig{
Expand All @@ -575,7 +566,6 @@ func (sess *Session) putRemoteFileWithProgress(rawURL, upPath string) error {
"Content-Length": fmt.Sprint(size),
},
})
bar.Finish()

if err != nil {
PrintErrorAndExit("put file error: %v", err)
Expand Down

0 comments on commit db85cf6

Please sign in to comment.