-
Notifications
You must be signed in to change notification settings - Fork 27
/
drawbox.go
151 lines (135 loc) · 4.03 KB
/
drawbox.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
package strutil
import (
"errors"
"strings"
)
// Box9Slice is used by DrawBox functions to draw frames around text content by
// defining the corner and edge characters. See DefaultBox9Slice for an
// example
type Box9Slice struct {
Top string
TopRight string
Right string
BottomRight string
Bottom string
BottomLeft string
Left string
TopLeft string
}
var defaultBox9Slice = Box9Slice{
Top: "─",
TopRight: "┐",
Right: "│",
BottomRight: "┘",
Bottom: "─",
BottomLeft: "└",
Left: "│",
TopLeft: "┌",
}
// DefaultBox9Slice defines the character object to use with "CustomBox".
// It is used as Box9Slice object in "DrawBox" function.
//
// Usage:
// DrawCustomBox("Hello World", 20, AligntTypeCenter, DefaultBox9Slice())
//
// Outputs:
// <code>
// ┌──────────────────┐
// │ Hello World │
// └──────────────────┘
// </code>
func DefaultBox9Slice() Box9Slice {
return defaultBox9Slice
}
var simpleBox9Slice = Box9Slice{
Top: "-",
TopRight: "+",
Right: "|",
BottomRight: "+",
Bottom: "-",
BottomLeft: "+",
Left: "|",
TopLeft: "+",
}
// SimpleBox9Slice defines a character set to use with DrawCustomBox. It uses
// only simple ASCII characters
//
// Usage:
// DrawCustomBox("Hello World", 20, Center, SimpleBox9Slice(), "\n")
//
// Outputs:
// +------------------+
// | Hello World |
// +------------------+
func SimpleBox9Slice() Box9Slice {
return simpleBox9Slice
}
// DrawCustomBox creates a frame with "content" in it. Characters in the frame is specified by "chars".
// "align" sets the alignment of the content. It must be one of the strutil.AlignType constants.
// There are 2 premade Box9Slice objects that can be retrieved by strutil.DefaultBox9Slice() or
// strutil.SimpleBox9Slice()
//
// Usage:
// DrawCustomBox("Hello World", 20, Center, SimpleBox9Slice(), "\n")
//
// Outputs:
// +------------------+
// | Hello World |
// +------------------+
func DrawCustomBox(content string, width int, align AlignType, chars Box9Slice, strNewLine string) (string, error) {
nl := []byte("\n")
if strNewLine != "" {
nl = []byte(strNewLine)
}
var topInsideWidth = width - Len(chars.TopLeft) - Len(chars.TopRight)
var middleInsideWidth = width - Len(chars.Left) - Len(chars.Right)
var bottomInsideWidth = width - Len(chars.BottomLeft) - Len(chars.BottomRight)
if topInsideWidth < 1 || middleInsideWidth < 1 || bottomInsideWidth < 1 {
return "", errors.New("no enough width")
}
content = WordWrap(content, middleInsideWidth, true)
lines := strings.Split(content, "\n")
var buff strings.Builder
minNumBytes := (width + 1) * (len(lines) + 2)
buff.Grow(minNumBytes)
//top
buff.WriteString(chars.TopLeft)
buff.WriteString(Tile(chars.Top, topInsideWidth))
buff.WriteString(chars.TopRight)
buff.Write(nl)
//middle
left := []byte(chars.Left)
right := []byte(chars.Right)
for _, line := range lines {
line = Align(line, align, middleInsideWidth)
if align == Left {
line = PadRight(line, middleInsideWidth, " ")
}
if line == "" {
line = strings.Repeat(" ", middleInsideWidth)
}
buff.Write(left)
buff.WriteString(line)
buff.Write(right)
buff.Write(nl)
}
//bottom
buff.WriteString(chars.BottomLeft)
buff.WriteString(Tile(chars.Bottom, bottomInsideWidth))
buff.WriteString(chars.BottomRight)
return buff.String(), nil
}
// DrawBox creates a frame with "content" in it. DefaultBox9Slice object is used to
// define characters in the frame. "align" sets the alignment of the content.
// It must be one of the strutil.AlignType constants.
//
// Usage:
// DrawBox("Hello World", 20, Center)
//
// Outputs:
// ┌──────────────────┐
// │ Hello World │
// └──────────────────┘
func DrawBox(content string, width int, align AlignType) (string, error) {
return DrawCustomBox(content, width, align, defaultBox9Slice, "\n")
}