Skip to content

fix: wrap wide license columns to the terminal width - #556

Open
Eljees wants to merge 1 commit into
anchore:mainfrom
Eljees:agent/wrap-wide-columns
Open

fix: wrap wide license columns to the terminal width#556
Eljees wants to merge 1 commit into
anchore:mainfrom
Eljees:agent/wrap-wide-columns

Conversation

@Eljees

@Eljees Eljees commented Jul 28, 2026

Copy link
Copy Markdown

Fixes #276

Problem

A package that carries several licenses makes the license cell very wide, so the row runs far past the edge of the terminal and the table becomes hard to read (the issue shows a jasper-libs row spilling across the screen).

The tables are built with go-pretty but no column width is configured anywhere, so a cell is rendered at its natural width regardless of how wide the terminal actually is.

Change

Adds a small helper in cmd/grant/cli/internal:

  • TerminalWidth() — the width of the terminal attached to stdout, or 0 when stdout is not a terminal or the size can't be determined (golang.org/x/term is already a dependency).
  • WrapWideColumns(t, columns...) — sets WidthMax on the named columns so go-pretty wraps inside the column instead of widening the row.

Applied to the six tables that carry a license column (output.go ×2, list.go ×3, check.go ×1), matched by header name rather than index.

Two deliberate properties:

  • Redirected output is untouched. When stdout isn't a terminal the helper returns early and sets no column config, so piping to a file or another process produces exactly the same bytes as today. That keeps the CLI tests and any downstream parsing unaffected.
  • Wrapping, not truncating, so no license information is lost.

The share of the terminal given to the license column (half, with a floor of 20 columns) is a heuristic — happy to change the split or make it configurable if you'd prefer something else.

Tests

wrappedColumnWidth is a pure function so the width policy is testable without a TTY; TestWrappedColumnWidth covers the unknown-width, negative, wide-terminal and narrow-terminal cases.

=== RUN   TestWrappedColumnWidth
--- PASS: TestWrappedColumnWidth/unknown_width_does_not_wrap
--- PASS: TestWrappedColumnWidth/negative_width_does_not_wrap
--- PASS: TestWrappedColumnWidth/wide_terminal_gets_half_its_width
--- PASS: TestWrappedColumnWidth/narrow_terminal_keeps_a_readable_minimum
ok  	github.com/anchore/grant/cmd/grant/cli/internal	0.124s

go build ./... is clean across the repo. I wasn't able to finish a full go test ./... locally — the machine I had available couldn't get through the CLI integration suite in reasonable time — so I'd lean on CI for that.

A package carrying several licenses makes the row far wider than the
terminal, so the table output wraps at arbitrary points and becomes hard
to read.

Limit the license columns to half the terminal width, letting go-pretty
wrap inside the column instead of pushing the row off screen. The width
is only applied when stdout is a terminal, so piped and redirected
output keeps exactly the shape it has today.

Signed-off-by: Eljees <3.14hell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: output can span many lines, making it difficult to read

1 participant