Skip to content

Commit ab98c9b

Browse files
authored
Merge pull request #11508 from chrischdi/pr-clusterctl-multiline-messages-indent
🌱 clusterctl: properly indent multiline lists in clusterctl describe
2 parents 5970cbc + 2315d6f commit ab98c9b

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

cmd/clusterctl/cmd/describe_cluster.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,10 @@ func formatParagraph(text string, maxWidth int) string {
780780
}
781781
break
782782
}
783+
indent := tmp
784+
if strings.HasPrefix(strings.TrimSpace(l), "* ") {
785+
indent += " "
786+
}
783787
for _, w := range re.Split(l, -1) {
784788
if len(tmp)+len(w) < maxWidth {
785789
if strings.TrimSpace(tmp) != "" {
@@ -789,7 +793,7 @@ func formatParagraph(text string, maxWidth int) string {
789793
continue
790794
}
791795
lines = append(lines, tmp)
792-
tmp = w
796+
tmp = indent + w
793797
}
794798
lines = append(lines, tmp)
795799
}

cmd/clusterctl/cmd/describe_cluster_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,41 @@ func (t *Table) NegatedFailureMessage(actual interface{}) string {
365365
actualTable := strings.Split(actual.(string), "\n")
366366
return fmt.Sprintf("Expected %v and received %v", t.tableData, actualTable)
367367
}
368+
369+
func Test_formatParagraph(t *testing.T) {
370+
tests := []struct {
371+
text string
372+
maxWidth int
373+
want string
374+
}{
375+
{
376+
text: "",
377+
maxWidth: 254,
378+
want: "",
379+
},
380+
{
381+
text: "* a b c d e f",
382+
maxWidth: 5,
383+
want: "* a b\n c d\n e f",
384+
},
385+
{
386+
text: "* a b c d e f\n" +
387+
" * g h\n" +
388+
" * i j",
389+
maxWidth: 5,
390+
want: "* a b\n" +
391+
" c d\n" +
392+
" e f\n" +
393+
" * g\n" +
394+
" h\n" +
395+
" * i\n" +
396+
" j",
397+
},
398+
}
399+
for ti, tt := range tests {
400+
t.Run(fmt.Sprintf("%d", ti), func(t *testing.T) {
401+
g := NewWithT(t)
402+
g.Expect("\n" + formatParagraph(tt.text, tt.maxWidth)).To(BeEquivalentTo("\n" + tt.want))
403+
})
404+
}
405+
}

0 commit comments

Comments
 (0)