Skip to content

Commit

Permalink
fix: 修复同一文件的进度条出现两次的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
arrebole committed Jul 31, 2023
1 parent 7033f30 commit a187a0e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
6 changes: 4 additions & 2 deletions cmd/upx/upx.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
)

func main() {
processbar.EnableProgressbar()
if upx.IsVerbose {
processbar.EnableProgressbar()
defer processbar.WaitProgressbar()
}
upx.CreateUpxApp().Run(os.Args)
processbar.WaitProgressbar()
}
4 changes: 2 additions & 2 deletions io.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

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

Expand Down Expand Up @@ -58,7 +58,7 @@ func Print(arg0 string, args ...interface{}) {
}

func PrintOnlyVerbose(arg0 string, args ...interface{}) {
if isVerbose {
if IsVerbose {
Print(arg0, args...)
}
}
Expand Down
17 changes: 12 additions & 5 deletions processbar/bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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}
}
Expand Down
14 changes: 8 additions & 6 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion upx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit a187a0e

Please sign in to comment.