-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin-tier-listing.go
42 lines (37 loc) · 1.03 KB
/
admin-tier-listing.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
package cmd
import "fmt"
type tabulator interface {
ToRow(i int, lengths []int) []string // returns row representation i-th element in collection, modifies lengths s.t it contains maximum column widths incl this row.
NumRows() int
NumCols() int
EmptyMessage() string
}
func toTable(tbl tabulator) string {
if tbl.NumRows() == 0 {
return tbl.EmptyMessage()
}
const tableSeparator = "|"
rows, cols := getRowsAndCols(tbl)
table := newPrettyTable(tableSeparator, cols...)
var contents string
for _, row := range rows {
contents += fmt.Sprintf("%s\n", table.buildRow(row...))
}
return contents
}
func getRowsAndCols(tbl tabulator) ([][]string, []Field) {
rows := make([][]string, 0, tbl.NumRows()+1)
lengths := make([]int, tbl.NumCols())
rows = append(rows, tbl.ToRow(-1, lengths))
for i := 0; i < tbl.NumRows(); i++ {
rows = append(rows, tbl.ToRow(i, lengths))
}
cols := make([]Field, tbl.NumCols())
for i, hdr := range rows[0] {
cols[i] = Field{
colorTheme: hdr,
maxLen: lengths[i] + 2,
}
}
return rows, cols
}