gitstack

grindlemire/go-tui code browser

7.9 KB Go 337 lines 2026-06-17 · 7d3f10d raw
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package tui

import (
	"testing"
)

// TestIntegration_BasicFlow tests the complete flow: New → AddChild → Render
func TestIntegration_BasicFlow(t *testing.T) {
	// Create root element
	root := New(
		WithSize(80, 24),
		WithDirection(Column),
	)

	// Add a child panel
	panel := New(
		WithSize(40, 10),
		WithBorder(BorderSingle),
	)

	root.AddChild(panel)

	// Render to buffer
	buf := NewBuffer(80, 24)
	root.Render(buf, 80, 24)

	// Verify layout was calculated
	panelRect := panel.Rect()
	if panelRect.Width != 40 {
		t.Errorf("panel.Rect().Width = %d, want 40", panelRect.Width)
	}
	if panelRect.Height != 10 {
		t.Errorf("panel.Rect().Height = %d, want 10", panelRect.Height)
	}

	// Verify border was rendered (check top-left corner)
	cell := buf.Cell(panelRect.X, panelRect.Y)
	if cell.Text != "┌" {
		t.Errorf("top-left cell = %q, want '┌'", cell.Text)
	}
}

// TestIntegration_NestedLayouts tests nested layouts with alternating directions
func TestIntegration_NestedLayouts(t *testing.T) {
	// Column layout
	//   Row layout (fills width, fixed height)
	//     Left panel (fixed width)
	//     Right panel (flex grow)
	//   Bottom panel (flex grow)

	root := New(
		WithSize(100, 50),
		WithDirection(Column),
	)

	topRow := New(
		WithHeight(20),
		WithDisplay(DisplayFlex), WithDirection(Row),
	)

	leftPanel := New(
		WithWidth(30),
	)

	rightPanel := New(
		WithFlexGrow(1),
	)

	bottomPanel := New(
		WithFlexGrow(1),
	)

	topRow.AddChild(leftPanel, rightPanel)
	root.AddChild(topRow, bottomPanel)

	// Render
	buf := NewBuffer(100, 50)
	root.Render(buf, 100, 50)

	// Verify topRow
	topRect := topRow.Rect()
	if topRect.Y != 0 {
		t.Errorf("topRow.Y = %d, want 0", topRect.Y)
	}
	if topRect.Height != 20 {
		t.Errorf("topRow.Height = %d, want 20", topRect.Height)
	}
	if topRect.Width != 100 {
		t.Errorf("topRow.Width = %d, want 100", topRect.Width)
	}

	// Verify leftPanel
	leftRect := leftPanel.Rect()
	if leftRect.X != 0 {
		t.Errorf("leftPanel.X = %d, want 0", leftRect.X)
	}
	if leftRect.Width != 30 {
		t.Errorf("leftPanel.Width = %d, want 30", leftRect.Width)
	}
	if leftRect.Height != 20 {
		t.Errorf("leftPanel.Height = %d, want 20 (stretched)", leftRect.Height)
	}

	// Verify rightPanel (should fill remaining: 100 - 30 = 70)
	rightRect := rightPanel.Rect()
	if rightRect.X != 30 {
		t.Errorf("rightPanel.X = %d, want 30", rightRect.X)
	}
	if rightRect.Width != 70 {
		t.Errorf("rightPanel.Width = %d, want 70", rightRect.Width)
	}

	// Verify bottomPanel (should fill remaining: 50 - 20 = 30)
	bottomRect := bottomPanel.Rect()
	if bottomRect.Y != 20 {
		t.Errorf("bottomPanel.Y = %d, want 20", bottomRect.Y)
	}
	if bottomRect.Height != 30 {
		t.Errorf("bottomPanel.Height = %d, want 30", bottomRect.Height)
	}
	if bottomRect.Width != 100 {
		t.Errorf("bottomPanel.Width = %d, want 100", bottomRect.Width)
	}
}

// TestIntegration_FlexGrowShrink tests flex grow and shrink behavior
func TestIntegration_FlexGrowShrink(t *testing.T) {
	type tc struct {
		children []struct {
			width        int
			grow, shrink float64
		}
		parentWidth   int
		expectedSizes []int
	}

	tests := map[string]tc{
		"equal grow": {
			children: []struct {
				width        int
				grow, shrink float64
			}{
				{0, 1, 1},
				{0, 1, 1},
			},
			parentWidth:   100,
			expectedSizes: []int{50, 50},
		},
		"unequal grow": {
			children: []struct {
				width        int
				grow, shrink float64
			}{
				{0, 1, 1},
				{0, 2, 1},
			},
			parentWidth:   90,
			expectedSizes: []int{30, 60},
		},
		"fixed and grow": {
			children: []struct {
				width        int
				grow, shrink float64
			}{
				{30, 0, 1},
				{0, 1, 1},
			},
			parentWidth:   100,
			expectedSizes: []int{30, 70},
		},
		"no shrink overflow": {
			children: []struct {
				width        int
				grow, shrink float64
			}{
				{60, 0, 0},
				{60, 0, 0},
			},
			parentWidth:   100,
			expectedSizes: []int{60, 60}, // No shrink, overflow allowed
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			root := New(
				WithWidth(tt.parentWidth),
				WithHeight(50),
				WithDisplay(DisplayFlex), WithDirection(Row),
			)

			children := make([]*Element, len(tt.children))
			for i, c := range tt.children {
				opts := []Option{
					WithFlexGrow(c.grow),
					WithFlexShrink(c.shrink),
				}
				if c.width > 0 {
					opts = append(opts, WithWidth(c.width))
				}
				children[i] = New(opts...)
				root.AddChild(children[i])
			}

			buf := NewBuffer(tt.parentWidth, 50)
			root.Render(buf, tt.parentWidth, 50)

			for i, child := range children {
				if child.Rect().Width != tt.expectedSizes[i] {
					t.Errorf("child[%d].Width = %d, want %d",
						i, child.Rect().Width, tt.expectedSizes[i])
				}
			}
		})
	}
}

// TestIntegration_MixedElementAndText tests a tree with both Element and Text nodes
func TestIntegration_MixedElementAndText(t *testing.T) {
	root := New(
		WithSize(80, 24),
		WithDirection(Column),
		WithJustify(JustifyCenter),
		WithAlign(AlignCenter),
	)

	// Panel with border
	panel := New(
		WithSize(40, 10),
		WithBorder(BorderRounded),
		WithDirection(Column),
		WithPadding(1),
		WithJustify(JustifyCenter),
		WithAlign(AlignCenter),
	)

	// Text element inside panel using new WithText API
	// WithText sets intrinsic width to text width and height to 1
	title := New(
		WithText("Hello World"),
		WithTextStyle(NewStyle().Bold()),
		WithTextAlign(TextAlignCenter),
		WithSize(38, 1), // Override intrinsic size for centering to work
	)

	panel.AddChild(title)
	root.AddChild(panel)

	// Render elements first (for layout and borders)
	// Text is now rendered automatically as part of RenderTree
	buf := NewBuffer(80, 24)
	root.Render(buf, 80, 24)

	// Verify panel is centered in root
	panelRect := panel.Rect()
	expectedX := (80 - 40) / 2
	expectedY := (24 - 10) / 2
	if panelRect.X != expectedX {
		t.Errorf("panel.X = %d, want %d (centered)", panelRect.X, expectedX)
	}
	if panelRect.Y != expectedY {
		t.Errorf("panel.Y = %d, want %d (centered)", panelRect.Y, expectedY)
	}

	// Check border was drawn
	topLeft := buf.Cell(panelRect.X, panelRect.Y)
	if topLeft.Text != "╭" {
		t.Errorf("border top-left = %q, want '╭'", topLeft.Text)
	}

	// Verify text was rendered
	// Text element's ContentRect determines where text is drawn
	contentRect := title.ContentRect()

	// Find where 'H' appears (should be in content area)
	found := false
	foundX := -1
	for y := contentRect.Y; y < contentRect.Bottom(); y++ {
		for x := contentRect.X; x < contentRect.Right(); x++ {
			if buf.Cell(x, y).Text == "H" {
				found = true
				foundX = x
				break
			}
		}
		if found {
			break
		}
	}
	if !found {
		t.Error("text 'Hello World' not rendered (could not find 'H' in content area)")
	}

	// Verify the text is centered
	textWidth := stringWidth("Hello World")
	expectedTextX := max(contentRect.X+(contentRect.Width-textWidth)/2, contentRect.X)
	if foundX != expectedTextX {
		t.Errorf("text 'H' at x=%d, want %d (centered)", foundX, expectedTextX)
	}
}

// TestIntegration_BackgroundAndBorder tests visual rendering
func TestIntegration_BackgroundAndBorder(t *testing.T) {
	bg := NewStyle().Background(Blue)
	border := NewStyle().Foreground(Red)

	panel := New(
		WithSize(10, 5),
		WithBorder(BorderSingle),
		WithBorderStyle(border),
		WithBackground(bg),
	)

	buf := NewBuffer(20, 10)
	panel.Calculate(20, 10)
	RenderTree(buf, panel)

	// Check background fill (interior, not border)
	// Border takes 1 cell on each side, so interior starts at (1, 1)
	interiorX := panel.Rect().X + 1
	interiorY := panel.Rect().Y + 1

	interiorCell := buf.Cell(interiorX, interiorY)
	// Background should be space with blue background
	if interiorCell.Text != " " {
		t.Errorf("interior cell rune = %q, want ' '", interiorCell.Text)
	}

	// Check border style (red foreground)
	borderCell := buf.Cell(panel.Rect().X, panel.Rect().Y)
	if borderCell.Text != "┌" {
		t.Errorf("border cell = %q, want '┌'", borderCell.Text)
	}
	if borderCell.Style.Fg != Red {
		t.Errorf("border foreground = %d, want %d (Red)", borderCell.Style.Fg, Red)
	}
}