TableFormat creates a table formatter for the given output type. The output type should be specified as an empty slice of the desired type. E.g.: TableFormat([]MyType{}, []string{"foo", "bar"}) defaultColumns is optional and specifies the default columns to display. If not specified, all columns a
(out any, defaultColumns []string)
| 110 | // If the data is empty, an empty string is returned. Callers should check for |
| 111 | // this and provide an appropriate message to the user. |
| 112 | func TableFormat(out any, defaultColumns []string) OutputFormat { |
| 113 | v := reflect.Indirect(reflect.ValueOf(out)) |
| 114 | if v.Kind() != reflect.Slice { |
| 115 | panic("DisplayTable called with a non-slice type") |
| 116 | } |
| 117 | |
| 118 | // Get the list of table column headers. |
| 119 | headers, defaultSort, err := typeToTableHeaders(v.Type().Elem(), true) |
| 120 | if err != nil { |
| 121 | panic("parse table headers: " + err.Error()) |
| 122 | } |
| 123 | |
| 124 | tf := &tableFormat{ |
| 125 | defaultColumns: headers, |
| 126 | allColumns: headers, |
| 127 | sort: defaultSort, |
| 128 | } |
| 129 | if len(defaultColumns) > 0 { |
| 130 | tf.defaultColumns = defaultColumns |
| 131 | } |
| 132 | |
| 133 | return tf |
| 134 | } |
| 135 | |
| 136 | // ID implements OutputFormat. |
| 137 | func (*tableFormat) ID() string { |
no test coverage detected