-
Notifications
You must be signed in to change notification settings - Fork 31
/
row.go
89 lines (71 loc) · 1.39 KB
/
row.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
package simpletable
import (
"fmt"
"strings"
)
// tblRow is a meta table row
type tblRow struct {
Cells []cellInterface
Table *Table
}
// len returns row length
func (r *tblRow) len() int {
l := 0
for _, c := range r.Cells {
l += c.width()
}
len := len(r.Cells)
return l + (3 * len) - 1
}
// toStringSlice returns row contents as a toString
func (r *tblRow) toStringSlice() []string {
l := [][]string{}
for _, c := range r.Cells {
l = append(l, c.lines())
}
l = r.transpose(l)
ret := []string{}
for _, s := range l {
row := strings.Join(s, fmt.Sprintf(" %s ", r.Table.style.Cell))
if !r.isDivider() {
row = fmt.Sprintf(" %s ", row)
}
ret = append(ret, row)
}
return ret
}
// transpose transposes slice of string slices
func (r *tblRow) transpose(s [][]string) [][]string {
ret := [][]string{}
l := len(s)
for x := 0; x < len(s[0]); x++ {
ret = append(ret, make([]string, l))
}
for x, row := range s {
for y, c := range row {
ret[y][x] = c
}
}
return ret
}
// resize resets all cells height
func (r *tblRow) resize() {
m := 0
for _, c := range r.Cells {
h := c.height()
if h > m {
m = h
}
}
for _, c := range r.Cells {
c.setHeight(m)
}
}
// isDivider check if a row is divider (if first row cell is divider - row divider to)
func (r *tblRow) isDivider() bool {
switch r.Cells[0].(type) {
case *dividerCell:
return true
}
return false
}