Break annotations with newlines and shorten length

Make annotations with newlines display a more consistent left edge, and indent the value
when the annotation is too long to give the value more space. Shorten the width of the
trimmed annotation to a value more consistent with our `-o wide` value.

Instead of putting the key and value flush with a `=` separator, make annotations closer
to fields than to labels by using `: ` as a separator.
This commit is contained in:
Clayton Coleman 2018-07-31 19:05:09 -04:00
parent b8809dcd67
commit a23465426d
No known key found for this signature in database
GPG Key ID: 3D16906B4F1C5CB3
2 changed files with 34 additions and 16 deletions

View File

@ -4078,7 +4078,7 @@ func (list SortableVolumeDevices) Less(i, j int) bool {
return list[i].DevicePath < list[j].DevicePath
}
var maxAnnotationLen = 200
var maxAnnotationLen = 140
// printAnnotationsMultilineWithFilter prints filtered multiple annotations with a proper alignment.
func printAnnotationsMultilineWithFilter(w PrefixWriter, title string, annotations map[string]string, skip sets.String) {
@ -4114,18 +4114,27 @@ func printAnnotationsMultilineWithIndent(w PrefixWriter, initialIndent, title, i
return
}
sort.Strings(keys)
indent := initialIndent + innerIndent
for i, key := range keys {
if i != 0 {
w.Write(LEVEL_0, initialIndent)
w.Write(LEVEL_0, innerIndent)
w.Write(LEVEL_0, indent)
}
line := fmt.Sprintf("%s=%s", key, annotations[key])
if len(line) > maxAnnotationLen {
w.Write(LEVEL_0, "%s...\n", line[:maxAnnotationLen])
value := strings.TrimSuffix(annotations[key], "\n")
if (len(value)+len(key)+2) > maxAnnotationLen || strings.Contains(value, "\n") {
w.Write(LEVEL_0, "%s:\n", key)
for _, s := range strings.Split(value, "\n") {
w.Write(LEVEL_0, "%s %s\n", indent, shorten(s, maxAnnotationLen-2))
}
} else {
w.Write(LEVEL_0, "%s\n", line)
w.Write(LEVEL_0, "%s: %s\n", key, value)
}
i++
}
}
func shorten(s string, maxLength int) string {
if len(s) > maxLength {
return s[:maxLength] + "..."
}
return s
}

View File

@ -2144,22 +2144,31 @@ func TestDescribeEvents(t *testing.T) {
}
func TestPrintLabelsMultiline(t *testing.T) {
var maxLenAnnotationStr string = "MaxLenAnnotation=Multicast addressing can be used in the link layer (Layer 2 in the OSI model), such as Ethernet multicast, and at the internet layer (Layer 3 for OSI) for Internet Protocol Version 4 "
key := "MaxLenAnnotation"
value := strings.Repeat("a", maxAnnotationLen-len(key)-2)
testCases := []struct {
annotations map[string]string
expectPrint string
}{
{
annotations: map[string]string{"col1": "asd", "COL2": "zxc"},
expectPrint: "Annotations:\tCOL2=zxc\n\tcol1=asd\n",
expectPrint: "Annotations:\tCOL2: zxc\n\tcol1: asd\n",
},
{
annotations: map[string]string{"MaxLenAnnotation": maxLenAnnotationStr[17:]},
expectPrint: "Annotations:\t" + maxLenAnnotationStr + "\n",
annotations: map[string]string{"MaxLenAnnotation": value},
expectPrint: fmt.Sprintf("Annotations:\t%s: %s\n", key, value),
},
{
annotations: map[string]string{"MaxLenAnnotation": maxLenAnnotationStr[17:] + "1"},
expectPrint: "Annotations:\t" + maxLenAnnotationStr + "...\n",
annotations: map[string]string{"MaxLenAnnotation": value + "1"},
expectPrint: fmt.Sprintf("Annotations:\t%s:\n\t %s\n", key, value+"1"),
},
{
annotations: map[string]string{"MaxLenAnnotation": value + value},
expectPrint: fmt.Sprintf("Annotations:\t%s:\n\t %s\n", key, strings.Repeat("a", maxAnnotationLen-2)+"..."),
},
{
annotations: map[string]string{"key": "value\nwith\nnewlines\n"},
expectPrint: "Annotations:\tkey:\n\t value\n\t with\n\t newlines\n",
},
{
annotations: map[string]string{},
@ -2167,13 +2176,13 @@ func TestPrintLabelsMultiline(t *testing.T) {
},
}
for i, testCase := range testCases {
t.Run(testCase.expectPrint, func(t *testing.T) {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
out := new(bytes.Buffer)
writer := NewPrefixWriter(out)
printAnnotationsMultiline(writer, "Annotations", testCase.annotations)
output := out.String()
if output != testCase.expectPrint {
t.Errorf("Test case %d: expected to find %q in output: %q", i, testCase.expectPrint, output)
t.Errorf("Test case %d: expected to match:\n%q\nin output:\n%q", i, testCase.expectPrint, output)
}
})
}