forked from kungfusheep/glyph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathborder_test.go
More file actions
246 lines (217 loc) · 6.12 KB
/
border_test.go
File metadata and controls
246 lines (217 loc) · 6.12 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
package glyph
import (
"testing"
)
// TestIfWithGrowBorder tests that a bordered container inside an If with Grow
// has its bottom border drawn at the correct position.
func TestIfWithGrowBorder(t *testing.T) {
showProcs := true
// Mimic the dashboard structure:
// VBoxNode{Grow(1)} containing:
// - VBoxNode{Border, Grow(1)} "Timing"
// - If(showProcs).Then(VBoxNode{Border, Grow(2)} "Processes")
view := VBox.Grow(1)(
VBox.Border(BorderSingle).BorderFG(Yellow).Title("Timing").Grow(1)(
Text("Line 1"),
Text("Line 2"),
Text("Line 3"),
),
If(&showProcs).Eq(true).Then(VBox.Border(BorderSingle).BorderFG(BrightBlue).Title("Processes").Grow(2)(
Text("Process 1"),
Text("Process 2"),
Text("Process 3"),
)),
)
// Build template
tmpl := Build(view)
// Create a buffer with specific dimensions
buf := NewBuffer(80, 30)
// Execute template - this does width distribution, layout, and render
tmpl.Execute(buf, 80, 30)
// Print the buffer to see the output
t.Log("Buffer contents:")
for y := 0; y < 30; y++ {
line := buf.GetLine(y)
if line != "" {
t.Logf("Line %2d: %s", y, line)
}
}
// Check that Timing box bottom border exists
timingBottomFound := false
processesBottomFound := false
for y := 0; y < 30; y++ {
for x := 0; x < 80; x++ {
cell := buf.Get(x, y)
// Check for bottom-left corner of a box
if cell.Rune == BorderSingle.BottomLeft {
// Look at the next few chars to see if it's a border
if x+1 < 80 {
nextCell := buf.Get(x+1, y)
if nextCell.Rune == BorderSingle.Horizontal {
// This is a bottom border
// Check color to distinguish Timing (Yellow) vs Processes (BrightBlue)
if cell.Style.FG == Yellow {
timingBottomFound = true
t.Logf("Found Timing bottom border at y=%d", y)
} else if cell.Style.FG == BrightBlue {
processesBottomFound = true
t.Logf("Found Processes bottom border at y=%d", y)
}
}
}
}
}
}
if !timingBottomFound {
t.Error("Timing box bottom border not found")
}
if !processesBottomFound {
t.Error("Processes box bottom border not found")
}
}
// TestDashboardLayoutBorders tests the full dashboard-like structure with
// nested grows and conditionals.
func TestDashboardLayoutBorders(t *testing.T) {
showProcs := true
showGraph := true
// Full dashboard structure (simplified):
// VBoxNode{Children: [
// Text "Header"
// Text "Progress bars"
// HBoxNode{Children: [Left.Grow(1), Right.Grow(2)]} // Horizontal flex
// VBoxNode{Children: [
// Col "Timing".Grow(1)
// If.Then(Col "Processes".Grow(2))
// ]}.Grow(1) // The OUTER Col also has Grow!
// Text "Footer"
// ]}
view := VBox(
// Fixed header
Text("Dashboard Header"),
Text("CPU: [████████████________] 60%"),
// Main content row (horizontal flex)
HBox.Gap(1)(
VBox.Border(BorderSingle).BorderFG(Cyan).Title("Stats").Grow(1)(
Text("Tasks: 100"),
Text("Memory: 4GB"),
),
VBox.Border(BorderRounded).BorderFG(Green).Title("Load").Grow(2)(
If(&showGraph).Eq(true).Then(
Text("Graph: ▁▂▃▄▅▆▇█"),
),
),
),
// Middle section with vertical flex - THIS IS THE KEY PART
// The outer Col has Grow(1), inner children have Grow(1) and Grow(2)
VBox.Grow(1)( // <-- OUTER COL HAS GROW!
VBox.Border(BorderDouble).BorderFG(Yellow).Title("Timing").Grow(1)(
Text("Render: 100µs"),
Text("Flush: 50µs"),
),
If(&showProcs).Eq(true).Then(VBox.Border(BorderSingle).BorderFG(BrightBlue).Title("Processes").Grow(2)(
Text("PID NAME CPU"),
Text("1001 nginx 2.5%"),
Text("1002 node 5.2%"),
)),
),
// Fixed footer
Text("Press q to quit"),
)
tmpl := Build(view)
buf := NewBuffer(80, 40)
tmpl.Execute(buf, 80, 40)
t.Log("Buffer contents:")
for y := 0; y < 40; y++ {
line := buf.GetLine(y)
if line != "" {
t.Logf("Line %2d: %s", y, line)
}
}
// Check for Processes bottom border
processesBottomFound := false
for y := 0; y < 40; y++ {
for x := 0; x < 80; x++ {
cell := buf.Get(x, y)
if cell.Rune == BorderSingle.BottomLeft && cell.Style.FG == BrightBlue {
if x+1 < 80 {
nextCell := buf.Get(x+1, y)
if nextCell.Rune == BorderSingle.Horizontal {
processesBottomFound = true
t.Logf("Found Processes bottom border at y=%d", y)
}
}
}
}
}
if !processesBottomFound {
t.Error("Processes box bottom border not found - this is the bug we're debugging!")
}
}
// TestHBoxWithBorderedChildren tests that borders inside HBox flex children
// are drawn correctly.
func TestHBoxWithBorderedChildren(t *testing.T) {
view := HBox.Gap(1)(
// Left panel
VBox.Grow(1)(
VBox.Border(BorderSingle).BorderFG(Cyan).Title("Stats")(
Text("Tasks: 142"),
Text("Sleeping: 138"),
),
VBox.Border(BorderRounded).BorderFG(Green).Title("Load")(
Text("1.17, 0.69, 0.85"),
),
),
// Right panel
VBox.Grow(2)(
VBox.Border(BorderSingle).BorderFG(Magenta).Title("Info")(
Text("Line 1"),
Text("Line 2"),
Text("Line 3"),
),
),
)
tmpl := Build(view)
buf := NewBuffer(60, 15)
tmpl.Execute(buf, 60, 15)
t.Log("Op geometries:")
for i, op := range tmpl.ops {
g := tmpl.geom[i]
name := ""
if op.Title != "" {
name = op.Title
} else if op.Kind == OpContainer {
if op.IsRow {
name = "Row"
} else {
name = "Col"
}
}
t.Logf(" [%d] %s: LocalX=%d LocalY=%d W=%d H=%d", i, name, g.LocalX, g.LocalY, g.W, g.H)
}
t.Log("Buffer contents:")
for y := 0; y < 15; y++ {
line := buf.GetLine(y)
t.Logf("Line %2d: %s", y, line)
}
statsBottomFound := false
loadBottomFound := false
for y := 0; y < 15; y++ {
for x := 0; x < 60; x++ {
cell := buf.Get(x, y)
if cell.Rune == BorderSingle.BottomLeft && cell.Style.FG == Cyan {
statsBottomFound = true
t.Logf("Found Stats bottom border at y=%d", y)
}
if cell.Rune == BorderRounded.BottomLeft && cell.Style.FG == Green {
loadBottomFound = true
t.Logf("Found Load bottom border at y=%d", y)
}
}
}
if !statsBottomFound {
t.Error("Stats box bottom border not found!")
}
if !loadBottomFound {
t.Error("Load box bottom border not found!")
}
}