1
1
package progressbar
2
2
3
3
import (
4
+ "bytes"
4
5
"errors"
5
6
"fmt"
6
7
"io"
@@ -121,6 +122,8 @@ type config struct {
121
122
122
123
// showDescriptionAtLineEnd specifies whether description should be written at line end instead of line start
123
124
showDescriptionAtLineEnd bool
125
+
126
+ stdBuffer bytes.Buffer
124
127
}
125
128
126
129
// Theme defines the elements of the bar
@@ -689,6 +692,7 @@ func (p *ProgressBar) render() error {
689
692
if ! p .state .finished && p .state .currentNum >= p .config .max {
690
693
p .state .finished = true
691
694
if ! p .config .clearOnFinish {
695
+ io .Copy (p .config .writer , & p .config .stdBuffer )
692
696
renderProgressBar (p .config , & p .state )
693
697
}
694
698
if p .config .onCompletion != nil {
@@ -707,6 +711,7 @@ func (p *ProgressBar) render() error {
707
711
}
708
712
709
713
// then, re-render the current progress bar
714
+ io .Copy (p .config .writer , & p .config .stdBuffer )
710
715
w , err := renderProgressBar (p .config , & p .state )
711
716
if err != nil {
712
717
return err
@@ -1142,3 +1147,23 @@ var termWidth = func() (width int, err error) {
1142
1147
1143
1148
return 0 , err
1144
1149
}
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