mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 19:31:44 +00:00
Merge pull request #103514 from soltysh/format_string
Hide long and multiline strings when printing
This commit is contained in:
commit
2423842549
@ -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)
|
||||
|
@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user