Skip to content

Commit 34fd55c

Browse files
authored
Merge pull request #191 from unkmonster/working
add support for print text during render progress bar
2 parents a47c25c + 7070eb6 commit 34fd55c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

progressbar.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package progressbar
22

33
import (
4+
"bytes"
45
"errors"
56
"fmt"
67
"io"
@@ -121,6 +122,8 @@ type config struct {
121122

122123
// showDescriptionAtLineEnd specifies whether description should be written at line end instead of line start
123124
showDescriptionAtLineEnd bool
125+
126+
stdBuffer bytes.Buffer
124127
}
125128

126129
// Theme defines the elements of the bar
@@ -689,6 +692,7 @@ func (p *ProgressBar) render() error {
689692
if !p.state.finished && p.state.currentNum >= p.config.max {
690693
p.state.finished = true
691694
if !p.config.clearOnFinish {
695+
io.Copy(p.config.writer, &p.config.stdBuffer)
692696
renderProgressBar(p.config, &p.state)
693697
}
694698
if p.config.onCompletion != nil {
@@ -707,6 +711,7 @@ func (p *ProgressBar) render() error {
707711
}
708712

709713
// then, re-render the current progress bar
714+
io.Copy(p.config.writer, &p.config.stdBuffer)
710715
w, err := renderProgressBar(p.config, &p.state)
711716
if err != nil {
712717
return err
@@ -1142,3 +1147,23 @@ var termWidth = func() (width int, err error) {
11421147

11431148
return 0, err
11441149
}
1150+
1151+
func shouldCacheOutput(pb *ProgressBar) bool {
1152+
return !pb.state.finished && !pb.state.exit && !pb.config.invisible
1153+
}
1154+
1155+
func Bprintln(pb *ProgressBar, a ...interface{}) (int, error) {
1156+
if !shouldCacheOutput(pb) {
1157+
return fmt.Fprintln(pb.config.writer, a...)
1158+
} else {
1159+
return fmt.Fprintln(&pb.config.stdBuffer, a...)
1160+
}
1161+
}
1162+
1163+
func Bprintf(pb *ProgressBar, format string, a ...interface{}) (int, error) {
1164+
if !shouldCacheOutput(pb) {
1165+
return fmt.Fprintf(pb.config.writer, format, a...)
1166+
} else {
1167+
return fmt.Fprintf(&pb.config.stdBuffer, format, a...)
1168+
}
1169+
}

0 commit comments

Comments
 (0)