forked from gizak/termui
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathblock.go
More file actions
301 lines (295 loc) · 6.88 KB
/
block.go
File metadata and controls
301 lines (295 loc) · 6.88 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package gotui
import (
"image"
)
// NewBlock returns a new Block.
func NewBlock() *Block {
return &Block{
Border: true,
BorderStyle: Theme.Block.Border,
BorderLeft: true,
BorderRight: true,
BorderTop: true,
BorderBottom: true,
BorderCollapse: false,
TitleStyle: Theme.Block.Title,
TitleAlignment: AlignLeft,
TitleBottomStyle: Theme.Block.Title,
TitleBottomAlignment: AlignLeft,
}
}
// drawBorder draws the border of the block to the buffer.
func (b *Block) drawBorder(buf *Buffer) {
var gradientSchema []Color
if b.BorderGradient.Enabled {
if len(b.BorderGradient.Stops) > 0 {
if b.BorderGradient.Direction == GradientVertical {
gradientSchema = GenerateMultiGradient(b.Dy(), b.BorderGradient.Stops...)
} else {
gradientSchema = GenerateMultiGradient(b.Dx(), b.BorderGradient.Stops...)
}
} else {
if b.BorderGradient.Direction == GradientVertical {
gradientSchema = GenerateGradient(b.BorderGradient.Start, b.BorderGradient.End, b.Dy())
} else {
gradientSchema = GenerateGradient(b.BorderGradient.Start, b.BorderGradient.End, b.Dx())
}
}
}
drawRune := func(r rune, p image.Point) {
if b.BorderCollapse {
existing := buf.GetCell(p).Rune
r = ResolveBorderRune(existing, r)
}
style := b.BorderStyle
if b.BackgroundColor != ColorClear && b.FillBorder {
style.Bg = b.BackgroundColor
}
if b.BorderGradient.Enabled {
var idx int
if b.BorderGradient.Direction == GradientVertical {
idx = p.Y - b.Min.Y
} else {
idx = p.X - b.Min.X
}
if idx >= 0 && idx < len(gradientSchema) {
style.Fg = gradientSchema[idx]
}
}
buf.SetCell(Cell{r, style}, p)
}
b.drawBorderLines(drawRune)
b.drawBorderCorners(drawRune)
}
func (b *Block) getBorderRunes() (top, bottom, left, right, tl, tr, bl, br rune) {
top = HORIZONTAL_LINE
bottom = HORIZONTAL_LINE
left = VERTICAL_LINE
right = VERTICAL_LINE
tl = TOP_LEFT
tr = TOP_RIGHT
bl = BOTTOM_LEFT
br = BOTTOM_RIGHT
if b.BorderSet != nil {
top = b.BorderSet.Top
bottom = b.BorderSet.Bottom
left = b.BorderSet.Left
right = b.BorderSet.Right
tl = b.BorderSet.TopLeft
tr = b.BorderSet.TopRight
bl = b.BorderSet.BottomLeft
br = b.BorderSet.BottomRight
return
}
if b.BorderRounded {
tl = ROUNDED_TOP_LEFT
tr = ROUNDED_TOP_RIGHT
bl = ROUNDED_BOTTOM_LEFT
br = ROUNDED_BOTTOM_RIGHT
return
}
switch b.BorderType {
case BorderBlock:
top = '▀'
bottom = '▄'
left = '▌'
right = '▐'
tl = '█'
tr = '█'
bl = '█'
br = '█'
case BorderDouble:
top = '═'
bottom = '═'
left = '║'
right = '║'
tl = '╔'
tr = '╗'
bl = '╚'
br = '╝'
case BorderThick:
top = '━'
bottom = '━'
left = '┃'
right = '┃'
tl = '┏'
tr = '┓'
bl = '┗'
br = '┛'
}
return
}
func (b *Block) drawBorderLines(drawRune func(rune, image.Point)) {
top, bottom, left, right, _, _, _, _ := b.getBorderRunes()
if b.BorderTop {
b.drawHorizontalBorder(drawRune, b.Min.Y, top)
}
if b.BorderBottom {
b.drawHorizontalBorder(drawRune, b.Max.Y-1, bottom)
}
if b.BorderLeft {
b.drawVerticalBorder(drawRune, b.Min.X, left)
}
if b.BorderRight {
b.drawVerticalBorder(drawRune, b.Max.X-1, right)
}
}
func (b *Block) drawHorizontalBorder(drawRune func(rune, image.Point), y int, r rune) {
xStart := b.Min.X
xEnd := b.Max.X
if b.BorderLeft {
xStart++
}
if b.BorderRight {
xEnd--
}
for x := xStart; x < xEnd; x++ {
drawRune(r, image.Pt(x, y))
}
}
func (b *Block) drawVerticalBorder(drawRune func(rune, image.Point), x int, r rune) {
yStart := b.Min.Y
yEnd := b.Max.Y
if b.BorderTop {
yStart++
}
if b.BorderBottom {
yEnd--
}
for y := yStart; y < yEnd; y++ {
drawRune(r, image.Pt(x, y))
}
}
func (b *Block) drawBorderCorners(drawRune func(rune, image.Point)) {
_, _, _, _, tl, tr, bl, br := b.getBorderRunes()
if b.BorderTop && b.BorderLeft {
drawRune(tl, b.Min)
}
if b.BorderTop && b.BorderRight {
drawRune(tr, image.Pt(b.Max.X-1, b.Min.Y))
}
if b.BorderBottom && b.BorderLeft {
drawRune(bl, image.Pt(b.Min.X, b.Max.Y-1))
}
if b.BorderBottom && b.BorderRight {
drawRune(br, b.Max.Sub(image.Pt(1, 1)))
}
}
// Draw draws the block to the buffer.
func (b *Block) Draw(buf *Buffer) {
b.drawBackground(buf)
if b.Border {
b.drawBorder(buf)
}
b.drawTitles(buf)
}
func (b *Block) drawBackground(buf *Buffer) {
if b.BackgroundColor != ColorClear {
bgCell := NewCell(' ', NewStyle(ColorClear, b.BackgroundColor))
bgRect := b.Rectangle
if !b.FillBorder && b.Border {
if b.BorderTop {
bgRect.Min.Y++
}
if b.BorderBottom {
bgRect.Max.Y--
}
if b.BorderLeft {
bgRect.Min.X++
}
if b.BorderRight {
bgRect.Max.X--
}
}
if bgRect.Min.X < bgRect.Max.X && bgRect.Min.Y < bgRect.Max.Y {
buf.Fill(bgCell, bgRect)
}
}
}
func (b *Block) drawTitles(buf *Buffer) {
titleX := b.Min.X + 2
switch b.TitleAlignment {
case AlignCenter:
titleX = b.Min.X + (b.Max.X-b.Min.X-len(b.Title))/2
case AlignRight:
titleX = b.Max.X - len(b.Title) - 2
}
// Clamp to minimum X to prevent negative positions
if titleX < b.Min.X {
titleX = b.Min.X
}
buf.SetString(
b.Title,
b.TitleStyle,
image.Pt(titleX, b.Min.Y),
)
if b.TitleLeft != "" {
buf.SetString(
b.TitleLeft,
b.TitleStyle,
image.Pt(b.Min.X+2, b.Min.Y),
)
}
if b.TitleRight != "" {
rightX := max(b.Max.X-len(b.TitleRight)-2, b.Min.X)
buf.SetString(
b.TitleRight,
b.TitleStyle,
image.Pt(rightX, b.Min.Y),
)
}
bottomTitleX := b.Min.X + 2
switch b.TitleBottomAlignment {
case AlignCenter:
bottomTitleX = b.Min.X + (b.Max.X-b.Min.X-len(b.TitleBottom))/2
case AlignRight:
bottomTitleX = b.Max.X - len(b.TitleBottom) - 2
}
// Clamp to minimum X to prevent negative positions
if bottomTitleX < b.Min.X {
bottomTitleX = b.Min.X
}
buf.SetString(
b.TitleBottom,
b.TitleBottomStyle,
image.Pt(bottomTitleX, b.Max.Y-1),
)
if b.TitleBottomLeft != "" {
buf.SetString(
b.TitleBottomLeft,
b.TitleBottomStyle,
image.Pt(b.Min.X+2, b.Max.Y-1),
)
}
if b.TitleBottomRight != "" {
rightX := max(b.Max.X-len(b.TitleBottomRight)-2, b.Min.X)
buf.SetString(
b.TitleBottomRight,
b.TitleBottomStyle,
image.Pt(rightX, b.Max.Y-1),
)
}
}
// SetRect sets the rectangle of the block.
func (b *Block) SetRect(x1, y1, x2, y2 int) {
b.Rectangle = image.Rect(x1, y1, x2, y2)
innerMinX := b.Min.X + 1 + b.PaddingLeft
innerMinY := b.Min.Y + 1 + b.PaddingTop
innerMaxX := b.Max.X - 1 - b.PaddingRight
innerMaxY := b.Max.Y - 1 - b.PaddingBottom
if innerMinX > innerMaxX {
mid := b.Min.X + (b.Max.X-b.Min.X)/2
innerMinX = mid
innerMaxX = mid
}
if innerMinY > innerMaxY {
mid := b.Min.Y + (b.Max.Y-b.Min.Y)/2
innerMinY = mid
innerMaxY = mid
}
b.Inner = image.Rect(innerMinX, innerMinY, innerMaxX, innerMaxY)
}
// GetRect returns the rectangle of the block.
func (b *Block) GetRect() image.Rectangle {
return b.Rectangle
}