Restore ability to print long strings

This commit is contained in:
Jordan Liggitt 2021-07-08 01:53:01 -04:00
parent 818ed1afff
commit 92541f46e6
2 changed files with 8 additions and 15 deletions

View File

@ -32,8 +32,6 @@ import (
"k8s.io/apimachinery/pkg/watch" "k8s.io/apimachinery/pkg/watch"
) )
const maxStringLength = 100
var _ ResourcePrinter = &HumanReadablePrinter{} var _ ResourcePrinter = &HumanReadablePrinter{}
type printHandler struct { type printHandler struct {
@ -213,21 +211,16 @@ func printTable(table *metav1.Table, output io.Writer, options PrintOptions) err
switch val := cell.(type) { switch val := cell.(type) {
case string: case string:
print := val print := val
more := 0 truncated := false
// cut to maxStringLength // truncate at newlines
if len(val) > maxStringLength {
more = len(print) - maxStringLength
print = print[:maxStringLength]
}
// and also check for newlines
newline := strings.Index(print, "\n") newline := strings.Index(print, "\n")
if newline >= 0 { if newline >= 0 {
more = more + len(print) - newline truncated = true
print = print[:newline] print = print[:newline]
} }
fmt.Fprint(output, print) fmt.Fprint(output, print)
if more > 0 { if truncated {
fmt.Fprintf(output, " + %d more...", more) fmt.Fprint(output, "...")
} }
default: default:
fmt.Fprint(output, val) fmt.Fprint(output, val)

View File

@ -740,7 +740,7 @@ func TestStringPrinting(t *testing.T) {
{Cells: []interface{}{"test1", "20h", "This is first line\nThis is second line\nThis is third line\nand another one\n"}}, {Cells: []interface{}{"test1", "20h", "This is first line\nThis is second line\nThis is third line\nand another one\n"}},
}, },
expected: `NAME AGE DESCRIPTION expected: `NAME AGE DESCRIPTION
test1 20h This is first line + 56 more... test1 20h This is first line...
`, `,
}, },
// lengthy string // lengthy string
@ -754,7 +754,7 @@ test1 20h This is first line + 56 more...
{Cells: []interface{}{"test1", "20h", "This is first line which is long and goes for on and on and on an on and on and on and on and on and on and on and on and on and on and on"}}, {Cells: []interface{}{"test1", "20h", "This is first line which is long and goes for on and on and on an on and on and on and on and on and on and on and on and on and on and on"}},
}, },
expected: `NAME AGE DESCRIPTION expected: `NAME AGE DESCRIPTION
test1 20h This is first line which is long and goes for on and on and on an on and on and on and on and on and + 38 more... test1 20h This is first line which is long and goes for on and on and on an on and on and on and on and on and on and on and on and on and on and on
`, `,
}, },
// lengthy string + newline // lengthy string + newline
@ -768,7 +768,7 @@ test1 20h This is first line which is long and goes for on and on and on an
{Cells: []interface{}{"test1", "20h", "This is first\n line which is long and goes for on and on and on an on and on and on and on and on and on and on and on and on and on and on"}}, {Cells: []interface{}{"test1", "20h", "This is first\n line which is long and goes for on and on and on an on and on and on and on and on and on and on and on and on and on and on"}},
}, },
expected: `NAME AGE DESCRIPTION expected: `NAME AGE DESCRIPTION
test1 20h This is first + 126 more... test1 20h This is first...
`, `,
}, },
} }