-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.go
63 lines (56 loc) · 1.08 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
package chocotable
import (
"fmt"
"strings"
runewidth "github.com/mattn/go-runewidth"
"github.com/stny/goodbye"
)
type Cell struct {
lines []string
Width int
Height int
pos int
table *Table
}
func NewCell(table *Table, pos int, data interface{}) *Cell {
s := fmt.Sprintf("%v", data)
c := &Cell{}
c.table = table
c.pos = pos
c.lines = strings.Split(s, "\n")
c.calcMaxWidth()
c.calcHeight()
return c
}
func (c *Cell) Render(index int) string {
var ret string
width := c.table.WidthOfColumns[c.pos]
if index > len(c.lines)-1 {
return " " + strings.Repeat(" ", width) + " "
}
line := c.lines[index]
switch c.table.Align {
case LEFT:
ret = Ljust(line, width, " ")
case RIGHT:
ret = Rjust(line, width, " ")
case CENTER:
fallthrough
default:
ret = Center(line, width, " ")
}
return " " + ret + " "
}
func (c *Cell) calcHeight() {
c.Height = len(c.lines)
}
func (c *Cell) calcMaxWidth() {
var ret int
for _, line := range c.lines {
length := runewidth.StringWidth(goodbye.ANSIColorCodes(line))
if length > ret {
ret = length
}
}
c.Width = ret
}