Skip to content

Commit 333475e

Browse files
committed
fix(help): print description on newlines for long flags
Follow-up to #2944. When flags are over 21 characters, the description is printed on a new line to avoid the flag from intersecting with the description and keeping everything aligned. This mimics what pacman does for long flags like `--disable-sandbox`. I decided to add logic to solve this instead of just adding a newline to the `--completioninterval` description (the only flag currently over 21 characters) so any future long flags added will automatically be aligned too.
1 parent 328f4b4 commit 333475e

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

cmd.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ func findYayOperationHelp(operation string) *operationHelp {
9292
}
9393

9494
func printUsageOptions(logger *text.Logger, title string, options []usageOption) {
95+
flagWidth := 21
96+
9597
logger.Println("\n" + title)
9698
for _, option := range options {
9799
if option.flags == "" && option.description == "" {
@@ -100,9 +102,15 @@ func printUsageOptions(logger *text.Logger, title string, options []usageOption)
100102
}
101103

102104
descriptionLines := strings.Split(option.description, "\n")
103-
logger.Printf(" %-21s %s\n", option.flags, descriptionLines[0])
105+
if len(option.flags) > flagWidth {
106+
// Print the description on the next line if the flags are too long
107+
logger.Printf(" %s\n", option.flags)
108+
logger.Printf(" %-*s %s\n", flagWidth, "", descriptionLines[0])
109+
} else {
110+
logger.Printf(" %-*s %s\n", flagWidth, option.flags, descriptionLines[0])
111+
}
104112
for _, line := range descriptionLines[1:] {
105-
logger.Printf(" %-21s %s\n", "", line)
113+
logger.Printf(" %-*s %s\n", flagWidth, "", line)
106114
}
107115
}
108116
}

0 commit comments

Comments
 (0)