Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Add ability to use custom style for bytes showing #139

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions progressbar.go
Expand Up @@ -109,6 +109,7 @@ type config struct {

// showDescriptionAtLineEnd specifies whether description should be written at line end instead of line start
showDescriptionAtLineEnd bool
customByteShow func(float64, int64) string
}

// Theme defines the elements of the bar
Expand Down Expand Up @@ -276,6 +277,13 @@ func OptionShowDescriptionAtLineEnd() Option {
}
}

// OptionUseCustomBytesShow a transformation function that takes
func OptionUseCustomBytesShow(option func(float64, int64) string) Option {
return func(p *ProgressBar) {
p.config.customByteShow = option
}
}

var defaultTheme = Theme{Saucer: "█", SaucerPadding: " ", BarStart: "|", BarEnd: "|"}

// NewOptions constructs a new instance of ProgressBar, with any options you specify
Expand Down Expand Up @@ -724,13 +732,17 @@ func renderProgressBar(c config, s *state) (int, error) {
} else {
bytesString += fmt.Sprintf("%s%s/%s%s", currentHumanize, currentSuffix, c.maxHumanized, c.maxHumanizedSuffix)
}
} else if c.customByteShow != nil {
bytesString += c.customByteShow(s.currentBytes, c.max)
} else {
bytesString += fmt.Sprintf("%.0f/%d", s.currentBytes, c.max)
}
} else {
if c.showBytes {
currentHumanize, currentSuffix := humanizeBytes(s.currentBytes)
bytesString += fmt.Sprintf("%s%s", currentHumanize, currentSuffix)
} else if c.customByteShow != nil {
bytesString += c.customByteShow(s.currentBytes, c.max)
} else {
bytesString += fmt.Sprintf("%.0f/%s", s.currentBytes, "-")
}
Expand Down
32 changes: 32 additions & 0 deletions progressbar_test.go
Expand Up @@ -416,6 +416,38 @@ func TestOptionSetElapsedTime(t *testing.T) {
// - (5/-, 5 it/s)
}

func ExampleOptionUseCustomBytesShow() {
bar := NewOptions(-1,
OptionSetWidth(10),
OptionShowIts(),
OptionShowCount(),
OptionSetElapsedTime(false), OptionUseCustomBytesShow(func(f float64, _ int64) string {
return fmt.Sprintf("%.0f items", f)
}))
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(5)

// Output:
// - (5 items, 5 it/s)
}

func ExampleOptionUseCustomBytesShowWithMax() {
bar := NewOptions(100,
OptionSetWidth(10),
OptionShowIts(),
OptionShowCount(),
OptionSetElapsedTime(false), OptionUseCustomBytesShow(func(f float64, i int64) string {
return fmt.Sprintf("%.0f of %d items", f, i)
}))
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(5)

// Output:
// - (5 of 100 items, 5 it/s)
}

func TestShowElapsedTimeOnFinish(t *testing.T) {
buf := strings.Builder{}
bar := NewOptions(10,
Expand Down