Skip to content

Commit

Permalink
change progress unicode style, simplify progress bar init() (#4224)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshavardhana committed Aug 26, 2022
1 parent 55c5b96 commit af1d0ee
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 58 deletions.
2 changes: 1 addition & 1 deletion cmd/cp-main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion cmd/mirror-main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
72 changes: 16 additions & 56 deletions cmd/progress-bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

"github.com/cheggaaa/pb"
"github.com/fatih/color"

"github.com/minio/pkg/console"
)

Expand All @@ -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))

Expand All @@ -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("[=> ]")
Expand All @@ -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)
}
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
}
}()
Expand Down

0 comments on commit af1d0ee

Please sign in to comment.