/
table.go
191 lines (171 loc) · 4.67 KB
/
table.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
189
190
191
// Package acidtab prints aligned tables.
package acidtab
import (
"fmt"
"strings"
"arp242.net/termtext"
)
type (
Close uint8 // Which sides of the table to "close".
Align uint8 // Alignment for columns.
PrintAs string // How to print a value.
PrintAsFunc func(v interface{}) string
// Borders to use.
Borders struct {
Line, Bar, Cross rune
TopLeft, TopRight, BottomLeft, BottomRight rune
BarRight, BarLeft, LineTop, LineBottom rune
}
)
// Which sides to close.
const (
CloseBottom Close = 1 << iota
CloseTop
CloseLeft
CloseRight
CloseAll Close = CloseBottom | CloseTop | CloseLeft | CloseRight
)
// Characters to use to draw the borders.
var (
BordersDefault = Borders{'─', '│', '┼', '┌', '┐', '└', '┘', '├', '┤', '┬', '┴'}
BordersHeavy = Borders{'━', '┃', '╋', '┏', '┓', '┗', '┛', '┣', '┫', '┳', '┻'}
BordersASCII = Borders{'-', '|', '+', '+', '+', '+', '+', '+', '+', '+', '+'}
BordersSpace = Borders{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}
)
// Column alignment.
const (
Auto Align = iota
Left
Right
Center
)
type Table struct {
header []string
rows [][]string
widths []int
close Close // Which sides to close?
borders Borders // Border characters to use.
pad string // Padding between columns, before and after.
prefix string // Print before every line.
pHeader bool // Print header?
printAs []PrintAs // Printf format verb; defaults to %v
printAsF []PrintAsFunc
align []Align
}
// New creates a new table with the given headers.
func New(header ...string) *Table {
t := &Table{
header: header,
widths: make([]int, len(header)),
printAs: make([]PrintAs, len(header)),
printAsF: make([]PrintAsFunc, len(header)),
align: make([]Align, len(header)),
pad: " ",
borders: BordersDefault,
pHeader: true,
}
for i := range header {
t.printAs[i] = "%v"
t.widths[i] = termtext.Width(header[i])
}
return t
}
func (t Table) String() string {
b := new(strings.Builder)
t.Horizontal(b)
return b.String()
}
func (t *Table) Close(close Close) *Table { t.close = close; return t }
func (t *Table) Pad(pad string) *Table { t.pad = pad; return t }
func (t *Table) Prefix(prefix string) *Table { t.prefix = prefix; return t }
func (t *Table) Borders(borders Borders) *Table { t.borders = borders; return t }
func (t *Table) Header(on bool) *Table { t.pHeader = on; return t }
func (t *Table) AlignCol(n int, a Align) *Table { t.align[n] = a; return t }
func (t *Table) PrintCol(n int, p PrintAs) *Table { t.printAs[n] = p; return t }
func (t *Table) PrintFuncCol(n int, p PrintAsFunc) *Table { t.printAsF[n] = p; return t }
// Width gets the display width of the table, including any padding characters.
//
// The width may grow if more rows are added.
func (t *Table) Width() int {
p := termtext.Width(t.prefix) + termtext.Width(t.pad)*2 + 1 // 1 for the bar character
var w int
for _, c := range t.widths {
w += c + p
}
if t.close&CloseLeft != 0 {
w++
}
if t.close&CloseRight == 0 {
w--
}
return w
}
// Grow the rows allocation.
func (t *Table) Grow(n int) {
if len(t.rows) == 0 {
t.rows = make([][]string, 0, n)
return
}
r := make([][]string, len(t.rows), len(t.rows)+n)
copy(r, t.rows)
t.rows = r
}
// Rows adds multiple rows; the number of values should be an exact multitude of
// the number of headers.
//
// For example:
//
// t.Rows(
// "row1", "row1",
// "row2", "row2",)
func (t *Table) Rows(r ...interface{}) *Table {
l := len(t.header)
if len(r)%l != 0 {
panic("wrong number")
}
t.Grow(len(r) / l)
for ; len(r) > 0; r = r[l:] {
t.Row(r[:l]...)
}
return t
}
// Row adds a new row.
//
// The number of values can be lower than the number of headers; the remaining
// cells will be filled with spaces.
//
// If the number of values is greater it will panic.
func (t *Table) Row(r ...interface{}) *Table {
if len(r) > len(t.header) {
panic(fmt.Sprintf("table.Row: too many values (%d); there are only %d headers", len(r), len(t.header)))
}
if len(t.rows) == 0 {
for i := range r {
if t.align[i] != Auto {
continue
}
if _, ok := r[i].(int); ok {
t.align[i] = Right
} else {
t.align[i] = Left
}
}
}
row := make([]string, len(r))
for i := range r {
f := t.printAsF[i]
if f != nil {
row[i] = f(r[i])
if row[i] == "\x00" {
row[i] = fmt.Sprintf(string(t.printAs[i]), r[i])
}
} else {
row[i] = fmt.Sprintf(string(t.printAs[i]), r[i])
}
if l := termtext.Width(row[i]); l > t.widths[i] {
t.widths[i] = l
}
}
t.rows = append(t.rows, row)
return t
}