-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.go
64 lines (50 loc) · 1.17 KB
/
canvas.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
package boxes
import "bytes"
type Canvas struct {
Boxes []*Box
}
func NewCanvas() *Canvas {
return &Canvas{}
}
func (c *Canvas) AddBox(box *Box) {
c.Boxes = append(c.Boxes, box)
}
func (c *Canvas) Render() (data []byte) {
for i, box := range c.Boxes {
if i > 0 {
data = append(data, byte('\n'))
}
data = append(data, box.Render()...)
}
return
}
func (c *Canvas) HorizontalRender() (final []byte) {
for i, box := range c.Boxes {
data := box.Render()
if i == 0 {
final = append(final, data...)
continue
}
finalLines := bytes.Split(final, []byte("\n"))
boxLines := bytes.Split(data, []byte("\n"))
width := len(finalLines[0])
rows := len(finalLines)
if len(boxLines) > rows {
rows = len(boxLines)
}
for j := 0; j <= rows; j++ {
if j >= len(finalLines) {
line := bytes.Repeat([]byte(" "), width)
finalLines = append(finalLines, line)
}
if j >= len(boxLines) {
line := bytes.Repeat([]byte(" "), len(boxLines[j-1]))
boxLines = append(boxLines, line)
}
finalLines[j] = append(finalLines[j], ' ')
finalLines[j] = append(finalLines[j], boxLines[j]...)
}
final = bytes.Join(finalLines, []byte("\n"))
}
return
}