-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathformat.go
188 lines (154 loc) · 3.76 KB
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tables
import (
"bytes"
"fmt"
"io"
"reflect"
"sort"
"text/tabwriter"
"k8s.io/klog/v2"
"k8s.io/kops/util/pkg/reflectutils"
)
// Table renders tables to stdout
type Table struct {
columns map[string]*TableColumn
}
type TableColumn struct {
Name string
Getter reflect.Value
}
func (c *TableColumn) getFromValue(v reflect.Value) string {
var args []reflect.Value
args = append(args, v)
fvs := c.Getter.Call(args)
fv := fvs[0]
return reflectutils.ValueAsString(fv)
}
// AddColumn registers an available column for formatting
func (t *Table) AddColumn(name string, getter interface{}) {
getterVal := reflect.ValueOf(getter)
column := &TableColumn{
Name: name,
Getter: getterVal,
}
if t.columns == nil {
t.columns = make(map[string]*TableColumn)
}
t.columns[name] = column
}
type funcSorter struct {
len int
less func(int, int) bool
swap func(int, int)
}
func (f *funcSorter) Len() int {
return f.len
}
func (f *funcSorter) Less(i, j int) bool {
return f.less(i, j)
}
func (f *funcSorter) Swap(i, j int) {
f.swap(i, j)
}
func SortByFunction(len int, swap func(int, int), less func(int, int) bool) {
sort.Sort(&funcSorter{len, less, swap})
}
func (t *Table) findColumns(columnNames ...string) ([]*TableColumn, error) {
columns := make([]*TableColumn, len(columnNames))
for i, columnName := range columnNames {
c := t.columns[columnName]
if c == nil {
return nil, fmt.Errorf("column not found: %v", columnName)
}
columns[i] = c
}
return columns, nil
}
// Render writes the items in a table, to out
func (t *Table) Render(items interface{}, out io.Writer, columnNames ...string) error {
itemsValue := reflect.ValueOf(items)
if itemsValue.Kind() != reflect.Slice {
klog.Fatal("unexpected kind for items: ", itemsValue.Kind())
}
columns, err := t.findColumns(columnNames...)
if err != nil {
return err
}
n := itemsValue.Len()
rows := make([][]string, n)
for i := 0; i < n; i++ {
row := make([]string, len(columns))
item := itemsValue.Index(i)
for j, column := range columns {
row[j] = column.getFromValue(item)
}
rows[i] = row
}
SortByFunction(n, func(i, j int) {
row := rows[i]
rows[i] = rows[j]
rows[j] = row
}, func(i, j int) bool {
l := rows[i]
r := rows[j]
for k := 0; k < len(columns); k++ {
lV := l[k]
rV := r[k]
if lV != rV {
return lV < rV
}
}
return false
})
var b bytes.Buffer
w := new(tabwriter.Writer)
// Format in tab-separated columns with a tab stop of 8.
w.Init(out, 0, 8, 1, '\t', tabwriter.StripEscape)
writeHeader := true
if writeHeader {
for i, c := range columns {
if i != 0 {
b.WriteByte('\t')
}
b.WriteByte(tabwriter.Escape)
b.WriteString(c.Name)
b.WriteByte(tabwriter.Escape)
}
b.WriteByte('\n')
_, err := w.Write(b.Bytes())
if err != nil {
return fmt.Errorf("error writing to output: %v", err)
}
b.Reset()
}
for _, row := range rows {
for i, col := range row {
if i != 0 {
b.WriteByte('\t')
}
b.WriteByte(tabwriter.Escape)
b.WriteString(col)
b.WriteByte(tabwriter.Escape)
}
b.WriteByte('\n')
_, err := w.Write(b.Bytes())
if err != nil {
return fmt.Errorf("error writing to output: %v", err)
}
b.Reset()
}
w.Flush()
return nil
}