diff --git a/cmd/upx/upx.go b/cmd/upx/upx.go index c7c5760..07388fb 100644 --- a/cmd/upx/upx.go +++ b/cmd/upx/upx.go @@ -8,7 +8,9 @@ import ( ) func main() { - processbar.EnableProgressbar() + if upx.IsVerbose { + processbar.EnableProgressbar() + defer processbar.WaitProgressbar() + } upx.CreateUpxApp().Run(os.Args) - processbar.WaitProgressbar() } diff --git a/io.go b/io.go index 2c71916..7cc2c30 100644 --- a/io.go +++ b/io.go @@ -11,7 +11,7 @@ import ( ) var ( - isVerbose = true + IsVerbose = true mu = &sync.Mutex{} ) @@ -58,7 +58,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 index cb83bf1..fcc60a6 100644 --- a/processbar/bar.go +++ b/processbar/bar.go @@ -29,15 +29,22 @@ func (p *UpxProcessBar) StartBar() { if !enableBar { return } - wg.Add(1) + + p.bar.Set("speed", 0) p.bar.Start() + wg.Add(1) + + // 避免小文件传输过快导致最后计算速度异常, 此处进行短暂睡眠 + time.Sleep(time.Millisecond * 100) } func (p *UpxProcessBar) Finish() { if !enableBar { return } - p.bar.Finish() + if !p.bar.IsFinished() { + p.bar.Finish() + } wg.Done() } @@ -49,12 +56,12 @@ func (p *UpxProcessBar) NewProxyReader(r io.Reader) io.ReadCloser { return p.bar.NewProxyReader(r) } -func NewProcessBar(filename string, current, limit int64) *UpxProcessBar { +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"}})`, + `{{ with string . "filename" }}{{.}} {{end}}{{ percent . }} {{ bar . "[" ("=" | green) (cycle . "=>" | green ) "-" "]" }} ({{counters . }}, {{speed . "%s/s" "100"}})`, ) - bar.SetRefreshRate(time.Millisecond * 125) + bar.SetRefreshRate(time.Millisecond * 20) bar.Set("filename", leftAlign(shortPath(filename, 30), 30)) return &UpxProcessBar{bar} } diff --git a/session.go b/session.go index adac5e3..1fb67ce 100644 --- a/session.go +++ b/session.go @@ -353,7 +353,7 @@ func (sess *Session) getFileWithProgress(upPath, localPath string, upInfo *upyun var bar *processbar.UpxProcessBar if upInfo.Size > 0 { - bar = processbar.NewProcessBar(localPath, 0, upInfo.Size) + bar = processbar.NewProcessBar(localPath, upInfo.Size) } dir := filepath.Dir(localPath) @@ -507,9 +507,9 @@ func (sess *Session) putFileWithProgress(localPath, upPath string, localInfo os. } var bar *processbar.UpxProcessBar - if isVerbose { + if IsVerbose { if localInfo.Size() > 0 { - bar = processbar.NewProcessBar(upPath, 0, localInfo.Size()) + bar = processbar.NewProcessBar(upPath, localInfo.Size()) cfg.Reader = NewFileWrappedReader(bar, fd) } } else { @@ -521,9 +521,11 @@ func (sess *Session) putFileWithProgress(localPath, upPath string, localInfo os. } } err = sess.updriver.Put(cfg) - bar.Finish() + if bar != nil { + bar.Finish() + } - if !isVerbose { + if !IsVerbose { log.Printf("file: %s, Done\n", upPath) } return err @@ -556,7 +558,7 @@ func (sess *Session) putRemoteFileWithProgress(rawURL, upPath string) error { } // 创建进度条 - bar := processbar.NewProcessBar(upPath, 0, size) + bar := processbar.NewProcessBar(upPath, size) // 上传文件 err = sess.updriver.Put(&upyun.PutObjectConfig{ 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"))