From f17313e7ab892fa5c7561a63296b68c1691f9b2b Mon Sep 17 00:00:00 2001 From: jiuker <2818723467@qq.com> Date: Sat, 24 Feb 2024 09:33:20 +0800 Subject: [PATCH] fix: emtpy progrese_bar and add change the color style white into green after upload success. (#4857) --- cmd/cp-main.go | 2 +- cmd/get-main.go | 21 ++++++++++++++------- cmd/progress-bar.go | 7 ++++++- cmd/put-main.go | 32 +++++++++++++++++++++++--------- 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/cmd/cp-main.go b/cmd/cp-main.go index 127e5285..c2c4fd96 100644 --- a/cmd/cp-main.go +++ b/cmd/cp-main.go @@ -644,7 +644,7 @@ loop: console.Eraseline() } } else if progressReader.ProgressBar.Get() > 0 { - progressReader.ProgressBar.Finish() + progressReader.Finish() } } else { if accntReader, ok := pg.(*accounter); ok { diff --git a/cmd/get-main.go b/cmd/get-main.go index 88c469f7..304a6ee3 100644 --- a/cmd/get-main.go +++ b/cmd/get-main.go @@ -58,7 +58,7 @@ EXAMPLES: } // mainGet is the entry point for get command. -func mainGet(cliCtx *cli.Context) error { +func mainGet(cliCtx *cli.Context) (e error) { ctx, cancelGet := context.WithCancel(globalContext) defer cancelGet() @@ -78,14 +78,12 @@ func mainGet(cliCtx *cli.Context) error { // Store a progress bar or an accounter var pg ProgressReader - // Enable progress bar reader only during default mode. if !globalQuiet && !globalJSON { // set up progress bar pg = newProgressBar(totalBytes) } else { pg = newAccounter(totalBytes) } - defer showLastProgressBar(pg) go func() { opts := prepareCopyURLsOpts{ sourceURLs: sourceURLs, @@ -96,7 +94,7 @@ func mainGet(cliCtx *cli.Context) error { for getURLs := range prepareGetURLs(ctx, opts) { if getURLs.Error != nil { - printGetURLsError(&getURLs) + getURLsCh <- getURLs break } totalBytes += getURLs.SourceContent.Size @@ -109,14 +107,23 @@ func mainGet(cliCtx *cli.Context) error { for { select { case <-ctx.Done(): - return nil + showLastProgressBar(pg, nil) + return case getURLs, ok := <-getURLsCh: if !ok { - return nil + showLastProgressBar(pg, nil) + return + } + if getURLs.Error != nil { + printGetURLsError(&getURLs) + showLastProgressBar(pg, getURLs.Error.ToGoError()) + return } urls := doCopy(ctx, doCopyOpts{cpURLs: getURLs, pg: pg, encKeyDB: encKeyDB, isMvCmd: false, preserve: false, isZip: false, ignoreStat: true}) if urls.Error != nil { - return urls.Error.ToGoError() + e = urls.Error.ToGoError() + showLastProgressBar(pg, e) + return } } } diff --git a/cmd/progress-bar.go b/cmd/progress-bar.go index da502dc1..c2c282b1 100644 --- a/cmd/progress-bar.go +++ b/cmd/progress-bar.go @@ -35,7 +35,7 @@ type progressBar struct { func newPB(total int64) *pb.ProgressBar { // Progress bar specific theme customization. - console.SetColor("Bar", color.New(color.FgGreen, color.Bold)) + console.SetColor("Bar", color.New(color.FgYellow, color.Bold)) // get the new original progress bar. bar := pb.New64(total) @@ -98,6 +98,11 @@ func (p *progressBar) SetCaption(caption string) *progressBar { return p } +func (p *progressBar) Finish() { + console.SetColor("Bar", color.New(color.FgGreen, color.Bold)) + p.ProgressBar.Finish() +} + func (p *progressBar) Set64(length int64) *progressBar { p.ProgressBar = p.ProgressBar.Set64(length) return p diff --git a/cmd/put-main.go b/cmd/put-main.go index f4cb75fa..7863fa90 100644 --- a/cmd/put-main.go +++ b/cmd/put-main.go @@ -76,7 +76,7 @@ EXAMPLES: } // mainPut is the entry point for put command. -func mainPut(cliCtx *cli.Context) error { +func mainPut(cliCtx *cli.Context) (e error) { args := cliCtx.Args() if len(args) < 2 { showCommandHelpAndExit(cliCtx, 1) // last argument is exit code. @@ -121,7 +121,6 @@ func mainPut(cliCtx *cli.Context) error { } else { pg = newAccounter(totalBytes) } - defer showLastProgressBar(pg) go func() { opts := prepareCopyURLsOpts{ sourceURLs: sourceURLs, @@ -132,7 +131,7 @@ func mainPut(cliCtx *cli.Context) error { for putURLs := range preparePutURLs(ctx, opts) { if putURLs.Error != nil { - printPutURLsError(&putURLs) + putURLsCh <- putURLs break } totalBytes += putURLs.SourceContent.Size @@ -145,14 +144,23 @@ func mainPut(cliCtx *cli.Context) error { for { select { case <-ctx.Done(): - return nil + showLastProgressBar(pg, nil) + return case putURLs, ok := <-putURLsCh: if !ok { - return nil + showLastProgressBar(pg, nil) + return + } + if putURLs.Error != nil { + printPutURLsError(&putURLs) + showLastProgressBar(pg, putURLs.Error.ToGoError()) + return } urls := doCopy(ctx, doCopyOpts{cpURLs: putURLs, pg: pg, encKeyDB: encKeyDB, isMvCmd: false, preserve: false, isZip: false, multipartSize: size, multipartThreads: strconv.Itoa(threads)}) if urls.Error != nil { - return urls.Error.ToGoError() + e = urls.Error.ToGoError() + showLastProgressBar(pg, e) + return } } } @@ -164,7 +172,6 @@ func printPutURLsError(putURLs *URLs) { if !globalQuiet && !globalJSON { console.Eraseline() } - if strings.Contains(putURLs.Error.ToGoError().Error(), " is a folder.") { errorIf(putURLs.Error.Trace(), @@ -175,9 +182,16 @@ func printPutURLsError(putURLs *URLs) { } } -func showLastProgressBar(pg ProgressReader) { +func showLastProgressBar(pg ProgressReader, e error) { + if e != nil { + // We only erase a line if we are displaying a progress bar + if !globalQuiet && !globalJSON { + console.Eraseline() + } + return + } if progressReader, ok := pg.(*progressBar); ok { - progressReader.ProgressBar.Finish() + progressReader.Finish() } else { if accntReader, ok := pg.(*accounter); ok { printMsg(accntReader.Stat())