-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.go
304 lines (249 loc) · 5.41 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package civ
import (
"fmt"
"strconv"
)
type Table struct {
header Row
// Maximum length of each columns data including both header
// and contents
maxLen []int
// A list of column number that is disabled to display
disabledCols []int
// Table contents excluding header
contents []Row
// View setting, starting from 0
offsetCol int
offsetRow int
outputStdout bool
}
type Row struct {
cols []Cell
hasMatched bool
isVisible bool
}
type Cell struct {
data string
matchBegin int
matchEnd int
width int // formatted size, available on only header
}
func NewTable(indata [][]string, dummyHeader bool) *Table {
t := &Table{
offsetCol: 0,
offsetRow: 0,
}
nCols := len(indata[0])
// Load header data
t.maxLen = make([]int, nCols)
for i, cell := range indata[0] {
c := Cell{
matchBegin: -1,
matchEnd: -1,
}
if dummyHeader {
// make dummy header
c.data = "col_" + strconv.Itoa(i)
t.header.cols = append(t.header.cols, c)
} else {
c.data = cell
t.header.cols = append(t.header.cols, c)
}
// initialize the maximum lenght of column by
// the header data
t.maxLen[i] = len(cell)
}
// Load table data
csvdata := indata[1:]
if dummyHeader {
// If using dummy header, csv data starts from indata[0]
csvdata = indata
}
for _, row := range csvdata {
var r Row
r.isVisible = true
for i, cell := range row {
c := &Cell{
data: cell,
matchBegin: -1,
matchEnd: -1,
}
r.cols = append(r.cols, *c)
if t.maxLen[i] < len(cell) {
t.maxLen[i] = len(cell)
}
}
t.contents = append(t.contents, r)
}
return t
}
// Return true and position if the given name is in col names
func (t *Table) FindColName(name string) (int, bool) {
for i, c := range t.header.cols {
if c.data == name {
return i, true
}
}
return -1, false
}
func (t *Table) NEnabledCols() int {
return len(t.header.cols) - len(t.disabledCols)
}
func (t *Table) IsColEnabled(colNum int) bool {
for _, n := range t.disabledCols {
if n == colNum {
return false
}
}
return true
}
// Add the idx'th column to disabled column list.
// That is, make it invisible
func (t *Table) AddDisabledCol(idx int) {
t.disabledCols = append(t.disabledCols, idx)
}
// Remove the idx'th column from disabled column list.
// That is, make it visible
func (t *Table) RemoveDisabledCol(idx int) {
res := []int{}
for _, c := range t.disabledCols {
if c != idx {
res = append(res, c)
}
}
t.disabledCols = res
}
func (t *Table) SetRowVisibility(rowIdx int, visible bool) {
t.contents[rowIdx].isVisible = visible
}
func (t *Table) SetMatched(r int, c int, b int, e int) {
t.contents[r].cols[c].matchBegin = b
t.contents[r].cols[c].matchEnd = e
}
func (t *Table) ResetDisabledCol() {
t.disabledCols = []int{}
}
func (t *Table) ResetVisibility() {
for i := 0; i < len(t.contents); i++ {
t.contents[i].isVisible = true
}
}
// Return the total size of visible cols. Note that c.width is the size
// of formatted cell, i.e. includes padding.
func (t *Table) computeWidth() (width int) {
width = 0
for i, c := range t.header.cols {
// Skip until offset
if i < t.offsetCol {
continue
}
// Skip if this col is disabled
if !t.IsColEnabled(i) {
continue
}
width += c.width
}
return width
}
// Return the total size of visible rows.
func (t *Table) computeHeight() (height int) {
height = 0
for i, r := range t.contents {
// Skip until offset
if i < t.offsetRow {
continue
}
if !r.isVisible {
continue
}
height++
}
// always include both header and line
return height + 2
}
// 0: up, 1: right, 2:down, 3: left
func (t *Table) isMovable(direction int) bool {
maxX, maxY := GetMaxXY()
x := t.computeWidth()
y := t.computeHeight()
if x >= maxX && direction == 1 {
// Movable to right and want to move right
return true
} else if t.offsetCol > 0 && direction == 3 {
// Viewing right part and want to move left
return true
} else if y > maxY && direction == 2 {
// Movable to down ana want ot move down
return true
} else if t.offsetRow > 0 && direction == 0 {
// Viewing bottom part and want to move up
return true
}
return false
}
func (t *Table) SetOffsetRow(r int) {
t.offsetRow = r
}
func (t *Table) MoveRight(move int) {
if !t.isMovable(1) {
return
}
t.offsetCol += move
if t.offsetCol > len(t.header.cols) {
t.offsetRow = len(t.header.cols)
}
}
func (t *Table) MoveLeft(move int) {
if !t.isMovable(3) {
return
}
t.offsetCol -= move
if t.offsetCol < 0 {
t.offsetCol = 0
}
}
func (t *Table) MoveUp(move int) {
if !t.isMovable(0) {
return
}
// Skip invisible rows
for i := t.offsetRow; !t.contents[i].isVisible && i > 0; i-- {
if !t.contents[i].isVisible {
move++
}
}
t.offsetRow -= move
if t.offsetRow < 0 {
t.offsetRow = 0
}
}
func (t *Table) MoveDown(move int) {
if !t.isMovable(2) {
return
}
// Skip invisible rows
for i := t.offsetRow; !t.contents[i].isVisible && i < len(t.contents); i++ {
if !t.contents[i].isVisible {
move++
}
}
t.offsetRow += move
if t.offsetRow > len(t.contents) {
t.offsetRow = len(t.contents)
}
}
func (t *Table) Debugdump() {
fmt.Println("---- Header ----")
for _, cell := range t.header.cols {
fmt.Print(cell.data, " ")
}
fmt.Println("")
fmt.Println("---- Contents -----------")
for _, row := range t.contents {
for _, cell := range row.cols {
fmt.Print(cell.data, " ")
}
fmt.Println("")
}
fmt.Println("maxLen: ", t.maxLen)
}