From d0c422fd9c8d932d6161f326c3168b221db540d0 Mon Sep 17 00:00:00 2001 From: Maciej Szulik Date: Tue, 6 Jul 2021 14:04:46 +0200 Subject: [PATCH] Hide long and multiline strings when printing Currently both long strings and multiline strings can potentially "break" printing. I'm adding extra formatting to ensure we cut strings either at newline or at 100 chars with information that more information is available. --- .../cli-runtime/pkg/printers/tableprinter.go | 25 ++++++- .../pkg/printers/tableprinter_test.go | 67 +++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/staging/src/k8s.io/cli-runtime/pkg/printers/tableprinter.go b/staging/src/k8s.io/cli-runtime/pkg/printers/tableprinter.go index 048cb66a2ba..b8211e529c5 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/printers/tableprinter.go +++ b/staging/src/k8s.io/cli-runtime/pkg/printers/tableprinter.go @@ -32,6 +32,8 @@ import ( "k8s.io/apimachinery/pkg/watch" ) +const maxStringLength = 100 + var _ ResourcePrinter = &HumanReadablePrinter{} type printHandler struct { @@ -208,7 +210,28 @@ func printTable(table *metav1.Table, output io.Writer, options PrintOptions) err fmt.Fprint(output, "\t") } if cell != nil { - fmt.Fprint(output, cell) + 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 + newline := strings.Index(print, "\n") + if newline >= 0 { + more = more + len(print) - newline + print = print[:newline] + } + fmt.Fprint(output, print) + if more > 0 { + fmt.Fprintf(output, " + %d more...", more) + } + default: + fmt.Fprint(output, val) + } } } fmt.Fprintln(output) diff --git a/staging/src/k8s.io/cli-runtime/pkg/printers/tableprinter_test.go b/staging/src/k8s.io/cli-runtime/pkg/printers/tableprinter_test.go index 2bdd62636bc..770067cf50d 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/printers/tableprinter_test.go +++ b/staging/src/k8s.io/cli-runtime/pkg/printers/tableprinter_test.go @@ -722,3 +722,70 @@ func TestUnknownTypePrinting(t *testing.T) { t.Errorf("An error was expected from printing unknown type") } } + +func TestStringPrinting(t *testing.T) { + tests := []struct { + columns []metav1.TableColumnDefinition + rows []metav1.TableRow + expected string + }{ + // multiline string + { + columns: []metav1.TableColumnDefinition{ + {Name: "Name", Type: "string"}, + {Name: "Age", Type: "string"}, + {Name: "Description", Type: "string"}, + }, + rows: []metav1.TableRow{ + {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... +`, + }, + // lengthy string + { + columns: []metav1.TableColumnDefinition{ + {Name: "Name", Type: "string"}, + {Name: "Age", Type: "string"}, + {Name: "Description", Type: "string"}, + }, + rows: []metav1.TableRow{ + {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... +`, + }, + // lengthy string + newline + { + columns: []metav1.TableColumnDefinition{ + {Name: "Name", Type: "string"}, + {Name: "Age", Type: "string"}, + {Name: "Description", Type: "string"}, + }, + rows: []metav1.TableRow{ + {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... +`, + }, + } + + for _, test := range tests { + // Create the table from the columns and rows. + table := &metav1.Table{ + ColumnDefinitions: test.columns, + Rows: test.rows, + } + // Print the table + out := bytes.NewBuffer([]byte{}) + printer := NewTablePrinter(PrintOptions{}) + printer.PrintObj(table, out) + + if test.expected != out.String() { + t.Errorf("Table printing error: expected \n(%s), got \n(%s)", test.expected, out.String()) + } + } +}