Repository navigation
Expand file tree
/
Copy pathcolumn.go
More file actions
91 lines (74 loc) · 2.87 KB
/
Copy pathcolumn.go
File metadata and controls
91 lines (74 loc) · 2.87 KB
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
// Package render turns a slice of rows into a borderless table, a bordered one, or a flat
// JSON array. All three are built from the same column list, so a column cannot exist in
// one and be missing from the others.
package render
// Absent is the table cell shown where the controller reported nothing. The
// JSON form omits the key instead of writing this, so a consumer parsing the JSON
// never has to recognize a placeholder.
const Absent = "-"
// Column is one output column: its JSON field name, its table heading and the cell
// it derives from a row.
type Column[T any] struct {
// Key is the JSON field name and the value --sort-by and --columns accept. An invariant
// test asserts it matches the row struct's json tag for the same position.
Key string
// Header is the table heading.
Header string
// Cell renders the table cell. Sort falls back to it, so a column with no Sort function is
// ordered by this text and not by Pretty.
Cell func(T) string
// Pretty renders the cell for the bordered table only, and is nil on every column
// whose two renderings are the same. It never feeds Sort or the JSON, so a glyph
// here cannot reorder rows or reach a consumer parsing the output.
Pretty func(T) string
// Sort yields the value to order by, and is nil where the rendered text already orders
// correctly: an octet count printed as "1.0KiB" and an age printed as "3d4h" do not.
Sort func(T) any
// Hidden keeps an identifier, or a column few reads need, out of the default set. --columns
// still names it, and --sort-by orders by it either way.
Hidden bool
}
func Keys[T any](cols []Column[T]) []string {
out := make([]string, 0, len(cols))
for _, c := range cols {
out = append(out, c.Key)
}
return out
}
// DefaultKeys are the keys a view prints when --columns is absent.
func DefaultKeys[T any](cols []Column[T]) []string {
out := make([]string, 0, len(cols))
for _, c := range cols {
if !c.Hidden {
out = append(out, c.Key)
}
}
return out
}
// Select returns the columns the keys name, in the order given. config.Resolve checked every key
// against the view's key list, which an invariant test pins to these columns, so none is missing.
func Select[T any](cols []Column[T], keys []string) []Column[T] {
out := make([]Column[T], 0, len(keys))
for _, k := range keys {
c, _ := find(cols, k)
out = append(out, c)
}
return out
}
// SortValue converts an optional number into the float64 a comparator takes, or nil for an
// unreported cell. A column's Sort yields one of nil, a string, a float64 or a bool.
func SortValue[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float64](
p *T,
) any {
if p == nil {
return nil
}
return float64(*p)
}
func Headers[T any](cols []Column[T]) []string {
out := make([]string, 0, len(cols))
for _, c := range cols {
out = append(out, c.Header)
}
return out
}