From 4bd2f07cf5a54062fff67ab850c444a8a0733754 Mon Sep 17 00:00:00 2001 From: Junpei Kawamoto Date: Fri, 24 Sep 2021 03:03:00 -0600 Subject: [PATCH] Add a white space after prefix and before suffix --- v3/preset.go | 8 ++++---- v3/preset_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 v3/preset_test.go diff --git a/v3/preset.go b/v3/preset.go index f5e2fff..f3ca193 100644 --- a/v3/preset.go +++ b/v3/preset.go @@ -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}}` ) diff --git a/v3/preset_test.go b/v3/preset_test.go new file mode 100644 index 0000000..41a8266 --- /dev/null +++ b/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) + } + } +}