|
1 | 1 | package cli |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | 4 | "slices" |
6 | 5 | "strings" |
7 | 6 | "unicode" |
@@ -103,7 +102,7 @@ func DefaultShortHelp(c *CommandInfo) string { |
103 | 102 | if o.EnvVar != "" { |
104 | 103 | desc += " [$" + o.EnvVar + "]" |
105 | 104 | } |
106 | | - rightPadding := strings.Repeat(" ", (optNameColWidth-len(optLeftPaddedNames[i]))+3) |
| 105 | + rightPadding := strings.Repeat(" ", optNameColWidth-len(optLeftPaddedNames[i])+3) |
107 | 106 | paddedNameAndVal := " " + optLeftPaddedNames[i] + rightPadding |
108 | 107 | u.WriteString(paddedNameAndVal) |
109 | 108 | u.WriteString(wrapBlurb(desc, len(paddedNameAndVal), helpMsgTextWidth)) |
@@ -158,7 +157,20 @@ func DefaultShortHelp(c *CommandInfo) string { |
158 | 157 | } |
159 | 158 |
|
160 | 159 | if len(c.Subcmds) > 0 { |
161 | | - helpWriteSubcmds(&u, c) |
| 160 | + var maxCmdNameLen int |
| 161 | + for i := range c.Subcmds { |
| 162 | + if n := len(c.Subcmds[i].Name); n > maxCmdNameLen { |
| 163 | + maxCmdNameLen = n |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + u.WriteString("\ncommands:\n") |
| 168 | + for i := range c.Subcmds { |
| 169 | + rightPadding := strings.Repeat(" ", maxCmdNameLen-len(c.Subcmds[i].Name)+3) |
| 170 | + paddedNameCol := " " + c.Subcmds[i].Name + rightPadding |
| 171 | + u.WriteString(paddedNameCol) |
| 172 | + u.WriteString(wrapBlurb(c.Subcmds[i].HelpBlurb, len(paddedNameCol), helpMsgTextWidth) + "\n") |
| 173 | + } |
162 | 174 | } |
163 | 175 |
|
164 | 176 | return u.String() |
@@ -273,7 +285,37 @@ func DefaultFullHelp(c *CommandInfo) string { |
273 | 285 | } |
274 | 286 |
|
275 | 287 | if len(c.Subcmds) > 0 { |
276 | | - helpWriteSubcmds(&u, c) |
| 288 | + var maxCmdNameLen int |
| 289 | + var maxCmdBlurbLen int |
| 290 | + for i := range c.Subcmds { |
| 291 | + if n := len(c.Subcmds[i].Name); n > maxCmdNameLen { |
| 292 | + maxCmdNameLen = n |
| 293 | + } |
| 294 | + if n := len(c.Subcmds[i].HelpBlurb); n > maxCmdBlurbLen { |
| 295 | + maxCmdBlurbLen = n |
| 296 | + } |
| 297 | + } |
| 298 | + |
| 299 | + doNonCondensed := maxCmdNameLen > helpShortMsgMaxFirstColLen || |
| 300 | + maxCmdBlurbLen > (helpMsgTextWidth-maxCmdNameLen-6) |
| 301 | + |
| 302 | + u.WriteString("\ncommands:\n") |
| 303 | + for i := range c.Subcmds { |
| 304 | + if doNonCondensed { |
| 305 | + u.WriteString(" ") |
| 306 | + u.WriteString(c.Subcmds[i].Name) |
| 307 | + u.WriteString("\n ") |
| 308 | + u.WriteString(wrapBlurb(c.Subcmds[i].HelpBlurb, 6, helpMsgTextWidth)) |
| 309 | + u.WriteByte('\n') |
| 310 | + if i < len(c.Subcmds)-1 { |
| 311 | + u.WriteByte('\n') |
| 312 | + } |
| 313 | + } else { |
| 314 | + rightPadding := strings.Repeat(" ", maxCmdNameLen-len(c.Subcmds[i].Name)+3) |
| 315 | + paddedNameCol := " " + c.Subcmds[i].Name + rightPadding |
| 316 | + u.WriteString(paddedNameCol + c.Subcmds[i].HelpBlurb + "\n") |
| 317 | + } |
| 318 | + } |
277 | 319 | } |
278 | 320 |
|
279 | 321 | return u.String() |
@@ -346,20 +388,6 @@ func helpWriteUsageLines(u *strings.Builder, c *CommandInfo) { |
346 | 388 | } |
347 | 389 | } |
348 | 390 |
|
349 | | -func helpWriteSubcmds(u *strings.Builder, c *CommandInfo) { |
350 | | - var maxCmdNameLen int |
351 | | - for i := range c.Subcmds { |
352 | | - if n := len(c.Subcmds[i].Name); n > maxCmdNameLen { |
353 | | - maxCmdNameLen = n |
354 | | - } |
355 | | - } |
356 | | - |
357 | | - u.WriteString("\ncommands:\n") |
358 | | - for i := range c.Subcmds { |
359 | | - fmt.Fprintf(u, " %-*s %s\n", maxCmdNameLen, c.Subcmds[i].Name, c.Subcmds[i].HelpBlurb) |
360 | | - } |
361 | | -} |
362 | | - |
363 | 391 | func wrapBlurb(v string, indentLen, lineLen int) string { |
364 | 392 | s := wrapText(v, indentLen, lineLen) |
365 | 393 | return s[indentLen:] |
|
0 commit comments