forked from charmbracelet/ultraviolet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathborder.go
More file actions
231 lines (215 loc) ยท 6.19 KB
/
Copy pathborder.go
File metadata and controls
231 lines (215 loc) ยท 6.19 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package uv
// NormalBorder returns a standard-type border with a normal weight and 90
// degree corners.
func NormalBorder() Border {
return Border{
Top: Side{Content: "โ"},
Bottom: Side{Content: "โ"},
Left: Side{Content: "โ"},
Right: Side{Content: "โ"},
TopLeft: Side{Content: "โ"},
TopRight: Side{Content: "โ"},
BottomLeft: Side{Content: "โ"},
BottomRight: Side{Content: "โ"},
}
}
// RoundedBorder returns a border with rounded corners.
func RoundedBorder() Border {
return Border{
Top: Side{Content: "โ"},
Bottom: Side{Content: "โ"},
Left: Side{Content: "โ"},
Right: Side{Content: "โ"},
TopLeft: Side{Content: "โญ"},
TopRight: Side{Content: "โฎ"},
BottomLeft: Side{Content: "โฐ"},
BottomRight: Side{Content: "โฏ"},
}
}
// BlockBorder returns a border that takes the whole block.
func BlockBorder() Border {
return Border{
Top: Side{Content: "โ"},
Bottom: Side{Content: "โ"},
Left: Side{Content: "โ"},
Right: Side{Content: "โ"},
TopLeft: Side{Content: "โ"},
TopRight: Side{Content: "โ"},
BottomLeft: Side{Content: "โ"},
BottomRight: Side{Content: "โ"},
}
}
// OuterHalfBlockBorder returns a half-block border that sits outside the frame.
func OuterHalfBlockBorder() Border {
return Border{
Top: Side{Content: "โ"},
Bottom: Side{Content: "โ"},
Left: Side{Content: "โ"},
Right: Side{Content: "โ"},
TopLeft: Side{Content: "โ"},
TopRight: Side{Content: "โ"},
BottomLeft: Side{Content: "โ"},
BottomRight: Side{Content: "โ"},
}
}
// InnerHalfBlockBorder returns a half-block border that sits inside the frame.
func InnerHalfBlockBorder() Border {
return Border{
Top: Side{Content: "โ"},
Bottom: Side{Content: "โ"},
Left: Side{Content: "โ"},
Right: Side{Content: "โ"},
TopLeft: Side{Content: "โ"},
TopRight: Side{Content: "โ"},
BottomLeft: Side{Content: "โ"},
BottomRight: Side{Content: "โ"},
}
}
// ThickBorder returns a border that's thicker than the one returned by
// NormalBorder.
func ThickBorder() Border {
return Border{
Top: Side{Content: "โ"},
Bottom: Side{Content: "โ"},
Left: Side{Content: "โ"},
Right: Side{Content: "โ"},
TopLeft: Side{Content: "โ"},
TopRight: Side{Content: "โ"},
BottomLeft: Side{Content: "โ"},
BottomRight: Side{Content: "โ"},
}
}
// DoubleBorder returns a border comprised of two thin strokes.
func DoubleBorder() Border {
return Border{
Top: Side{Content: "โ"},
Bottom: Side{Content: "โ"},
Left: Side{Content: "โ"},
Right: Side{Content: "โ"},
TopLeft: Side{Content: "โ"},
TopRight: Side{Content: "โ"},
BottomLeft: Side{Content: "โ"},
BottomRight: Side{Content: "โ"},
}
}
// HiddenBorder returns a border that renders as a series of single-cell
// spaces. It's useful for cases when you want to remove a standard border but
// maintain layout positioning. This said, you can still apply a background
// color to a hidden border.
func HiddenBorder() Border {
return Border{
Top: Side{Content: " "},
Bottom: Side{Content: " "},
Left: Side{Content: " "},
Right: Side{Content: " "},
TopLeft: Side{Content: " "},
TopRight: Side{Content: " "},
BottomLeft: Side{Content: " "},
BottomRight: Side{Content: " "},
}
}
// MarkdownBorder return a table border in markdown style.
func MarkdownBorder() Border {
return Border{
Left: Side{Content: "|"},
Right: Side{Content: "|"},
TopLeft: Side{Content: "|"},
TopRight: Side{Content: "|"},
BottomLeft: Side{Content: "|"},
BottomRight: Side{Content: "|"},
}
}
// ASCIIBorder returns a table border with ASCII characters.
func ASCIIBorder() Border {
return Border{
Top: Side{Content: "-"},
Bottom: Side{Content: "-"},
Left: Side{Content: "|"},
Right: Side{Content: "|"},
TopLeft: Side{Content: "+"},
TopRight: Side{Content: "+"},
BottomLeft: Side{Content: "+"},
BottomRight: Side{Content: "+"},
}
}
// Side represents a single border side with its properties.
type Side struct {
Content string
Style
Link
}
// Border represents a border with its properties.
type Border struct {
Top Side
Bottom Side
Left Side
Right Side
TopLeft Side
TopRight Side
BottomLeft Side
BottomRight Side
}
// Style returns a new [Border] with the given style applied to all [Side]s.
func (b Border) Style(style Style) Border {
b.Top.Style = style
b.Bottom.Style = style
b.Left.Style = style
b.Right.Style = style
b.TopLeft.Style = style
b.TopRight.Style = style
b.BottomLeft.Style = style
b.BottomRight.Style = style
return b
}
// Link returns a new [Border] with the given link applied to all [Side]s.
func (b Border) Link(link Link) Border {
b.Top.Link = link
b.Bottom.Link = link
b.Left.Link = link
b.Right.Link = link
b.TopLeft.Link = link
b.TopRight.Link = link
b.BottomLeft.Link = link
b.BottomRight.Link = link
return b
}
// Draw draws the border around the given component.
func (b *Border) Draw(scr Screen, area Rectangle) {
for y := area.Min.Y; y < area.Max.Y; y++ {
for x := area.Min.X; x < area.Max.X; x++ {
var cell *Cell
switch {
case y == area.Min.Y && x == area.Min.X:
cell = borderCell(scr, &b.TopLeft)
case y == area.Min.Y && x == area.Max.X-1:
cell = borderCell(scr, &b.TopRight)
case y == area.Max.Y-1 && x == area.Min.X:
cell = borderCell(scr, &b.BottomLeft)
case y == area.Max.Y-1 && x == area.Max.X-1:
cell = borderCell(scr, &b.BottomRight)
case y == area.Min.Y:
cell = borderCell(scr, &b.Top)
case y == area.Max.Y-1:
cell = borderCell(scr, &b.Bottom)
case x == area.Min.X:
cell = borderCell(scr, &b.Left)
case x == area.Max.X-1:
cell = borderCell(scr, &b.Right)
default:
continue
}
if cell == nil {
continue
}
scr.SetCell(x, y, cell)
}
}
}
func borderCell(scr Screen, b *Side) *Cell {
c := NewCell(scr.WidthMethod(), b.Content)
if c != nil {
c.Style = b.Style
c.Link = b.Link
}
return c
}