Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for print text during render progress bar #191

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

// 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 @@ -349,7 +352,7 @@
b.config.useIECUnits)

if b.config.renderWithBlankState {
b.RenderBlank()

Check failure on line 355 in progressbar.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `b.RenderBlank` is not checked (errcheck)
}

return &b
Expand Down Expand Up @@ -607,7 +610,7 @@
if p.config.invisible {
return
}
p.render()

Check failure on line 613 in progressbar.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `p.render` is not checked (errcheck)
}

// New64 returns a new ProgressBar
Expand Down Expand Up @@ -655,7 +658,7 @@

p.lock.Unlock() // so p.Add can lock

p.Add(0) // re-render

Check failure on line 661 in progressbar.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `p.Add` is not checked (errcheck)
}

// IsFinished returns true if progress bar is completed
Expand Down Expand Up @@ -689,7 +692,8 @@
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 {
p.config.onCompletion()
Expand All @@ -707,6 +711,7 @@
}

// 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 @@ -1042,7 +1047,7 @@
// ignore any errors in Sync(), as stdout
// can't be synced on some operating systems
// like Debian 9 (Stretch)
f.Sync()

Check failure on line 1050 in progressbar.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `f.Sync` is not checked (errcheck)
}

return nil
Expand All @@ -1065,7 +1070,7 @@
// Read will read the data and add the number of bytes to the progressbar
func (r *Reader) Read(p []byte) (n int, err error) {
n, err = r.Reader.Read(p)
r.bar.Add(n)

Check failure on line 1073 in progressbar.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `r.bar.Add` is not checked (errcheck)
return
}

Expand All @@ -1074,7 +1079,7 @@
if closer, ok := r.Reader.(io.Closer); ok {
return closer.Close()
}
r.bar.Finish()

Check failure on line 1082 in progressbar.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `r.bar.Finish` is not checked (errcheck)
return
}

Expand Down Expand Up @@ -1142,3 +1147,23 @@

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...)
}
}
Loading