Skip to content

Commit

Permalink
Restore ability to print long strings
Browse files Browse the repository at this point in the history
Kubernetes-commit: 92541f46e6b38264927b17d0becb07fa11902b17
  • Loading branch information
liggitt authored and k8s-publishing-bot committed Jul 8, 2021
1 parent f7c3433 commit 792f804
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
17 changes: 5 additions & 12 deletions pkg/printers/tableprinter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import (
"k8s.io/apimachinery/pkg/watch"
)

const maxStringLength = 100

var _ ResourcePrinter = &HumanReadablePrinter{}

type printHandler struct {
Expand Down Expand Up @@ -213,21 +211,16 @@ func printTable(table *metav1.Table, output io.Writer, options PrintOptions) err
switch val := cell.(type) {
case string:
print := val
more := 0
// cut to maxStringLength
if len(val) > maxStringLength {
more = len(print) - maxStringLength
print = print[:maxStringLength]
}
// and also check for newlines
truncated := false
// truncate at newlines
newline := strings.Index(print, "\n")
if newline >= 0 {
more = more + len(print) - newline
truncated = true
print = print[:newline]
}
fmt.Fprint(output, print)
if more > 0 {
fmt.Fprintf(output, " + %d more...", more)
if truncated {
fmt.Fprint(output, "...")
}
default:
fmt.Fprint(output, val)
Expand Down
6 changes: 3 additions & 3 deletions pkg/printers/tableprinter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
},
expected: `NAME AGE DESCRIPTION
test1 20h This is first line + 56 more...
test1 20h This is first line...
`,
},
// lengthy string
Expand All @@ -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"}},
},
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
Expand All @@ -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"}},
},
expected: `NAME AGE DESCRIPTION
test1 20h This is first + 126 more...
test1 20h This is first...
`,
},
}
Expand Down

0 comments on commit 792f804

Please sign in to comment.