Skip to content

Show progress only when logging to terminal #2582

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

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ var HideProgress bool

// hideBar is used only for testing.
func hideBar(bar *pb.ProgressBar) {
bar.Set(pb.ReturnSymbol, "")
bar.SetTemplateString("")
bar.Set(pb.Static, true)
}

type Status = string
Expand Down
13 changes: 8 additions & 5 deletions pkg/progressbar/progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ func New(size int64) (*pb.ProgressBar, error) {
bar := pb.New64(size)

bar.Set(pb.Bytes, true)
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {

// Both logrous and pb use stderr by default.
logFd := os.Stderr.Fd()

// Show progress only when logging to terminal.
if isatty.IsTerminal(logFd) || isatty.IsCygwinTerminal(logFd) {
bar.SetTemplateString(`{{counters . }} {{bar . | green }} {{percent .}} {{speed . "%s/s"}}`)
bar.SetRefreshRate(200 * time.Millisecond)
} else {
bar.Set(pb.Terminal, false)
bar.Set(pb.ReturnSymbol, "\n")
bar.SetTemplateString(`{{counters . }} ({{percent .}}) {{speed . "%s/s"}}`)
bar.SetRefreshRate(5 * time.Second)
bar.Set(pb.Static, true)
}

bar.SetWidth(80)
if err := bar.Err(); err != nil {
return nil, err
Expand Down
Loading