-
Notifications
You must be signed in to change notification settings - Fork 11
Fixed indentation issues for long commands. #286
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
Conversation
Reviewer's GuideAdjusts Cobra command help formatting to align long command names and internal commands, and adds tests covering long-name layout in both grouped and ungrouped help output. Flow diagram for buildGroupCommandHelp with new alignment helpersflowchart TD
A[buildGroupCommandHelp called with cmd and group] --> B[Initialize help string and empty cmds slice]
B --> C[Collect commands in group into cmds]
C --> D[Compute padding using maxColumnNameLen cmd]
D --> E[Sort cmds by name]
E --> F[Build subGroupNameList from command annotations]
F --> G[Append group.Title to help]
G --> H[Determine intend string using hasSubgroup cmd]
H --> I{For each subgroupName in subGroupNameList}
I -->|Yes| J[If multiple subgroups append subgroup header to help]
J --> K[For each command in cmds with matching SubGroupName]
K --> L[Append formatted line using rpad command name and padding]
I -->|No more subgroups| M[For each command in cmds without SubGroupName]
M --> N[Append formatted line using rpad command name and padding plus intend]
N --> O[Return help string]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey - I've found 1 issue, and left some high level feedback:
- The
hasSubgrouphelper duplicates work already done when buildingsubGroupNameList; you can drop the extra traversal and simply derive the indent fromlen(subGroupNameList)to avoid additional allocations and iteration. - The
intendvariable appears to be a misspelling ofindent; renaming it will make the indentation logic easier to follow and less confusing. - If command names may contain non-ASCII characters,
maxColumnNameLenshould useutf8.RuneCountInStringinstead oflento keep the help columns visually aligned with multibyte characters.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The `hasSubgroup` helper duplicates work already done when building `subGroupNameList`; you can drop the extra traversal and simply derive the indent from `len(subGroupNameList)` to avoid additional allocations and iteration.
- The `intend` variable appears to be a misspelling of `indent`; renaming it will make the indentation logic easier to follow and less confusing.
- If command names may contain non-ASCII characters, `maxColumnNameLen` should use `utf8.RuneCountInString` instead of `len` to keep the help columns visually aligned with multibyte characters.
## Individual Comments
### Comment 1
<location> `cmd/root.go:104` </location>
<code_context>
}
}
+ padding := maxColumnNameLen(cmd)
+
sort.Slice(cmds, func(i, j int) bool {
</code_context>
<issue_to_address>
**suggestion:** Column width calculation may misalign multi-byte (non-ASCII) command names
`maxColumnNameLen` and `rpad` rely on `len(s)`, which measures bytes, not display cells. For non-ASCII command names (e.g., emoji, CJK), this will miscompute padding and misalign columns. If such names are supported, use `utf8.RuneCountInString` or a display-width–aware library instead of byte length.
Suggested implementation:
```golang
import (
"fmt"
"unicode/utf8"
"github.com/spf13/cobra"
)
```
```golang
func maxColumnNameLen(cmd *cobra.Command) int {
max := 0
for _, c := range cmd.Commands() {
nameLen := utf8.RuneCountInString(c.Name())
if nameLen > max {
max = nameLen
}
}
return max
}
```
```golang
func rpad(s string, padding int) string {
// padding is the desired rune width of the column
runeLen := utf8.RuneCountInString(s)
if runeLen >= padding {
return s
}
// number of spaces to add to reach the target rune width
spaceCount := padding - runeLen
return s + fmt.Sprintf("%*s", spaceCount, "")
}
```
If `maxColumnNameLen` or `rpad` are defined with slightly different signatures or logic in your actual file, you should adapt the SEARCH blocks to match the exact existing implementations (e.g., different variable names or comments). The core changes are:
1. Replace all `len(<string>)` calls used for column-width computation with `utf8.RuneCountInString(<string>)`.
2. Ensure any padding calculations that depend on string length use rune counts instead of byte counts.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
cmd/root.go
Outdated
| } | ||
| } | ||
|
|
||
| padding := maxColumnNameLen(cmd) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Column width calculation may misalign multi-byte (non-ASCII) command names
maxColumnNameLen and rpad rely on len(s), which measures bytes, not display cells. For non-ASCII command names (e.g., emoji, CJK), this will miscompute padding and misalign columns. If such names are supported, use utf8.RuneCountInString or a display-width–aware library instead of byte length.
Suggested implementation:
import (
"fmt"
"unicode/utf8"
"github.com/spf13/cobra"
)func maxColumnNameLen(cmd *cobra.Command) int {
max := 0
for _, c := range cmd.Commands() {
nameLen := utf8.RuneCountInString(c.Name())
if nameLen > max {
max = nameLen
}
}
return max
}func rpad(s string, padding int) string {
// padding is the desired rune width of the column
runeLen := utf8.RuneCountInString(s)
if runeLen >= padding {
return s
}
// number of spaces to add to reach the target rune width
spaceCount := padding - runeLen
return s + fmt.Sprintf("%*s", spaceCount, "")
}If maxColumnNameLen or rpad are defined with slightly different signatures or logic in your actual file, you should adapt the SEARCH blocks to match the exact existing implementations (e.g., different variable names or comments). The core changes are:
- Replace all
len(<string>)calls used for column-width computation withutf8.RuneCountInString(<string>). - Ensure any padding calculations that depend on string length use rune counts instead of byte counts.
| return fmt.Sprintf(formattedString, s) | ||
| } | ||
|
|
||
| func hasSubgroup(cmd *cobra.Command) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Call it for each group. I didn’t want to call it before buildGroupCommandHelp and pass it as a parameter. Since we only have two groups, this is probably fine.
Summary by Sourcery
Improve CLI help output formatting to correctly align command names and descriptions, including for very long command names and grouped commands.
Bug Fixes:
Enhancements:
Tests: