Repository navigation
Expand file tree
/
Copy pathtable.go
More file actions
145 lines (125 loc) · 4.36 KB
/
Copy pathtable.go
File metadata and controls
145 lines (125 loc) · 4.36 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
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
package render
import (
"bytes"
"fmt"
"io"
"github.com/olekukonko/tablewriter"
"github.com/olekukonko/tablewriter/renderer"
"github.com/olekukonko/tablewriter/tw"
)
// Table writes the rows as a borderless, space-aligned table for piping: a full border spends
// three columns per field, and the widest views carry many fields. An empty row set still prints
// the heading line, which separates a controller holding nothing from a read that failed.
func Table[T any](w io.Writer, cols []Column[T], rows []T) error {
return write(w, cols, rows, plainCell[T], borderless(),
tw.Padding{Left: "", Right: " ", Overwrite: true}, tw.AlignLeft, nil)
}
// PrettyTable writes the rows as a light-ruled, bordered table with a glyph in the columns that
// declare one. --pretty selects it, for reading on a terminal rather than for piping: the rules
// cost three columns per field, and some glyphs are two columns wide.
func PrettyTable[T any](w io.Writer, cols []Column[T], rows []T) error {
return write(w, cols, rows, prettyCell[T], bordered(), tw.PaddingDefault, tw.AlignCenter, glyphAligns(cols))
}
func plainCell[T any](c Column[T]) func(T) string { return c.Cell }
func prettyCell[T any](c Column[T]) func(T) string {
if c.Pretty != nil {
return c.Pretty
}
return c.Cell
}
// glyphAligns centers each column that declares a glyph, so a glyph one or two columns wide sits
// under its centered heading instead of against the left rule.
func glyphAligns[T any](cols []Column[T]) []tw.Align {
out := make([]tw.Align, len(cols))
for i, c := range cols {
out[i] = tw.AlignLeft
if c.Pretty != nil {
out[i] = tw.AlignCenter
}
}
return out
}
func write[T any](
w io.Writer, cols []Column[T], rows []T, cell func(Column[T]) func(T) string,
rendition tw.Rendition, padding tw.Padding, header tw.Align, rowAligns []tw.Align,
) error {
// The table is built into a buffer so each line can be stripped of the padding
// tablewriter leaves past the last column. Trailing blanks are invisible on a
// terminal and turn up in every checksum, diff and copied cell downstream.
var buf bytes.Buffer
t := tablewriter.NewTable(&buf,
tablewriter.WithRenderer(renderer.NewBlueprint(rendition)),
tablewriter.WithHeaderAlignment(header),
tablewriter.WithRowAlignmentConfig(tw.CellAlignment{Global: tw.AlignLeft, PerColumn: rowAligns}),
tablewriter.WithHeaderAutoFormat(tw.Off),
tablewriter.WithTrimSpace(tw.On),
tablewriter.WithPadding(padding),
)
t.Header(Headers(cols))
for _, row := range rows {
cells := make([]string, 0, len(cols))
for _, c := range cols {
cells = append(cells, cell(c)(row))
}
if err := t.Append(cells); err != nil {
return fmt.Errorf("appending a table row: %w", err)
}
}
if err := t.Render(); err != nil {
return fmt.Errorf("rendering the table: %w", err)
}
return writeTrimmed(w, buf.Bytes())
}
// writeTrimmed copies the rendered table out, one line at a time, without the
// trailing padding.
func writeTrimmed(w io.Writer, b []byte) error {
for line := range bytes.SplitSeq(bytes.TrimRight(b, "\n"), []byte("\n")) {
if _, err := w.Write(append(bytes.TrimRight(line, " \t"), '\n')); err != nil {
return fmt.Errorf("writing a table line: %w", err)
}
}
return nil
}
// bordered draws tw.StyleLight, so the box-drawing set is the library's and the column widths are
// measured by it rather than by counting runes.
func bordered() tw.Rendition {
return tw.Rendition{
Borders: tw.Border{Left: tw.On, Right: tw.On, Top: tw.On, Bottom: tw.On},
Symbols: tw.NewSymbols(tw.StyleLight),
Settings: tw.Settings{
Separators: tw.Separators{
ShowHeader: tw.On,
ShowFooter: tw.Off,
BetweenRows: tw.Off,
BetweenColumns: tw.On,
},
Lines: tw.Lines{
ShowTop: tw.On,
ShowBottom: tw.On,
ShowHeaderLine: tw.On,
ShowFooterLine: tw.Off,
},
},
}
}
// borderless turns off every rule and separator, leaving two spaces of padding as
// the only column gap.
func borderless() tw.Rendition {
return tw.Rendition{
Borders: tw.Border{Left: tw.Off, Right: tw.Off, Top: tw.Off, Bottom: tw.Off},
Settings: tw.Settings{
Separators: tw.Separators{
ShowHeader: tw.Off,
ShowFooter: tw.Off,
BetweenRows: tw.Off,
BetweenColumns: tw.Off,
},
Lines: tw.Lines{
ShowTop: tw.Off,
ShowBottom: tw.Off,
ShowHeaderLine: tw.Off,
ShowFooterLine: tw.Off,
},
},
}
}