-
Notifications
You must be signed in to change notification settings - Fork 15
/
table.go
207 lines (180 loc) · 5.11 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package clif
import (
"fmt"
)
type (
Table struct {
// AllowEmptyFill decides whether SetRow() and SetColumn() with row indices
// bigger than the amount of rows creates additional, empty rows in between.
// USE WITH CAUTION!
AllowEmptyFill bool
// set of output headers
Headers *TableRow
// colAmount is the amount of cols per row (fixed size)
colAmount int
// rowAmount is the amount of rows
rowAmount int
// set of row, col, lines
Rows []*TableRow
// Style for rendering table
style *TableStyle
}
TableStyle struct {
LeftTop string
Left string
LeftBottom string
Right string
RightTop string
RightBottom string
CrossInner string
CrossTop string
CrossBottom string
CrossLeft string
CrossRight string
InnerHorizontal string
InnerVertical string
Top string
Bottom string
Prefix string
Suffix string
HeaderRenderer func(content string) string
ContentRenderer func(content string) string
}
TableRow struct {
// MaxLineCount is the maximum amount of
MaxLineCount int
ColAmount int
Cols []*TableCol
table *Table
}
TableCol struct {
// Content is to the column content
content *string
// LineCount contains the amount of lines in the content
lineCount int
// renderer is the content renderer.. see `TableStyle.(Content|Header)Renderer`
renderer func(content string) string
// row is back-reference to row
row *TableRow
}
)
var (
ErrHeadersNotSetYet = fmt.Errorf("Cannot add/set data when headers are not set")
)
// NewTable constructs new Table with optional list of headers
func NewTable(headers []string, style ...*TableStyle) *Table {
if len(style) == 0 {
style = []*TableStyle{NewDefaultTableStyle()}
}
this := &Table{
Rows: make([]*TableRow, 0),
style: style[0],
}
if headers != nil {
this.SetHeaders(headers)
}
return this
}
// Render prints the table into a string
func (this *Table) Render(maxWidth ...int) string {
return this.style.Render(this, maxWidth...)
}
// Reset clears all (row) data of the table
func (this *Table) Reset() {
this.Rows = make([]*TableRow, 0)
}
// SetHeaders sets the headers of the table. Can only be called before any
// data has been added.
func (this *Table) SetHeaders(headers []string) error {
if this.Headers != nil && this.rowAmount > 0 {
return fmt.Errorf("Cannot set headers after data has been added")
}
this.colAmount = len(headers)
this.Headers = NewTableRow(headers)
return nil
}
// SetStyle changes the table style
func (this *Table) SetStyle(style *TableStyle) {
this.style = style
}
// AddRow adds another row to the table. Headers must be set beforehand.
func (this *Table) AddRow(cols []string) error {
if err := this.checkAddCols(cols); err != nil {
return err
}
this.addRow(cols)
return nil
}
// AddRows adds multiple row to the table. Headers must be set beforehand.
func (this *Table) AddRows(rows [][]string) error {
for idx, cols:= range rows {
if err := this.checkAddCols(cols); err != nil {
return fmt.Errorf("Row %d: %s", idx+1, err)
}
}
for _, cols:= range rows {
this.addRow(cols)
}
return nil
}
// SetRow sets columns in a specific row.
//
// If `AllowEmptyFill` is true, then the row index can be arbitrary and empty
// columns will be automatically created, if needed.
// Otherwise the row index must be within the bounds of existing data or an
// error is returned.
func (this *Table) SetRow(idx int, cols []string) error {
if err := this.checkAddCols(cols); err != nil {
return err
}
if idx < this.colAmount {
row := NewTableRow(cols).SetTable(this)
this.Rows[idx] = row
} else if idx > this.colAmount {
if this.AllowEmptyFill {
empty := make([]string, this.colAmount)
diff := int(this.colAmount - idx)
for i := 0; i < diff; i++ {
this.addRow(empty)
}
this.addRow(cols)
} else {
return fmt.Errorf("Cannot set row at index %d -> Only %d rows in data", idx, this.rowAmount)
}
} else { // == this.cols
this.addRow(cols)
}
return nil
}
// SetColumn sets the contents of a specific column in a specific row. See `SetRow`
// for limitations on the row index.
// The column index must be within the bounds of the column amount.
func (this *Table) SetColumn(rowIdx, colIdx int, content string) error {
if this.Headers == nil {
return ErrHeadersNotSetYet
} else if colIdx >= this.colAmount {
return fmt.Errorf("Cannot set row at index %d -> Only %d rows in data", rowIdx, this.rowAmount)
}
if rowIdx < this.colAmount {
this.Rows[rowIdx].Cols[colIdx] = NewTableCol(this.Rows[rowIdx], content)
} else {
cols := make([]string, this.colAmount)
cols[colIdx] = content
return this.SetRow(rowIdx, cols)
}
return nil
}
func (this *Table) addRow(cols []string) {
row := NewTableRow(cols).SetTable(this)
this.Rows = append(this.Rows, row)
this.rowAmount++
}
func (this *Table) checkAddCols(cols []string) error {
if this.Headers == nil {
return ErrHeadersNotSetYet
}
if l := len(cols); l != this.colAmount {
return fmt.Errorf("Cannot add %d cols. Expected width is %d", l, this.colAmount)
}
return nil
}