-
Notifications
You must be signed in to change notification settings - Fork 31
/
cell.go
190 lines (149 loc) · 3.62 KB
/
cell.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
package simpletable
const (
// AlignLeft sets cell left alignment (default)
AlignLeft = 0
// AlignCenter sets cell center alignment
AlignCenter = 1
// AlignRight sets cell right alignment
AlignRight = 2
)
// cellInterface is a basic cell interface
type cellInterface interface {
len() int // Returns cell text length
isSpanned() bool // Returns true if cell spanned
width() int // Returns cell content width
setWidth(int) // Sets cell content width
height() int // Returns cell content height
setHeight(int) // Sets cell content height
getColumn() *tblColumn // Returns parent column
setColumn(*tblColumn) // Sets parent column
lines() []string // Returns cell as string slice
}
// Cell is a table cell
type Cell struct {
Align int // Cell alignment
Span int // span cell to right (1 - default)
Text string // Cell raw text
content *content // Cell content object instance
children []*emptyCell // Nested empty cells
column *tblColumn // Parent column
}
func (c *Cell) len() int {
return c.width()
}
func (c *Cell) isSpanned() bool {
return c.Span > 1
}
func (c *Cell) width() int {
return c.content.width()
}
func (c *Cell) setWidth(width int) {
c.content.setWidth(width)
}
func (c *Cell) height() int {
return c.content.height()
}
func (c *Cell) setHeight(height int) {
c.content.setHeight(height)
}
func (c *Cell) getColumn() *tblColumn {
return c.column
}
func (c *Cell) setColumn(column *tblColumn) {
c.column = column
}
func (c *Cell) resize() {
if !c.isSpanned() {
return
}
s := c.column.getWidth()
for _, ch := range c.children {
s += ch.getColumn().getWidth()
}
s += len(c.children) * 3
if s > c.len() {
c.setWidth(s)
} else {
cols := []*tblColumn{
c.column,
}
for _, ch := range c.children {
cols = append(cols, ch.column)
}
c.column.Table.incrementColumns(cols, c.len()-s)
}
}
func (c *Cell) lines() []string {
return c.content.lines(c.Align)
}
// dividerCell is table divider cell
type dividerCell struct {
span int // Divider span
children []*emptyCell // Nested empty meta cells
column *tblColumn // Divider parent column
}
func (d *dividerCell) len() int {
return 1
}
func (d *dividerCell) isSpanned() bool {
return d.span > 1
}
func (d *dividerCell) width() int {
return 1
}
func (d *dividerCell) setWidth(width int) {
}
func (d *dividerCell) height() int {
return 1
}
func (d *dividerCell) setHeight(height int) {
}
func (d *dividerCell) getColumn() *tblColumn {
return d.column
}
func (d *dividerCell) setColumn(column *tblColumn) {
d.column = column
}
func (d *dividerCell) lines() []string {
s := d.column.Table.style.Divider
c := &content{
c: []string{
d.column.Table.line(s.Left, s.Center, s.Right, s.Intersection),
},
}
return c.lines(AlignLeft)
}
// emptyCell is meta cell, used when cell is spanned
type emptyCell struct {
parent cellInterface // Parent cell
w int // Cell width
column *tblColumn // Parent cell column
content *content // Cell content
}
func (e *emptyCell) len() int {
return 0
}
func (e *emptyCell) isSpanned() bool {
return false
}
func (e *emptyCell) width() int {
return e.w
}
func (e *emptyCell) setWidth(width int) {
e.w = width
}
func (e *emptyCell) height() int {
return e.content.height()
}
func (e *emptyCell) setHeight(height int) {
e.content.setHeight(height)
}
func (e *emptyCell) getColumn() *tblColumn {
return e.column
}
func (e *emptyCell) setColumn(column *tblColumn) {
e.column = column
}
func (e *emptyCell) lines() []string {
return e.content.lines(AlignLeft)
}