Skip to content

Commit

Permalink
Merge pull request #191 from unkmonster/working
Browse files Browse the repository at this point in the history
add support for print text during render progress bar
  • Loading branch information
schollz authored Aug 2, 2024
2 parents a47c25c + 7070eb6 commit 34fd55c
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions progressbar.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package progressbar

import (
"bytes"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -121,6 +122,8 @@ type config struct {

// showDescriptionAtLineEnd specifies whether description should be written at line end instead of line start
showDescriptionAtLineEnd bool

stdBuffer bytes.Buffer
}

// Theme defines the elements of the bar
Expand Down Expand Up @@ -689,6 +692,7 @@ func (p *ProgressBar) render() error {
if !p.state.finished && p.state.currentNum >= p.config.max {
p.state.finished = true
if !p.config.clearOnFinish {
io.Copy(p.config.writer, &p.config.stdBuffer)

Check failure on line 695 in progressbar.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `io.Copy` is not checked (errcheck)
renderProgressBar(p.config, &p.state)

Check failure on line 696 in progressbar.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value is not checked (errcheck)
}
if p.config.onCompletion != nil {
Expand All @@ -707,6 +711,7 @@ func (p *ProgressBar) render() error {
}

// then, re-render the current progress bar
io.Copy(p.config.writer, &p.config.stdBuffer)

Check failure on line 714 in progressbar.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `io.Copy` is not checked (errcheck)
w, err := renderProgressBar(p.config, &p.state)
if err != nil {
return err
Expand Down Expand Up @@ -1142,3 +1147,23 @@ var termWidth = func() (width int, err error) {

return 0, err
}

func shouldCacheOutput(pb *ProgressBar) bool {
return !pb.state.finished && !pb.state.exit && !pb.config.invisible
}

func Bprintln(pb *ProgressBar, a ...interface{}) (int, error) {
if !shouldCacheOutput(pb) {
return fmt.Fprintln(pb.config.writer, a...)
} else {
return fmt.Fprintln(&pb.config.stdBuffer, a...)
}
}

func Bprintf(pb *ProgressBar, format string, a ...interface{}) (int, error) {
if !shouldCacheOutput(pb) {
return fmt.Fprintf(pb.config.writer, format, a...)
} else {
return fmt.Fprintf(&pb.config.stdBuffer, format, a...)
}
}

0 comments on commit 34fd55c

Please sign in to comment.