Skip to content

Commit 7f52074

Browse files
committed
make sure subcommands are properly aligned
Entire-Checkpoint: 942f12e5f6c3
1 parent eb75c06 commit 7f52074

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

cmd/entire/cli/labs.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cli
33
import (
44
"fmt"
55
"strings"
6+
"unicode/utf8"
67

78
"github.com/spf13/cobra"
89
)
@@ -94,10 +95,17 @@ Try:
9495
}
9596

9697
func renderExperimentalCommands(commands []experimentalCommandInfo) string {
98+
width := 0
99+
for _, info := range commands {
100+
if w := utf8.RuneCountInString(info.Invocation); w > width {
101+
width = w
102+
}
103+
}
104+
97105
var out strings.Builder
98106
for _, info := range commands {
99107
out.WriteString(" ")
100-
out.WriteString(padRight(info.Invocation, 16))
108+
out.WriteString(padRight(info.Invocation, width))
101109
out.WriteByte(' ')
102110
out.WriteString(info.Summary)
103111
out.WriteByte('\n')
@@ -106,8 +114,9 @@ func renderExperimentalCommands(commands []experimentalCommandInfo) string {
106114
}
107115

108116
func padRight(value string, width int) string {
109-
if len(value) >= width {
117+
n := utf8.RuneCountInString(value)
118+
if n >= width {
110119
return value
111120
}
112-
return value + strings.Repeat(" ", width-len(value))
121+
return value + strings.Repeat(" ", width-n)
113122
}

0 commit comments

Comments
 (0)