Skip to content

Commit

Permalink
Merge pull request #190 from jkawamoto/prefix
Browse files Browse the repository at this point in the history
Add a white space after prefix and before suffix
  • Loading branch information
cheggaaa committed Oct 17, 2021
2 parents 3058580 + 4bd2f07 commit bbc97ac
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
8 changes: 4 additions & 4 deletions v3/preset.go
Expand Up @@ -3,13 +3,13 @@ package pb
var (
// Full - preset with all default available elements
// Example: 'Prefix 20/100 [-->______] 20% 1 p/s ETA 1m Suffix'
Full ProgressBarTemplate = `{{string . "prefix"}}{{counters . }} {{bar . }} {{percent . }} {{speed . }} {{rtime . "ETA %s"}}{{string . "suffix"}}`
Full ProgressBarTemplate = `{{with string . "prefix"}}{{.}} {{end}}{{counters . }} {{bar . }} {{percent . }} {{speed . }} {{rtime . "ETA %s"}}{{with string . "suffix"}} {{.}}{{end}}`

// Default - preset like Full but without elapsed time
// Example: 'Prefix 20/100 [-->______] 20% 1 p/s ETA 1m Suffix'
Default ProgressBarTemplate = `{{string . "prefix"}}{{counters . }} {{bar . }} {{percent . }} {{speed . }}{{string . "suffix"}}`
// Example: 'Prefix 20/100 [-->______] 20% 1 p/s Suffix'
Default ProgressBarTemplate = `{{with string . "prefix"}}{{.}} {{end}}{{counters . }} {{bar . }} {{percent . }} {{speed . }}{{with string . "suffix"}} {{.}}{{end}}`

// Simple - preset without speed and any timers. Only counters, bar and percents
// Example: 'Prefix 20/100 [-->______] 20% Suffix'
Simple ProgressBarTemplate = `{{string . "prefix"}}{{counters . }} {{bar . }} {{percent . }}{{string . "suffix"}}`
Simple ProgressBarTemplate = `{{with string . "prefix"}}{{.}} {{end}}{{counters . }} {{bar . }} {{percent . }}{{with string . "suffix"}} {{.}}{{end}}`
)
29 changes: 29 additions & 0 deletions v3/preset_test.go
@@ -0,0 +1,29 @@
package pb

import (
"strings"
"testing"
)

func TestPreset(t *testing.T) {
prefix := "Prefix"
suffix := "Suffix"

for _, preset := range []ProgressBarTemplate{Full, Default, Simple} {
bar := preset.New(100).
SetCurrent(20).
Set("prefix", prefix).
Set("suffix", suffix).
SetWidth(50)

// initialize the internal state
_, _ = bar.render()
s := bar.String()
if !strings.HasPrefix(s, prefix+" ") {
t.Error("prefix not found:", s)
}
if !strings.HasSuffix(s, " "+suffix) {
t.Error("suffix not found:", s)
}
}
}

0 comments on commit bbc97ac

Please sign in to comment.