/
text.go
56 lines (51 loc) · 944 Bytes
/
text.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
package goc
import "github.com/nsf/termbox-go"
type Text struct {
Content []termbox.Cell
}
func (t *Text) Cells() ([]termbox.Cell, int) {
width := 0
lines := t.Lines()
for _, l := range lines {
if ll := len(l); ll > width {
width = ll
}
}
cells := []termbox.Cell{}
for _, l := range lines {
ll := len(l)
cells = append(cells, l...)
for i := 0; i < width-ll; i++ {
cells = append(cells, termbox.Cell{Ch: 0})
}
}
return cells, width
}
func (t *Text) Lines() [][]termbox.Cell {
lines := [][]termbox.Cell{}
line := []termbox.Cell{}
for _, c := range t.Content {
if c.Ch == '\n' {
lines = append(lines, line)
line = []termbox.Cell{}
} else {
line = append(line, c)
}
}
return lines
}
func (t *Text) Size() (width, height int) {
if t.Content == nil {
return
}
line := 0
for _, c := range t.Content {
if c.Ch == '\n' {
height++
if line > width {
width = line
}
}
}
return
}