Skip to content

Commit 3fcc54e

Browse files
committed
Show progress only when logging to terminal
Logging progress to file is not useful and it also breaks the log format, adding empty line before the first progress, and leaving the last progress at the start of the next log line. Fix the terminal check to use os.Stderr, since both pb and logrus use os.Stderr. When not using a terminal, hide the progress bar. Fixes #2581 Signed-off-by: Nir Soffer <[email protected]>
1 parent 1090234 commit 3fcc54e

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

pkg/progressbar/progressbar.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ func New(size int64) (*pb.ProgressBar, error) {
1212
bar := pb.New64(size)
1313

1414
bar.Set(pb.Bytes, true)
15-
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
15+
16+
// Both logrous and pb use stderr by default.
17+
logFd := os.Stderr.Fd()
18+
19+
// Show progress only when logging to terminal.
20+
if isatty.IsTerminal(logFd) || isatty.IsCygwinTerminal(logFd) {
1621
bar.SetTemplateString(`{{counters . }} {{bar . | green }} {{percent .}} {{speed . "%s/s"}}`)
1722
bar.SetRefreshRate(200 * time.Millisecond)
1823
} else {
19-
bar.Set(pb.Terminal, false)
20-
bar.Set(pb.ReturnSymbol, "\n")
21-
bar.SetTemplateString(`{{counters . }} ({{percent .}}) {{speed . "%s/s"}}`)
22-
bar.SetRefreshRate(5 * time.Second)
24+
bar.Set(pb.Static, true)
2325
}
26+
2427
bar.SetWidth(80)
2528
if err := bar.Err(); err != nil {
2629
return nil, err

0 commit comments

Comments
 (0)