diff --git a/cmd/upx/upx.go b/cmd/upx/upx.go index 7880c1a..610acf9 100644 --- a/cmd/upx/upx.go +++ b/cmd/upx/upx.go @@ -4,9 +4,13 @@ import ( "os" "github.com/upyun/upx" + "github.com/upyun/upx/processbar" ) func main() { - upx.EnableProgressbar() + if upx.IsVerbose { + processbar.ProcessBar.Enable() + defer processbar.ProcessBar.Wait() + } upx.CreateUpxApp().Run(os.Args) } diff --git a/go.mod b/go.mod index 0645198..15e0cb3 100644 --- a/go.mod +++ b/go.mod @@ -3,27 +3,31 @@ module github.com/upyun/upx go 1.17 require ( - github.com/arrebole/progressbar v0.0.3 github.com/fatih/color v1.15.0 github.com/stretchr/testify v1.8.1 github.com/syndtr/goleveldb v1.0.0 github.com/upyun/go-sdk/v3 v3.0.4 github.com/urfave/cli v1.22.12 - golang.org/x/term v0.8.0 + github.com/vbauerster/mpb/v8 v8.5.2 + golang.org/x/term v0.10.0 ) require ( + github.com/VividCortex/ewma v1.2.0 // indirect + github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect 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 + github.com/kr/pretty v0.1.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.17 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect - github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect github.com/pmezard/go-difflib v1.0.0 // indirect 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 + golang.org/x/text v0.11.0 // indirect + gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index a5cfb1b..ccba35d 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= -github.com/arrebole/progressbar v0.0.3 h1:BhBzvdyV+qx+iUjh9ELQNPyu5Fcg7HZ1oSlVHFnSFOs= -github.com/arrebole/progressbar v0.0.3/go.mod h1:432gxWrns8GVPbgB2WcTtAFeL/3u3YHwdDltG9Ik3VA= +github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow= +github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4= +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= @@ -14,16 +16,19 @@ github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pO github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= -github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ= -github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -39,7 +44,6 @@ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= @@ -50,6 +54,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= @@ -75,27 +81,29 @@ 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= golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= -golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols= -golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c= +golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4= +golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= diff --git a/io.go b/io.go index 7648225..018f56e 100644 --- a/io.go +++ b/io.go @@ -7,49 +7,15 @@ import ( "strings" "sync" - "github.com/arrebole/progressbar" + "github.com/vbauerster/mpb/v8" ) var ( - isVerbose = true + IsVerbose = true mu = &sync.Mutex{} ) -type WrappedWriter struct { - w io.WriteCloser - Copyed int - bar *progressbar.ProgressBar -} - -func (w *WrappedWriter) Write(b []byte) (int, error) { - n, err := w.w.Write(b) - w.Copyed += n - if w.bar != nil { - w.bar.Set(w.Copyed) - } - return n, err -} - -func (w *WrappedWriter) Close() error { - return w.w.Close() -} - -type WrappedReader struct { - r io.Reader - Copyed int - bar *progressbar.ProgressBar -} - -func (r *WrappedReader) Read(b []byte) (int, error) { - n, err := r.r.Read(b) - r.Copyed += n - if r.bar != nil { - r.bar.Set(r.Copyed) - } - return n, err -} - -func NewFileWrappedWriter(localPath string, bar *progressbar.ProgressBar, resume bool) (*WrappedWriter, error) { +func NewFileWrappedWriter(localPath string, bar *mpb.Bar, resume bool) (io.WriteCloser, error) { var fd *os.File var err error if resume { @@ -66,14 +32,18 @@ func NewFileWrappedWriter(localPath string, bar *progressbar.ProgressBar, resume return nil, err } - if bar != nil { - bar.SetOffset64(fileinfo.Size()) + if bar == nil { + return fd, nil + } + bar.SetCurrent(fileinfo.Size()) + return bar.ProxyWriter(fd), nil +} + +func NewFileWrappedReader(bar *mpb.Bar, fd io.ReadCloser) io.ReadCloser { + if bar == nil { + return fd } - return &WrappedWriter{ - w: fd, - Copyed: int(fileinfo.Size()), - bar: bar, - }, nil + return bar.ProxyReader(fd) } func Print(arg0 string, args ...interface{}) { @@ -90,7 +60,7 @@ func Print(arg0 string, args ...interface{}) { } func PrintOnlyVerbose(arg0 string, args ...interface{}) { - if isVerbose { + if IsVerbose { Print(arg0, args...) } } diff --git a/processbar/bar.go b/processbar/bar.go new file mode 100644 index 0000000..094f727 --- /dev/null +++ b/processbar/bar.go @@ -0,0 +1,56 @@ +package processbar + +import ( + "sync" + "time" + + "github.com/vbauerster/mpb/v8" + "github.com/vbauerster/mpb/v8/decor" +) + +type UpxProcessBar struct { + process *mpb.Progress + enable bool +} + +var ProcessBar = &UpxProcessBar{ + process: mpb.New( + mpb.WithWidth(100), + mpb.WithRefreshRate(180*time.Millisecond), + mpb.WithWaitGroup(&sync.WaitGroup{}), + ), + enable: false, +} + +func (p *UpxProcessBar) Enable() { + p.enable = true +} + +func (p *UpxProcessBar) AddBar(name string, total int64) *mpb.Bar { + if !p.enable { + return nil + } + + bar := p.process.AddBar(0, + mpb.PrependDecorators( + decor.Name(leftAlign(shortPath(name, 30), 30), decor.WCSyncWidth), + decor.Counters(decor.SizeB1024(0), "%.2f / %.2f", decor.WCSyncWidth), + ), + mpb.AppendDecorators( + decor.NewPercentage("%d", decor.WCSyncWidth), + decor.OnComplete( + decor.Name("...", decor.WCSyncWidth), " done", + ), + decor.AverageSpeed(decor.SizeB1024(0), " %.1f", decor.WCSyncWidth), + )) + + bar.SetTotal(total, false) + bar.DecoratorAverageAdjust(time.Now()) + return bar +} + +func (p *UpxProcessBar) Wait() { + if p.enable { + p.process.Wait() + } +} diff --git a/processbar/fmt.go b/processbar/fmt.go new file mode 100644 index 0000000..422f957 --- /dev/null +++ b/processbar/fmt.go @@ -0,0 +1,49 @@ +package processbar + +import "strings" + +func shortPath(s string, width int) string { + if slen(s) <= width { + return s + } + + dotLen := 3 + headLen := (width - dotLen) / 2 + tailLen := width - dotLen - headLen + + st := 1 + for ; st < len(s); st++ { + if slen(s[0:st]) > headLen { + break + } + } + + ed := len(s) - 1 + for ; ed >= 0; ed-- { + if slen(s[ed:]) > tailLen { + break + } + } + + return s[0:st-1] + strings.Repeat(".", dotLen) + s[ed+1:] +} + +func leftAlign(s string, width int) string { + l := slen(s) + for i := 0; i < width-l; i++ { + s += " " + } + return s +} +func rightAlign(s string, width int) string { + l := slen(s) + for i := 0; i < width-l; i++ { + s = " " + s + } + return s +} + +func slen(s string) int { + l, rl := len(s), len([]rune(s)) + return (l-rl)/2 + rl +} diff --git a/progress.go b/progress.go deleted file mode 100644 index 74692c4..0000000 --- a/progress.go +++ /dev/null @@ -1,44 +0,0 @@ -package upx - -import ( - "fmt" - "os" - "time" - - "github.com/arrebole/progressbar" -) - -var enableBar bool = false - -func EnableProgressbar() { - enableBar = true -} - -func AddBar(name string, total int) *progressbar.ProgressBar { - if !enableBar { - return nil - } - return progressbar.NewOptions64( - int64(total), - progressbar.OptionSetDescription(leftAlign(shortPath(name, 30), 30)), - progressbar.OptionSetWriter(os.Stderr), - progressbar.OptionShowBytes(true), - progressbar.OptionSetWidth(10), - progressbar.OptionThrottle(65*time.Millisecond), - progressbar.OptionShowCount(), - progressbar.OptionSpinnerType(14), - progressbar.OptionFullWidth(), - progressbar.OptionSetRenderBlankState(true), - progressbar.OptionEnableColorCodes(true), - progressbar.OptionSetTheme(progressbar.Theme{ - Saucer: "[green]=[reset]", - SaucerHead: "[green]>[reset]", - SaucerPadding: " ", - BarStart: "[", - BarEnd: "]", - }), - progressbar.OptionOnCompletion(func() { - fmt.Fprint(os.Stderr, "\n") - }), - ) -} diff --git a/reader.go b/reader.go deleted file mode 100644 index 686479e..0000000 --- a/reader.go +++ /dev/null @@ -1,20 +0,0 @@ -package upx - -import ( - "io" -) - -type ProgressReader struct { - reader io.Reader - coyed int -} - -func (r *ProgressReader) Read(b []byte) (n int, err error) { - n, err = r.reader.Read(b) - r.coyed += n - return -} - -func (r *ProgressReader) Copyed() int { - return r.coyed -} diff --git a/session.go b/session.go index 411618c..5678f24 100644 --- a/session.go +++ b/session.go @@ -18,11 +18,12 @@ import ( "sync" "time" - "github.com/arrebole/progressbar" "github.com/fatih/color" "github.com/upyun/go-sdk/v3/upyun" "github.com/upyun/upx/fsutil" "github.com/upyun/upx/partial" + "github.com/upyun/upx/processbar" + "github.com/vbauerster/mpb/v8" ) const ( @@ -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 *progressbar.ProgressBar + var bar *mpb.Bar if upInfo.Size > 0 { - bar = AddBar(localPath, int(upInfo.Size)) + bar = processbar.ProcessBar.AddBar(localPath, upInfo.Size) } dir := filepath.Dir(localPath) @@ -386,7 +387,12 @@ func (sess *Session) getFileWithProgress(upPath, localPath string, upInfo *upyun }, ) err = downloader.Download() - + if bar != nil { + bar.EnableTriggerComplete() + if err != nil { + bar.Abort(false) + } + } return err } @@ -506,10 +512,11 @@ func (sess *Session) putFileWithProgress(localPath, upPath string, localInfo os. Reader: fd, } - if isVerbose { + var bar *mpb.Bar + if IsVerbose { if localInfo.Size() > 0 { - bar := AddBar(upPath, int(localInfo.Size())) - cfg.Reader = &WrappedReader{r: fd, bar: bar} + bar = processbar.ProcessBar.AddBar(upPath, localInfo.Size()) + cfg.Reader = NewFileWrappedReader(bar, fd) } } else { log.Printf("file: %s, Start\n", upPath) @@ -520,7 +527,13 @@ func (sess *Session) putFileWithProgress(localPath, upPath string, localInfo os. } } err = sess.updriver.Put(cfg) - if !isVerbose { + if bar != nil { + bar.EnableTriggerComplete() + if err != nil { + bar.Abort(false) + } + } + if !IsVerbose { log.Printf("file: %s, Done\n", upPath) } return err @@ -553,18 +566,24 @@ func (sess *Session) putRemoteFileWithProgress(rawURL, upPath string) error { } // 创建进度条 - bar := AddBar(upPath, int(size)) + bar := processbar.ProcessBar.AddBar(upPath, size) + reader := NewFileWrappedReader(bar, resp.Body) // 上传文件 err = sess.updriver.Put(&upyun.PutObjectConfig{ Path: upPath, - Reader: &WrappedReader{r: resp.Body, bar: bar}, + Reader: reader, UseMD5: false, Headers: map[string]string{ "Content-Length": fmt.Sprint(size), }, }) - + if bar != nil { + bar.EnableTriggerComplete() + if err != nil { + bar.Abort(false) + } + } if err != nil { PrintErrorAndExit("put file error: %v", err) } diff --git a/upx.go b/upx.go index 61812b2..a792347 100644 --- a/upx.go +++ b/upx.go @@ -26,7 +26,7 @@ func CreateUpxApp() *cli.App { } app.Before = func(c *cli.Context) error { if c.Bool("q") { - isVerbose = false + IsVerbose = false } if c.String("auth") != "" { err := authStrToConfig(c.String("auth")) diff --git a/utils.go b/utils.go index 013944e..066ac15 100644 --- a/utils.go +++ b/utils.go @@ -12,52 +12,6 @@ import ( "time" ) -func shortPath(s string, width int) string { - if slen(s) <= width { - return s - } - - dotLen := 3 - headLen := (width - dotLen) / 2 - tailLen := width - dotLen - headLen - - st := 1 - for ; st < len(s); st++ { - if slen(s[0:st]) > headLen { - break - } - } - - ed := len(s) - 1 - for ; ed >= 0; ed-- { - if slen(s[ed:]) > tailLen { - break - } - } - - return s[0:st-1] + strings.Repeat(".", dotLen) + s[ed+1:] -} - -func leftAlign(s string, width int) string { - l := slen(s) - for i := 0; i < width-l; i++ { - s += " " - } - return s -} -func rightAlign(s string, width int) string { - l := slen(s) - for i := 0; i < width-l; i++ { - s = " " + s - } - return s -} - -func slen(s string) int { - l, rl := len(s), len([]rune(s)) - return (l-rl)/2 + rl -} - func parseMTime(value string, match *MatchConfig) error { if value == "" { return nil