gitstack

grindlemire/go-tui code browser

10.2 KB Go 363 lines 2026-03-07 ยท 3fed657 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package layout

import "testing"

func TestFlexWrap_BasicLineBreak(t *testing.T) {
	type tc struct {
		parentWidth    int
		childWidths    []int
		childHeight    int
		expectedLines  int // number of lines (verified by Y positions)
		expectedYs     []int
		expectedXs     []int
		expectedWidths []int
	}

	tests := map[string]tc{
		"all fit on one line": {
			parentWidth:    100,
			childWidths:    []int{20, 30, 40},
			childHeight:    10,
			expectedLines:  1,
			expectedYs:     []int{0, 0, 0},
			expectedXs:     []int{0, 20, 50},
			expectedWidths: []int{20, 30, 40},
		},
		"two lines": {
			parentWidth:    60,
			childWidths:    []int{30, 30, 30},
			childHeight:    10,
			expectedLines:  2,
			expectedYs:     []int{0, 0, 10},
			expectedXs:     []int{0, 30, 0},
			expectedWidths: []int{30, 30, 30},
		},
		"three lines": {
			parentWidth:    30,
			childWidths:    []int{20, 20, 20},
			childHeight:    10,
			expectedLines:  3,
			expectedYs:     []int{0, 10, 20},
			expectedXs:     []int{0, 0, 0},
			expectedWidths: []int{20, 20, 20},
		},
		"single item per line": {
			parentWidth:    15,
			childWidths:    []int{20, 20},
			childHeight:    10,
			expectedLines:  2,
			expectedYs:     []int{0, 10},
			expectedXs:     []int{0, 0},
			expectedWidths: []int{20, 20},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			parent := newTestNode(DefaultStyle())
			parent.style.Width = Fixed(tt.parentWidth)
			parent.style.Height = Fixed(100)
			parent.style.Display = DisplayFlex
			parent.style.Direction = Row
			parent.style.FlexWrap = Wrap
			parent.style.AlignItems = AlignStart

			children := make([]*testNode, len(tt.childWidths))
			for i, w := range tt.childWidths {
				child := newTestNode(DefaultStyle())
				child.style.Width = Fixed(w)
				child.style.Height = Fixed(tt.childHeight)
				child.style.FlexShrink = 0 // Don't shrink; wrap instead
				children[i] = child
			}
			parent.AddChild(children...)

			Calculate(parent, tt.parentWidth, 100)

			for i, child := range children {
				if child.layout.Rect.X != tt.expectedXs[i] {
					t.Errorf("child[%d].X = %d, want %d", i, child.layout.Rect.X, tt.expectedXs[i])
				}
				if child.layout.Rect.Y != tt.expectedYs[i] {
					t.Errorf("child[%d].Y = %d, want %d", i, child.layout.Rect.Y, tt.expectedYs[i])
				}
				if child.layout.Rect.Width != tt.expectedWidths[i] {
					t.Errorf("child[%d].Width = %d, want %d", i, child.layout.Rect.Width, tt.expectedWidths[i])
				}
			}
		})
	}
}

func TestFlexWrap_NoWrapPreservesExistingBehavior(t *testing.T) {
	// With WrapNone (default), children that overflow should shrink, not wrap
	parent := newTestNode(DefaultStyle())
	parent.style.Width = Fixed(50)
	parent.style.Height = Fixed(50)
	parent.style.Display = DisplayFlex
	parent.style.Direction = Row

	child1 := newTestNode(DefaultStyle())
	child1.style.Width = Fixed(30)
	child1.style.Height = Fixed(10)
	child1.style.MinWidth = Fixed(0) // Allow shrink below intrinsic

	child2 := newTestNode(DefaultStyle())
	child2.style.Width = Fixed(30)
	child2.style.Height = Fixed(10)
	child2.style.MinWidth = Fixed(0)

	parent.AddChild(child1, child2)
	Calculate(parent, 50, 50)

	// Both should be on the same Y (no wrapping)
	if child1.layout.Rect.Y != 0 {
		t.Errorf("child1.Y = %d, want 0", child1.layout.Rect.Y)
	}
	if child2.layout.Rect.Y != 0 {
		t.Errorf("child2.Y = %d, want 0", child2.layout.Rect.Y)
	}
}

func TestFlexWrap_GrowWithinLine(t *testing.T) {
	// Items with flex-grow should expand to fill their line
	parent := newTestNode(DefaultStyle())
	parent.style.Width = Fixed(100)
	parent.style.Height = Fixed(100)
	parent.style.Display = DisplayFlex
	parent.style.Direction = Row
	parent.style.FlexWrap = Wrap
	parent.style.AlignItems = AlignStart

	// Line 1: 40 + grow item
	child1 := newTestNode(DefaultStyle())
	child1.style.Width = Fixed(40)
	child1.style.Height = Fixed(10)
	child1.style.FlexShrink = 0

	child2 := newTestNode(DefaultStyle())
	child2.style.Width = Fixed(40)
	child2.style.Height = Fixed(10)
	child2.style.FlexGrow = 1
	child2.style.FlexShrink = 0

	// Line 2: forces wrap (40+40+40 > 100)
	child3 := newTestNode(DefaultStyle())
	child3.style.Width = Fixed(40)
	child3.style.Height = Fixed(10)
	child3.style.FlexShrink = 0

	parent.AddChild(child1, child2, child3)
	Calculate(parent, 100, 100)

	// child2 should grow to fill remaining space on line 1 (100 - 40 = 60)
	if child2.layout.Rect.Width != 60 {
		t.Errorf("child2.Width = %d, want 60", child2.layout.Rect.Width)
	}

	// child3 should be on line 2
	if child3.layout.Rect.Y != 10 {
		t.Errorf("child3.Y = %d, want 10", child3.layout.Rect.Y)
	}
}

func TestFlexWrap_AutoCrossAxisGrowth(t *testing.T) {
	type tc struct {
		parentWidth          int
		parentHeight         int // 0 means Auto
		childWidths          []int
		childHeight          int
		expectedParentHeight int
	}

	tests := map[string]tc{
		"auto height grows to fit two lines": {
			parentWidth:          60,
			parentHeight:         0, // Auto
			childWidths:          []int{30, 30, 30},
			childHeight:          10,
			expectedParentHeight: 20, // line1=[30,30], line2=[30] => 2 lines * 10
		},
		"auto height grows to fit three lines": {
			parentWidth:          25,
			parentHeight:         0,
			childWidths:          []int{20, 20, 20},
			childHeight:          10,
			expectedParentHeight: 30, // 3 lines * 10
		},
		"fixed height does not grow": {
			parentWidth:          50,
			parentHeight:         15, // Fixed
			childWidths:          []int{30, 30, 30},
			childHeight:          10,
			expectedParentHeight: 15, // Stays at fixed 15
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			parent := newTestNode(DefaultStyle())
			parent.style.Width = Fixed(tt.parentWidth)
			if tt.parentHeight > 0 {
				parent.style.Height = Fixed(tt.parentHeight)
			}
			// Leave Height as Auto for auto-sizing
			parent.style.Display = DisplayFlex
			parent.style.Direction = Row
			parent.style.FlexWrap = Wrap
			parent.style.AlignItems = AlignStart

			children := make([]*testNode, len(tt.childWidths))
			for i, w := range tt.childWidths {
				child := newTestNode(DefaultStyle())
				child.style.Width = Fixed(w)
				child.style.Height = Fixed(tt.childHeight)
				child.style.FlexShrink = 0
				children[i] = child
			}
			parent.AddChild(children...)

			Calculate(parent, 200, 200)

			if parent.layout.Rect.Height != tt.expectedParentHeight {
				t.Errorf("parent height = %d, want %d", parent.layout.Rect.Height, tt.expectedParentHeight)
			}
		})
	}
}

func TestFlexWrap_AlignContent(t *testing.T) {
	type tc struct {
		alignContent   AlignContent
		expectedLineYs []int // Y position of first item on each line
	}

	// Setup: 50x100 parent, 3 items of width 40, height 10 each.
	// Each item alone takes a full line (40 < 50, but 40+40=80 > 50).
	// 3 lines * 10 = 30 used. 100 - 30 = 70 free cross space.

	tests := map[string]tc{
		"content-start": {
			alignContent:   ContentStart,
			expectedLineYs: []int{0, 10, 20},
		},
		"content-end": {
			alignContent:   ContentEnd,
			expectedLineYs: []int{70, 80, 90},
		},
		"content-center": {
			alignContent:   ContentCenter,
			expectedLineYs: []int{35, 45, 55},
		},
		"content-space-between": {
			alignContent:   ContentSpaceBetween,
			expectedLineYs: []int{0, 45, 90},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			parent := newTestNode(DefaultStyle())
			parent.style.Width = Fixed(50)
			parent.style.Height = Fixed(100)
			parent.style.Display = DisplayFlex
			parent.style.Direction = Row
			parent.style.FlexWrap = Wrap
			parent.style.AlignContent = tt.alignContent
			parent.style.AlignItems = AlignStart

			children := make([]*testNode, 3)
			for i := range children {
				child := newTestNode(DefaultStyle())
				child.style.Width = Fixed(40)
				child.style.Height = Fixed(10)
				child.style.FlexShrink = 0
				children[i] = child
			}
			parent.AddChild(children...)

			Calculate(parent, 200, 200)

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

func TestFlexWrap_WrapReverse(t *testing.T) {
	// WrapReverse should reverse line order on the cross axis
	parent := newTestNode(DefaultStyle())
	parent.style.Width = Fixed(50)
	parent.style.Height = Fixed(100)
	parent.style.Display = DisplayFlex
	parent.style.Direction = Row
	parent.style.FlexWrap = WrapReverse
	parent.style.AlignItems = AlignStart

	child1 := newTestNode(DefaultStyle())
	child1.style.Width = Fixed(40)
	child1.style.Height = Fixed(10)
	child1.style.FlexShrink = 0

	child2 := newTestNode(DefaultStyle())
	child2.style.Width = Fixed(40)
	child2.style.Height = Fixed(10)
	child2.style.FlexShrink = 0

	parent.AddChild(child1, child2)
	Calculate(parent, 200, 200)

	// With WrapReverse, line order is reversed:
	// Line 1 (child1) should be below line 2 (child2)
	if child2.layout.Rect.Y >= child1.layout.Rect.Y {
		t.Errorf("WrapReverse: child2.Y (%d) should be < child1.Y (%d)",
			child2.layout.Rect.Y, child1.layout.Rect.Y)
	}
}

func TestFlexWrap_ColumnDirection(t *testing.T) {
	// Flex-wrap in column direction wraps along the X axis
	parent := newTestNode(DefaultStyle())
	parent.style.Width = Fixed(100)
	parent.style.Height = Fixed(30)
	parent.style.Display = DisplayFlex
	parent.style.Direction = Column
	parent.style.FlexWrap = Wrap
	parent.style.AlignItems = AlignStart

	child1 := newTestNode(DefaultStyle())
	child1.style.Width = Fixed(20)
	child1.style.Height = Fixed(20)
	child1.style.FlexShrink = 0

	child2 := newTestNode(DefaultStyle())
	child2.style.Width = Fixed(20)
	child2.style.Height = Fixed(20)
	child2.style.FlexShrink = 0

	child3 := newTestNode(DefaultStyle())
	child3.style.Width = Fixed(20)
	child3.style.Height = Fixed(20)
	child3.style.FlexShrink = 0

	parent.AddChild(child1, child2, child3)
	Calculate(parent, 100, 30)

	// Column direction with height 30: each child is 20 tall
	// Line 1: child1 (20), can't fit child2 (20+20=40>30)
	// Line 2: child2 (20), can't fit child3
	// Line 3: child3
	// So child1.X=0, child2.X=20, child3.X=40
	if child1.layout.Rect.X != 0 {
		t.Errorf("child1.X = %d, want 0", child1.layout.Rect.X)
	}
	if child2.layout.Rect.X != 20 {
		t.Errorf("child2.X = %d, want 20", child2.layout.Rect.X)
	}
	if child3.layout.Rect.X != 40 {
		t.Errorf("child3.X = %d, want 40", child3.layout.Rect.X)
	}
}