-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
55 lines (47 loc) · 1.16 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
package tablook
import (
"errors"
"github.com/mattn/go-runewidth"
)
var ErrTooFewRecords = errors.New("tablook: at least one row must be given in addition to header row")
type Tab struct {
records [][]string
maxLen []int
selected int // in view
selColumn int
currentY int // current y-axis shift
currentX int // current x-axis shift
toSkip int // how many cells we should skip when printing firstCol
firstCol int // from which column we should start printing
isColDel []bool
}
func New(records [][]string) (Tab, error) {
if len(records) < 2 {
return Tab{}, ErrTooFewRecords
}
data := Tab{selected: 1, selColumn: -1}
data.records = records
maxLen := make([]int, data.cols())
for _, rcrd := range records {
for i, col := range rcrd {
l := runewidth.StringWidth(col)
if maxLen[i] < l {
maxLen[i] = l
}
}
}
data.maxLen = maxLen
data.isColDel = make([]bool, data.cols())
return data, nil
}
func (data Tab) lenSum() int {
s := 0
for i, x := range data.maxLen {
if !data.isColDel[i] {
s += x
}
}
return s
}
func (data Tab) cols() int { return len(data.records[0]) }
func (data Tab) rows() int { return len(data.records) }