Skip to content

Commit d4b664f

Browse files
authored
Adding NoPercent config option and Suffix / UpdateSuffix() (#20)
* Adding NoPercent config option and Suffix / UpdateSuffix * linter * add more links to examples Fixes #19
1 parent 33b87a8 commit d4b664f

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,7 @@ If you have more advanced needs for TUI including raw mode input or readline, yo
133133
[github.com/fortio/terminal](https://github.com/fortio/terminal#terminal)
134134

135135
And still use this for a progress bar part.
136+
137+
It is used for instance in [github.com/fortio/fps's web mode progress update](https://github.com/fortio/fps#web-serving-fire-mode)
138+
139+
And [grol](https://github.com/grol-io/grol#grol)'s multi file processing progress info (`make grol-tests`)

examples/simple/example.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ func main() {
2828
everyFlag := flag.Duration("every", 1*time.Second, "Print extra stuff every")
2929
noAnsiFlag := flag.Bool("no-ansi", false, "Disable ANSI escape codes (colors and cursor movement)")
3030
moveUpFlag := flag.Bool("moveup", false, "Demo in place move instead of writer")
31+
noPercent := flag.Bool("no-percent", false, "Disable percent display")
32+
noSuffix := flag.Bool("no-suffix", false, "Disable suffix display")
3133
flag.Parse()
3234
cfg := progressbar.DefaultConfig()
3335
cfg.UseColors = *colorFlag
3436
cfg.NoAnsi = *noAnsiFlag
37+
cfg.NoPercent = *noPercent
3538
cfg.ScreenWriter = os.Stdout // For playground, defaults to stderr otherwise.
3639
pb := cfg.NewBar()
3740
w := pb.Writer()
@@ -46,6 +49,9 @@ func main() {
4649
// exact number of 'pixels', just to demo every smooth step:
4750
n := pb.Width * 8
4851
for i := 0; i <= n; i++ {
52+
if !*noSuffix {
53+
pb.UpdateSuffix(fmt.Sprintf(" %d/%d", i, n))
54+
}
4955
pb.Progress(100. * float64(i) / float64(n))
5056
if moveUpMode && i%63 == 0 {
5157
pb.MoveCursorUp(1)

progressbar.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ type Config struct {
6060
Spinner bool
6161
// Prefix to show before the progress bar (can be updated while running using UpdatePrefix() or through Extra()).
6262
Prefix string
63+
// Suffix to show after the percentage information on the bar (can be updated while running using UpdateSuffix()).
64+
Suffix string
65+
// If NoPercent is true, the percentage is not shown on the bar (if the default %.1f%% format is not adequate).
66+
NoPercent bool
6367
// Minimum duration between updates (0 to update every time).
6468
UpdateInterval time.Duration
6569
// Option to avoid all ANSI sequences (useful for non terminal output/test/go playground),
@@ -96,6 +100,14 @@ func (bar *Bar) UpdatePrefix(p string) {
96100
bar.out.Unlock()
97101
}
98102

103+
// UpdateSuffix changes the suffix while the progress bar is running.
104+
// This is thread safe / acquires a shared lock to avoid issues on the output.
105+
func (bar *Bar) UpdateSuffix(s string) {
106+
bar.out.Lock()
107+
bar.Suffix = s
108+
bar.out.Unlock()
109+
}
110+
99111
// Progress shows a progress bar percentage (0-100%). On the same line as current line,
100112
// so when call repeatedly it will overwrite/update itself.
101113
// Use MoveCursorUp to move up to update other lines as needed or use Writer()
@@ -118,7 +130,7 @@ func (bar *Bar) Progress(progressPercent float64) {
118130
}
119131
barStr := ""
120132
progressPercentStr := ""
121-
if progressPercent >= 0 && progressPercent <= 100 {
133+
if progressPercent >= 0 && progressPercent <= 100 { //nolint:nestif // it's not that bad here
122134
width := float64(bar.Width)
123135
if width == 0 {
124136
width = DefaultWidth
@@ -137,7 +149,9 @@ func (bar *Bar) Progress(progressPercent float64) {
137149
reset = "▻" // "◣"
138150
}
139151
barStr = color + strings.Repeat(Full, fullCount) + FractionalBlocks[remainder] + strings.Repeat(Space, spaceCount) + reset
140-
progressPercentStr = fmt.Sprintf(" %.1f%%", progressPercent)
152+
if !bar.NoPercent {
153+
progressPercentStr = fmt.Sprintf(" %.1f%%", progressPercent)
154+
}
141155
}
142156
spinner := ""
143157
if bar.Spinner {
@@ -157,6 +171,7 @@ func (bar *Bar) Progress(progressPercent float64) {
157171
bar.out.buf = append(bar.out.buf, spinner...)
158172
bar.out.buf = append(bar.out.buf, barStr...)
159173
bar.out.buf = append(bar.out.buf, progressPercentStr...)
174+
bar.out.buf = append(bar.out.buf, bar.Suffix...)
160175
bar.out.buf = append(bar.out.buf, extra...)
161176
bar.out.buf = append(bar.out.buf, bar.indexBasedMoveUp()...)
162177
// bar.out.buf = append(bar.out.buf, '\n') // Uncomment to debug/see all the incremental updates.

0 commit comments

Comments
 (0)