From af1d0ee6dadd7fba7a249bbc293e3e1cfd2876d1 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Fri, 26 Aug 2022 12:45:12 -0700 Subject: [PATCH] change progress unicode style, simplify progress bar init() (#4224) --- cmd/cp-main.go | 2 +- cmd/mirror-main.go | 2 +- cmd/progress-bar.go | 72 ++++++++++----------------------------------- 3 files changed, 18 insertions(+), 58 deletions(-) diff --git a/cmd/cp-main.go b/cmd/cp-main.go index 2046fc02..e672358f 100644 --- a/cmd/cp-main.go +++ b/cmd/cp-main.go @@ -257,7 +257,7 @@ func doCopy(ctx context.Context, cpURLs URLs, pg ProgressReader, encKeyDB map[st sourcePath := filepath.ToSlash(filepath.Join(sourceAlias, sourceURL.Path)) if progressReader, ok := pg.(*progressBar); ok { - progressReader.SetCaption(cpURLs.SourceContent.URL.String() + ": ") + progressReader.SetCaption(cpURLs.SourceContent.URL.String() + ":") } else { targetPath := filepath.ToSlash(filepath.Join(targetAlias, targetURL.Path)) printMsg(copyMessage{ diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index 356eb913..78339f25 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -432,7 +432,7 @@ func (mj *mirrorJob) doMirror(ctx context.Context, sURLs URLs) URLs { targetURL := sURLs.TargetContent.URL length := sURLs.SourceContent.Size - mj.status.SetCaption(sourceURL.String() + ": ") + mj.status.SetCaption(sourceURL.String() + ":") // Initialize target metadata. sURLs.TargetContent.Metadata = make(map[string]string) diff --git a/cmd/progress-bar.go b/cmd/progress-bar.go index e9254e69..3247987e 100644 --- a/cmd/progress-bar.go +++ b/cmd/progress-bar.go @@ -25,7 +25,6 @@ import ( "github.com/cheggaaa/pb" "github.com/fatih/color" - "github.com/minio/pkg/console" ) @@ -34,7 +33,7 @@ type progressBar struct { *pb.ProgressBar } -func newProgressReader(r io.Reader, caption string, total int64) *pb.Reader { +func newPB(total int64) *pb.ProgressBar { // Progress bar specific theme customization. console.SetColor("Bar", color.New(color.FgGreen, color.Bold)) @@ -60,12 +59,9 @@ func newProgressReader(r io.Reader, caption string, total int64) *pb.Reader { // Use different unicodes for Linux, OS X and Windows. switch runtime.GOOS { - case "linux": + case "linux", "darwin": // Need to add '\x00' as delimiter for unicode characters. - bar.Format("┃\x00▓\x00█\x00░\x00┃") - case "darwin": - // Need to add '\x00' as delimiter for unicode characters. - bar.Format(" \x00▓\x00 \x00░\x00 ") + bar.Format("━\x00━\x00━\x00┉\x00━") default: // Default to non unicode characters. bar.Format("[=> ]") @@ -74,6 +70,12 @@ func newProgressReader(r io.Reader, caption string, total int64) *pb.Reader { // Start the progress bar. bar.Start() + return bar +} + +func newProgressReader(r io.Reader, caption string, total int64) *pb.Reader { + bar := newPB(total) + if caption != "" { bar.Prefix(caption) } @@ -83,52 +85,10 @@ func newProgressReader(r io.Reader, caption string, total int64) *pb.Reader { // newProgressBar - instantiate a progress bar. func newProgressBar(total int64) *progressBar { - // Progress bar speific theme customization. - console.SetColor("Bar", color.New(color.FgGreen, color.Bold)) - - pgbar := progressBar{} - - // get the new original progress bar. - bar := pb.New64(total) - - // Set new human friendly print units. - bar.SetUnits(pb.U_BYTES) - - // Refresh rate for progress bar is set to 125 milliseconds. - bar.SetRefreshRate(time.Millisecond * 125) - - // Do not print a newline by default handled, it is handled manually. - bar.NotPrint = true - - // Show current speed is true. - bar.ShowSpeed = true - - // Custom callback with colorized bar. - bar.Callback = func(s string) { - console.Print(console.Colorize("Bar", "\r"+s)) - } - - // Use different unicodes for Linux, OS X and Windows. - switch runtime.GOOS { - case "linux": - // Need to add '\x00' as delimiter for unicode characters. - bar.Format("┃\x00▓\x00█\x00░\x00┃") - case "darwin": - // Need to add '\x00' as delimiter for unicode characters. - bar.Format(" \x00▓\x00 \x00░\x00 ") - default: - // Default to non unicode characters. - bar.Format("[=> ]") - } - - // Start the progress bar. - bar.Start() - - // Copy for future - pgbar.ProgressBar = bar + bar := newPB(total) // Return new progress bar here. - return &pgbar + return &progressBar{ProgressBar: bar} } // Set caption. @@ -163,26 +123,26 @@ func (p *progressBar) SetTotal(total int64) { // cursorAnimate - returns a animated rune through read channel for every read. func cursorAnimate() <-chan string { cursorCh := make(chan string) - var cursors string + var cursors []string switch runtime.GOOS { case "linux": // cursors = "➩➪➫➬➭➮➯➱" // cursors = "▁▃▄▅▆▇█▇▆▅▄▃" - cursors = "◐◓◑◒" + cursors = []string{"◐", "◓", "◑", "◒"} // cursors = "←↖↑↗→↘↓↙" // cursors = "◴◷◶◵" // cursors = "◰◳◲◱" // cursors = "⣾⣽⣻⢿⡿⣟⣯⣷" case "darwin": - cursors = "◐◓◑◒" + cursors = []string{"◐", "◓", "◑", "◒"} default: - cursors = "|/-\\" + cursors = []string{"|", "/", "-", "\\"} } go func() { for { for _, cursor := range cursors { - cursorCh <- string(cursor) + cursorCh <- cursor } } }()