Skip to content

Commit

Permalink
tetra: Display output that fits to terminal screen
Browse files Browse the repository at this point in the history
Detect available terminal lines and print only output
that fits on terminal.

This is enabled only when in periodical output with
cleared screen.

Signed-off-by: Jiri Olsa <[email protected]>
  • Loading branch information
olsajiri committed Dec 19, 2024
1 parent 381d397 commit 2662614
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
23 changes: 21 additions & 2 deletions cmd/tetra/debug/progs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/cilium/ebpf/link"
"github.com/spf13/cobra"
"golang.org/x/sys/unix"
"golang.org/x/term"
)

type prog struct {
Expand Down Expand Up @@ -233,13 +234,31 @@ func round(state map[uint32]*prog) error {
writer := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', tabwriter.AlignRight)
fmt.Fprintln(writer, "Ovh(%)\tId\tCnt\tTime\tName\tPin")

cnt := 0
lines := 0

if !cfg.noclr && !cfg.once {
// We have 3 header lines, so terminal smaller than that is too
// small to print anything meaningful
_, lines, _ = term.GetSize(0)
if lines < 4 {
return nil
}
lines = lines - 4
}

for _, ovh := range overheads {
p := ovh.p
fmt.Fprintf(writer, "%6.2f\t%d\t%d\t%d\t%s\t%s\n",
fmt.Fprintf(writer, "%6.2f\t%d\t%d\t%d\t%s\t%s",
ovh.pct, p.id, ovh.cnt, ovh.time, p.name, p.pin)

if lines != 0 && cnt == lines {
break
}
cnt++
fmt.Fprintf(writer, "\n")
}

fmt.Fprintln(writer)
writer.Flush()

// Remove stale programs from state map
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ require (
go.uber.org/multierr v1.11.0
golang.org/x/sync v0.10.0
golang.org/x/sys v0.28.0
golang.org/x/term v0.27.0
golang.org/x/time v0.8.0
google.golang.org/grpc v1.69.0
google.golang.org/protobuf v1.35.2
Expand Down Expand Up @@ -155,7 +156,6 @@ require (
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/term v0.27.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/tools v0.27.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
Expand Down

0 comments on commit 2662614

Please sign in to comment.