@@ -31,10 +31,33 @@ func Table() table.Writer {
31
31
// e.g. `[]any{someRow, TableSeparator, someRow}`
32
32
type TableSeparator struct {}
33
33
34
- // filterTableColumns returns configurations to hide columns
34
+ // filterHeaders filters the headers to only include the columns
35
+ // that are provided in the array. If the array is empty, all
36
+ // headers are included.
37
+ func filterHeaders (header table.Row , columns []string ) table.Row {
38
+ if len (columns ) == 0 {
39
+ return header
40
+ }
41
+
42
+ filteredHeaders := make (table.Row , len (columns ))
43
+ for i , column := range columns {
44
+ column = strings .ReplaceAll (column , "_" , " " )
45
+
46
+ for _ , headerTextRaw := range header {
47
+ headerText , _ := headerTextRaw .(string )
48
+ if strings .EqualFold (column , headerText ) {
49
+ filteredHeaders [i ] = headerText
50
+ break
51
+ }
52
+ }
53
+ }
54
+ return filteredHeaders
55
+ }
56
+
57
+ // createColumnConfigs returns configuration to hide columns
35
58
// that are not provided in the array. If the array is empty,
36
59
// no filtering will occur!
37
- func filterTableColumns (header table.Row , columns []string ) []table.ColumnConfig {
60
+ func createColumnConfigs (header table.Row , columns []string ) []table.ColumnConfig {
38
61
if len (columns ) == 0 {
39
62
return nil
40
63
}
@@ -157,10 +180,13 @@ func DisplayTable(out any, sort string, filterColumns []string) (string, error)
157
180
func renderTable (out any , sort string , headers table.Row , filterColumns []string ) (string , error ) {
158
181
v := reflect .Indirect (reflect .ValueOf (out ))
159
182
183
+ headers = filterHeaders (headers , filterColumns )
184
+ columnConfigs := createColumnConfigs (headers , filterColumns )
185
+
160
186
// Setup the table formatter.
161
187
tw := Table ()
162
188
tw .AppendHeader (headers )
163
- tw .SetColumnConfigs (filterTableColumns ( headers , filterColumns ) )
189
+ tw .SetColumnConfigs (columnConfigs )
164
190
if sort != "" {
165
191
tw .SortBy ([]table.SortBy {{
166
192
Name : sort ,
0 commit comments