Skip to content

Commit 763d715

Browse files
committed
πŸ› fix(docs.go): split UsageText into an array of strings
πŸ› fix(fish_test.go): add EXAMPLE_VARIABLE_NAME to another-flag's EnvVars ✨ feat(template.go): add support for multiline UsageText in MarkdownTabularDocTemplate
1 parent b857483 commit 763d715

File tree

4 files changed

+89
-46
lines changed

4 files changed

+89
-46
lines changed

β€Ždocs.go

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (a *App) ToTabularMarkdown(opts ...TabularOption) (string, error) {
6060
Name: a.Name,
6161
Description: tt.PrepareMultilineString(a.Description),
6262
Usage: tt.PrepareMultilineString(a.Usage),
63-
UsageText: tt.PrepareMultilineString(a.UsageText),
63+
UsageText: strings.FieldsFunc(a.UsageText, func(r rune) bool { return r == '\n' }),
6464
ArgsUsage: tt.PrepareMultilineString(a.ArgsUsage),
6565
GlobalFlags: tt.PrepareFlags(a.VisibleFlags()),
6666
Commands: tt.PrepareCommands(a.VisibleCommands(), o.appPath, "", 0),
@@ -256,24 +256,26 @@ func prepareUsage(command *Command, usageText string) string {
256256

257257
type (
258258
cliTabularAppTemplate struct {
259-
AppPath string
260-
Name string
261-
Usage, UsageText, ArgsUsage string
262-
Description string
263-
GlobalFlags []cliTabularFlagTemplate
264-
Commands []cliTabularCommandTemplate
259+
AppPath string
260+
Name string
261+
Usage, ArgsUsage string
262+
UsageText []string
263+
Description string
264+
GlobalFlags []cliTabularFlagTemplate
265+
Commands []cliTabularCommandTemplate
265266
}
266267

267268
cliTabularCommandTemplate struct {
268-
AppPath string
269-
Name string
270-
Aliases []string
271-
Usage, UsageText, ArgsUsage string
272-
Description string
273-
Category string
274-
Flags []cliTabularFlagTemplate
275-
SubCommands []cliTabularCommandTemplate
276-
Level uint
269+
AppPath string
270+
Name string
271+
Aliases []string
272+
Usage, ArgsUsage string
273+
UsageText []string
274+
Description string
275+
Category string
276+
Flags []cliTabularFlagTemplate
277+
SubCommands []cliTabularCommandTemplate
278+
Level uint
277279
}
278280

279281
cliTabularFlagTemplate struct {
@@ -299,7 +301,7 @@ func (tt tabularTemplate) PrepareCommands(commands []*Command, appPath, parentCo
299301
Name: strings.TrimSpace(strings.Join([]string{parentCommandName, cmd.Name}, " ")),
300302
Aliases: cmd.Aliases,
301303
Usage: tt.PrepareMultilineString(cmd.Usage),
302-
UsageText: tt.PrepareMultilineString(cmd.UsageText),
304+
UsageText: strings.FieldsFunc(cmd.UsageText, func(r rune) bool { return r == '\n' }),
303305
ArgsUsage: tt.PrepareMultilineString(cmd.ArgsUsage),
304306
Description: tt.PrepareMultilineString(cmd.Description),
305307
Category: cmd.Category,
@@ -375,8 +377,14 @@ func (tabularTemplate) PrepareMultilineString(s string) string {
375377
}
376378

377379
func (tabularTemplate) Prettify(s string) string {
378-
s = regexp.MustCompile(`\n{2,}`).ReplaceAllString(s, "\n\n") // normalize newlines
379-
s = strings.Trim(s, " \n") // trim spaces and newlines
380+
var max = func(x, y int) int {
381+
if x > y {
382+
return x
383+
}
384+
return y
385+
}
386+
387+
var b strings.Builder
380388

381389
// search for tables
382390
for _, rawTable := range regexp.MustCompile(`(?m)^(\|[^\n]+\|\r?\n)((?:\|:?-+:?)+\|)(\n(?:\|[^\n]+\|\r?\n?)*)?$`).FindAllString(s, -1) {
@@ -406,11 +414,14 @@ func (tabularTemplate) Prettify(s string) string {
406414

407415
// calculate max lengths
408416
var lengths = make([]int, len(matrix[0]))
409-
const padding = 2 // 2 spaces for padding
410-
for _, row := range matrix {
417+
for n, row := range matrix {
411418
for i, cell := range row {
412-
if len(cell) > lengths[i]-padding {
413-
lengths[i] = utf8.RuneCountInString(cell) + padding
419+
if n == 1 {
420+
continue // skip separator
421+
}
422+
423+
if l := utf8.RuneCountInString(cell); l > lengths[i] {
424+
lengths[i] = l
414425
}
415426
}
416427
}
@@ -420,37 +431,57 @@ func (tabularTemplate) Prettify(s string) string {
420431
for j, cell := range row {
421432
if i == 1 { // is separator
422433
if centered[j] {
423-
cell = ":" + strings.Repeat("-", lengths[j]-2) + ":"
434+
b.Reset()
435+
b.WriteRune(':')
436+
b.WriteString(strings.Repeat("-", max(0, lengths[j])))
437+
b.WriteRune(':')
438+
439+
row[j] = b.String()
424440
} else {
425-
cell = strings.Repeat("-", lengths[j]+1)
441+
row[j] = strings.Repeat("-", max(0, lengths[j]+2))
426442
}
443+
444+
continue
427445
}
428446

429447
var (
430-
padLeft, padRight = 1, 1
431448
cellWidth = utf8.RuneCountInString(cell)
449+
padLeft, padRight = 1, max(1, lengths[j]-cellWidth+1) // align to the left
432450
)
433451

434452
if centered[j] { // is centered
435-
padLeft = (lengths[j] - cellWidth) / 2
436-
padRight = lengths[j] - cellWidth - padLeft
437-
} else if i == 1 { // is header
438-
padLeft, padRight = 0, 0
439-
} else { // align to the left
440-
padRight = lengths[j] - cellWidth
453+
padLeft = max(1, (lengths[j]-cellWidth)/2)
454+
padRight = max(1, lengths[j]-cellWidth-(padLeft-1))
441455
}
442456

443-
row[j] = strings.Repeat(" ", padLeft) + cell + strings.Repeat(" ", padRight)
457+
b.Reset()
458+
b.WriteString(strings.Repeat(" ", padLeft))
459+
460+
if padLeft+cellWidth+padRight <= lengths[j]+1 {
461+
b.WriteRune(' ') // add an extra space if the cell is not full
462+
}
463+
464+
b.WriteString(cell)
465+
b.WriteString(strings.Repeat(" ", padRight))
466+
467+
row[j] = b.String()
444468
}
445469
}
446470

447-
var newTable string
471+
b.Reset()
472+
448473
for _, row := range matrix { // build new table
449-
newTable += "|" + strings.Join(row, "|") + "|\n"
474+
b.WriteRune('|')
475+
b.WriteString(strings.Join(row, "|"))
476+
b.WriteRune('|')
477+
b.WriteRune('\n')
450478
}
451479

452-
s = strings.Replace(s, rawTable, newTable, 1)
480+
s = strings.Replace(s, rawTable, b.String(), 1)
453481
}
454482

483+
s = regexp.MustCompile(`\n{2,}`).ReplaceAllString(s, "\n\n") // normalize newlines
484+
s = strings.Trim(s, " \n") // trim spaces and newlines
485+
455486
return s + "\n" // add an extra newline
456487
}

β€Žfish_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func testApp() *App {
3939
Name: "another-flag",
4040
Aliases: []string{"b"},
4141
Usage: "another usage text",
42+
EnvVars: []string{"EXAMPLE_VARIABLE_NAME"},
4243
},
4344
&BoolFlag{
4445
Name: "hidden-flag",

β€Žtemplate.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ var MarkdownTabularDocTemplate = `{{ define "flags" }}
146146
{{ .Usage }}.
147147
{{ end }}
148148
{{ if .UsageText }}
149-
> {{ .UsageText }}.
149+
{{ range $line := .UsageText -}}
150+
> {{ $line }}
151+
{{ end -}}
150152
{{ end }}
151153
{{ if .Description }}
152154
{{ .Description }}.
@@ -176,7 +178,9 @@ The following flags are supported:
176178
{{ if .Usage }}{{ .Usage }}.
177179
{{ end }}
178180
{{ if .UsageText }}
179-
> {{ .UsageText }}.
181+
{{ range $line := .UsageText -}}
182+
> {{ $line }}
183+
{{ end -}}
180184
{{ end }}
181185
Usage:
182186

β€Žtestdata/expected-tabular-markdown-full.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Description of the application.
44

55
Some app.
66

7-
> app [first_arg] [second_arg].
7+
> app [first_arg] [second_arg]
88
99
Usage:
1010

@@ -14,11 +14,11 @@ $ app [GLOBAL FLAGS] [COMMAND] [COMMAND FLAGS] [ARGUMENTS...]
1414

1515
Global flags:
1616

17-
| Name | Description | Default value | Environment variables |
18-
|------------------------------|---------------------|:---------------:|:-----------------------:|
19-
| `--socket="…"` (`-s`) | some 'usage' text | `value` | *none* |
20-
| `--flag="…"` (`--fl`, `-f`) | | | *none* |
21-
| `--another-flag` (`-b`) | another usage text | `false` | *none* |
17+
| Name | Description | Default value | Environment variables |
18+
|-----------------------------|--------------------|:-------------:|:-----------------------:|
19+
| `--socket="…"` (`-s`) | some 'usage' text | `value` | *none* |
20+
| `--flag="…"` (`--fl`, `-f`) | | | *none* |
21+
| `--another-flag` (`-b`) | another usage text | `false` | `EXAMPLE_VARIABLE_NAME` |
2222

2323
### `config` command (aliases: `c`)
2424

@@ -76,7 +76,14 @@ $ app [GLOBAL FLAGS] some-command [ARGUMENTS...]
7676

7777
standard usage text.
7878

79-
> Usage for the usage text - formatted: Based on the specified ConfigMap and summon secrets.yml - list: Inspect the environment for a specific process running on a Pod - for_effect: Compare 'namespace' environment with 'local' ``` func() { ... } ``` Should be a part of the same code block.
79+
> Usage for the usage text
80+
> - formatted: Based on the specified ConfigMap and summon secrets.yml
81+
> - list: Inspect the environment for a specific process running on a Pod
82+
> - for_effect: Compare 'namespace' environment with 'local'
83+
> ```
84+
> func() { ... }
85+
> ```
86+
> Should be a part of the same code block
8087
8188
Usage:
8289
@@ -95,7 +102,7 @@ The following flags are supported:
95102

96103
standard usage text.
97104

98-
> Single line of UsageText.
105+
> Single line of UsageText
99106
100107
Usage:
101108

0 commit comments

Comments
Β (0)