Skip to content

Commit

Permalink
fix: emtpy progrese_bar and add change the color style white into gre…
Browse files Browse the repository at this point in the history
…en after upload success. (#4857)
  • Loading branch information
jiuker committed Feb 24, 2024
1 parent 744dc91 commit f17313e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cmd/cp-main.go
Expand Up @@ -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 {
Expand Down
21 changes: 14 additions & 7 deletions cmd/get-main.go
Expand Up @@ -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()

Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/progress-bar.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
32 changes: 23 additions & 9 deletions cmd/put-main.go
Expand Up @@ -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.
Expand Down Expand Up @@ -121,7 +121,6 @@ func mainPut(cliCtx *cli.Context) error {
} else {
pg = newAccounter(totalBytes)
}
defer showLastProgressBar(pg)
go func() {
opts := prepareCopyURLsOpts{
sourceURLs: sourceURLs,
Expand All @@ -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
Expand All @@ -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
}
}
}
Expand All @@ -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(),
Expand All @@ -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())
Expand Down

0 comments on commit f17313e

Please sign in to comment.