Skip to content

Make cobra help output respect the terminal width - #6

Merged
gurnben merged 1 commit into
mainfrom
fix/help-wrapping
Oct 21, 2026
Merged

gurnben merged 1 commit into
mainfrom
fix/help-wrapping

Conversation

@gurnben

@gurnben gurnben commented Oct 21, 2026

Copy link
Copy Markdown
Member

Summary

Help was the last unwrapped output surface: cobra's default template renders flag usages via FlagUsages, which never wraps — long descriptions hard-wrapped at the terminal's right edge with continuations at column zero while the flags sat indented mid-line. Long descriptions rendered verbatim, and cobra's built-in completion summaries overflowed narrow terminals.

  • Flag usages now wrap with continuations aligned under each description: the usage template is patched in place (not copied, so it tracks cobra's default across versions) to use pflag's FlagUsagesWrapped at the measured terminal width. Templates inherit from the root, covering every subcommand.
  • Long descriptions re-wrap at runtime to the current width.
  • The trailing Use "… [command] --help" hint breaks onto two lines below 78 columns.
  • Cobra's built-in completion commands are hidden from the command list (their summaries don't fit narrow terminals; the commands remain invocable).
  • Over-long command summaries shortened to fit the list column.
  • The init --forge default URL moved out of the flag line into the usage text — an unbreakable token that cannot fit pflag's description column at narrow widths (the wizard still shows it as the placeholder, and empty --forge keeps the same default).

Verification

Every help screen (--help on root, run, init, report, ledger subcommands) captured in a PTY at 60 and 80 columns with ANSI-aware width measurement: zero over-width lines, continuations aligned. Full suite with race detector, vet, staticcheck clean.

💘 Generated with Crush

Help was the last unwrapped surface: cobra's default template emits
flag usages via FlagUsages, which never wraps, so long descriptions
hard-wrapped at the terminal's right edge with continuations at column
zero while the flags themselves sat indented mid-line. Long
descriptions rendered verbatim, and cobra's built-in completion
summaries overflowed narrow terminals.

The usage template is now patched in place (not copied, so it tracks
cobra's default) to use pflag's FlagUsagesWrapped at the measured
width, which aligns continuations under each description. Long
descriptions re-wrap at runtime, the trailing use-hint breaks below 78
columns, the completion commands are hidden from the list (still
invocable), command summaries were shortened to fit the list column,
and the init --forge default URL moved out of the flag line — an
unbreakable token that cannot fit pflag's description column at narrow
widths.

Verified by capturing every help screen in a PTY at 60 and 80 columns
and measuring display widths: zero over-width lines, continuations
aligned.

💘 Generated with Crush

Assisted-by: Crush:rits/zai-org/glm-5-3
@gurnben
gurnben requested a review from a team as a code owner October 21, 2026 15:19
@gurnben
gurnben merged commit 43cff46 into main Oct 21, 2026
2 checks passed
@gurnben
gurnben deleted the fix/help-wrapping branch October 21, 2026 15:21
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.

1 participant