-
Notifications
You must be signed in to change notification settings - Fork 0
/
box.go
109 lines (90 loc) · 2.02 KB
/
box.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
package boxes
import (
"bytes"
"math"
"github.com/rs/xid"
)
const (
bottomCorner = "'"
topCorner = "."
row = "-"
column = "|"
space = " "
newLine = "\n"
)
// Box is a container for a content
type Box struct {
id []byte
padding int
content *bytes.Buffer
}
func New() *Box {
return &Box{
id: xid.New().Bytes(),
content: bytes.NewBuffer(nil),
}
}
func (b *Box) Padding(padding int) *Box {
if padding < 1 {
b.padding = 1
}
b.padding = padding
return b
}
func (b *Box) Write(content []byte) (*Box, error) {
_, err := b.content.Write(content)
return b, err
}
func (b *Box) WriteString(content string) (*Box, error) {
return b.Write([]byte(content))
}
func (b *Box) Bytes() []byte {
return b.content.Bytes()
}
func (b *Box) Render() []byte {
var lineSize int
content := b.content.Bytes()
lines := bytes.Split(content, []byte("\n"))
for _, line := range lines {
if len(line) > lineSize {
lineSize = len(line)
}
}
padding := b.padding
lineSize = lineSize + (padding * 2)
fullRow := bytes.Repeat([]byte(row), lineSize)
emptyRow := bytes.Repeat([]byte(" "), lineSize)
final := bytes.NewBuffer(nil)
final.Write([]byte(topCorner))
final.Write(fullRow)
final.Write([]byte(topCorner))
final.WriteString(newLine)
linePadding := b.padding
if linePadding > 1 {
linePadding = int(math.Ceil(float64(linePadding) * 0.3))
}
for i := 0; i < linePadding; i++ {
final.WriteString(column)
final.Write(emptyRow)
final.WriteString(column)
final.WriteString(newLine)
}
for _, line := range lines {
final.WriteString(column)
final.Write(emptyRow[0:b.padding])
final.Write(line)
final.Write(emptyRow[0 : len(emptyRow)-len(line)-(padding)])
final.WriteString(column)
final.WriteString(newLine)
}
for i := 0; i < linePadding; i++ {
final.WriteString(column)
final.Write(emptyRow)
final.WriteString(column)
final.WriteString(newLine)
}
final.Write([]byte(bottomCorner))
final.Write(fullRow)
final.Write([]byte(bottomCorner))
return final.Bytes()
}