Skip to content
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
1 change: 1 addition & 0 deletions cmd/catp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ catp [OPTIONS] PATH ...
POST /rate-limit?value=X sets a new one (0 or less means unlimited)
-http-auth string
user:pass to require as HTTP basic auth for -http endpoints
-i rewrite progress line in place (terminal only)
-l count lines
-no-progress
disable progress printing
Expand Down
4 changes: 3 additions & 1 deletion cmd/catp/catp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func Main(options ...func(o *Options)) error { //nolint:funlen,cyclop,gocognit,g
cpuProfile := flag.String("dbg-cpu-prof", "", "write first 10 seconds of CPU profile to file")
memProfile := flag.String("dbg-mem-prof", "", "write heap profile to file after 10 seconds")
output := flag.String("output", "", "output to file (can have .gz or .zst ext for compression) instead of STDOUT")
flag.BoolVar(&r.progressInPlace, "i", false, "rewrite progress line in place (terminal only)")
flag.BoolVar(&r.noProgress, "no-progress", false, "disable progress printing")
flag.BoolVar(&r.countLines, "l", false, "count lines")
flag.Float64Var(&r.rateLimit, "rate-limit", 0, "output rate limit lines per second")
Expand Down Expand Up @@ -228,9 +229,10 @@ func Main(options ...func(o *Options)) error { //nolint:funlen,cyclop,gocognit,g
return
}

println(s)
r.pr.Println(s)
},
}
r.pr.InPlace = r.progressInPlace

for _, fn := range files {
if fn == "-" {
Expand Down
5 changes: 3 additions & 2 deletions cmd/catp/catp/catp.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ type runner struct {
lastStatus string
speed []speedSample // Kept only with -http, for the status page charts.

noProgress bool
countLines bool
noProgress bool
progressInPlace bool
countLines bool

startLine int
endLine int
Expand Down
31 changes: 30 additions & 1 deletion progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import (
"fmt"
"io"
"os"
"runtime"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -35,6 +36,9 @@
// IncrementalSpeed shows speed and remaining estimate based on performance between two status updates.
IncrementalSpeed bool

// InPlace rewrites the status line in place instead of appending lines, only effective if stderr is a terminal.
InPlace bool

done chan bool
task Task
lines func() int64
Expand Down Expand Up @@ -150,7 +154,7 @@
p.prnt = p.Print
if p.prnt == nil {
p.prnt = func(s Status) {
println(DefaultStatus(s))
p.Println(DefaultStatus(s))

Check notice on line 157 in progress.go

View workflow job for this annotation

GitHub Actions / test (stable)

1 statement(s) on lines 157:158 are not covered by tests.
}
}

Expand Down Expand Up @@ -266,10 +270,35 @@
}
}

// Println prints a status line to stderr, honoring InPlace.
func (p *Progress) Println(s string) {
if p.inPlace() {
print("\r" + s + "\033[K") // \033[K erases leftovers of a longer previous line.

return
}

Check notice on line 279 in progress.go

View workflow job for this annotation

GitHub Actions / test (stable)

3 statement(s) are not covered by tests.

println(s)

Check notice on line 281 in progress.go

View workflow job for this annotation

GitHub Actions / test (stable)

1 statement(s) are not covered by tests.
}

func (p *Progress) inPlace() bool {
if !p.InPlace {
return false
}

Check notice on line 287 in progress.go

View workflow job for this annotation

GitHub Actions / test (stable)

2 statement(s) are not covered by tests.

st, err := os.Stderr.Stat()

return err == nil && st.Mode()&os.ModeCharDevice != 0

Check notice on line 291 in progress.go

View workflow job for this annotation

GitHub Actions / test (stable)

2 statement(s) are not covered by tests.
}

// Stop stops progress reporting.
func (p *Progress) Stop() {
p.printStatus(true)

if p.inPlace() {
println() // Keep the final status and move to a fresh line.
}

Check notice on line 300 in progress.go

View workflow job for this annotation

GitHub Actions / test (stable)

2 statement(s) are not covered by tests.

if !p.task.Continue {
p.metrics = nil
}
Expand Down
Loading