gitstack

grindlemire/go-tui code browser

12.5 KB Go 545 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
package tui

import (
	"testing"
)

func TestNewBuffer(t *testing.T) {
	type tc struct {
		width  int
		height int
	}

	tests := map[string]tc{
		"standard size": {
			width:  80,
			height: 24,
		},
		"small size": {
			width:  10,
			height: 5,
		},
		"single cell": {
			width:  1,
			height: 1,
		},
		"zero width": {
			width:  0,
			height: 10,
		},
		"zero height": {
			width:  10,
			height: 0,
		},
		"negative dimensions": {
			width:  -5,
			height: -3,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			b := NewBuffer(tt.width, tt.height)

			expectedWidth := max(tt.width, 0)
			expectedHeight := max(tt.height, 0)

			if b.Width() != expectedWidth {
				t.Errorf("Width() = %d, want %d", b.Width(), expectedWidth)
			}
			if b.Height() != expectedHeight {
				t.Errorf("Height() = %d, want %d", b.Height(), expectedHeight)
			}

			w, h := b.Size()
			if w != expectedWidth || h != expectedHeight {
				t.Errorf("Size() = (%d, %d), want (%d, %d)", w, h, expectedWidth, expectedHeight)
			}

			rect := b.Rect()
			if rect.X != 0 || rect.Y != 0 || rect.Width != expectedWidth || rect.Height != expectedHeight {
				t.Errorf("Rect() = %+v, want {0, 0, %d, %d}", rect, expectedWidth, expectedHeight)
			}
		})
	}
}

func TestBuffer_InitializedWithSpaces(t *testing.T) {
	b := NewBuffer(5, 3)
	defaultStyle := NewStyle()

	for y := range 3 {
		for x := range 5 {
			cell := b.Cell(x, y)
			if cell.Text != " " {
				t.Errorf("Cell(%d, %d).Rune = %q, want ' '", x, y, cell.Text)
			}
			if !cell.Style.Equal(defaultStyle) {
				t.Errorf("Cell(%d, %d) has non-default style", x, y)
			}
			if cell.Width != 1 {
				t.Errorf("Cell(%d, %d).Width = %d, want 1", x, y, cell.Width)
			}
		}
	}
}

func TestBuffer_SetCell_GetCell(t *testing.T) {
	type tc struct {
		x, y     int
		cell     Cell
		expected Cell // what we expect to get back (empty if out of bounds)
	}

	b := NewBuffer(5, 3)
	style := NewStyle().Foreground(Red)

	tests := map[string]tc{
		"in bounds": {
			x:        2,
			y:        1,
			cell:     NewCell('A', style),
			expected: NewCell('A', style),
		},
		"top-left corner": {
			x:        0,
			y:        0,
			cell:     NewCell('B', style),
			expected: NewCell('B', style),
		},
		"bottom-right corner": {
			x:        4,
			y:        2,
			cell:     NewCell('C', style),
			expected: NewCell('C', style),
		},
		"negative x": {
			x:        -1,
			y:        1,
			cell:     NewCell('X', style),
			expected: Cell{}, // out of bounds returns empty
		},
		"negative y": {
			x:        1,
			y:        -1,
			cell:     NewCell('Y', style),
			expected: Cell{}, // out of bounds returns empty
		},
		"x out of bounds": {
			x:        5,
			y:        1,
			cell:     NewCell('Z', style),
			expected: Cell{}, // out of bounds returns empty
		},
		"y out of bounds": {
			x:        1,
			y:        3,
			cell:     NewCell('W', style),
			expected: Cell{}, // out of bounds returns empty
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			// Reset buffer for each test
			b = NewBuffer(5, 3)

			b.SetCell(tt.x, tt.y, tt.cell)
			got := b.Cell(tt.x, tt.y)

			if !got.Equal(tt.expected) {
				t.Errorf("Cell(%d, %d) = %+v, want %+v", tt.x, tt.y, got, tt.expected)
			}
		})
	}
}

func TestBuffer_Fill(t *testing.T) {
	b := NewBuffer(10, 5)
	style := NewStyle().Foreground(Green)

	rect := NewRect(2, 1, 4, 2)
	b.Fill(rect, '#', style)

	// Check filled area
	for y := 1; y <= 2; y++ {
		for x := 2; x <= 5; x++ {
			cell := b.Cell(x, y)
			if cell.Text != "#" {
				t.Errorf("Cell(%d, %d).Rune = %q, want '#'", x, y, cell.Text)
			}
			if !cell.Style.Equal(style) {
				t.Errorf("Cell(%d, %d) has wrong style", x, y)
			}
		}
	}

	// Check unfilled area (outside rect)
	if b.Cell(1, 1).Text != " " {
		t.Error("Cell outside fill rect should be unchanged")
	}
	if b.Cell(6, 1).Text != " " {
		t.Error("Cell outside fill rect should be unchanged")
	}
}

func TestBuffer_Fill_WideChar(t *testing.T) {
	b := NewBuffer(10, 3)
	style := NewStyle()

	// Fill with a wide character
	rect := NewRect(0, 0, 6, 1)
	b.Fill(rect, '好', style)

	// Should have 3 wide chars (each taking 2 columns)
	for i := range 3 {
		x := i * 2
		if b.Cell(x, 0).Text != "好" {
			t.Errorf("Cell(%d, 0).Rune = %q, want '好'", x, b.Cell(x, 0).Text)
		}
		if !b.Cell(x+1, 0).IsContinuation() {
			t.Errorf("Cell(%d, 0) should be continuation", x+1)
		}
	}
}

func TestBuffer_Fill_ClipsToBuffer(t *testing.T) {
	b := NewBuffer(5, 3)
	style := NewStyle()

	// Fill rect that extends beyond buffer
	rect := NewRect(-1, -1, 10, 10)
	b.Fill(rect, 'X', style)

	// All cells should be filled
	for y := range 3 {
		for x := range 5 {
			if b.Cell(x, y).Text != "X" {
				t.Errorf("Cell(%d, %d).Rune = %q, want 'X'", x, y, b.Cell(x, y).Text)
			}
		}
	}
}

func TestBuffer_Clear(t *testing.T) {
	b := NewBuffer(5, 3)
	style := NewStyle().Bold().Foreground(Red)

	// Fill with styled content
	for y := range 3 {
		for x := range 5 {
			b.SetRune(x, y, 'X', style)
		}
	}

	// Clear
	b.Clear()

	// All cells should be space with default style
	defaultStyle := NewStyle()
	for y := range 3 {
		for x := range 5 {
			cell := b.Cell(x, y)
			if cell.Text != " " {
				t.Errorf("Cell(%d, %d).Rune = %q, want ' '", x, y, cell.Text)
			}
			if !cell.Style.Equal(defaultStyle) {
				t.Errorf("Cell(%d, %d) should have default style", x, y)
			}
		}
	}
}

func TestBuffer_ClearRect(t *testing.T) {
	b := NewBuffer(10, 5)
	style := NewStyle().Bold()

	// Fill entire buffer
	b.Fill(b.Rect(), 'X', style)

	// Clear a portion
	rect := NewRect(2, 1, 3, 2)
	b.ClearRect(rect)

	// Check cleared area
	defaultStyle := NewStyle()
	for y := 1; y <= 2; y++ {
		for x := 2; x <= 4; x++ {
			cell := b.Cell(x, y)
			if cell.Text != " " {
				t.Errorf("Cell(%d, %d).Rune = %q, want ' ' (cleared)", x, y, cell.Text)
			}
			if !cell.Style.Equal(defaultStyle) {
				t.Errorf("Cell(%d, %d) should have default style", x, y)
			}
		}
	}

	// Check non-cleared area
	if b.Cell(1, 1).Text != "X" {
		t.Error("Cell outside clear rect should be unchanged")
	}
	if b.Cell(5, 1).Text != "X" {
		t.Error("Cell outside clear rect should be unchanged")
	}
}

func TestBuffer_ClearRect_ClearsWideCharEdges(t *testing.T) {
	b := NewBuffer(10, 3)
	style := NewStyle()

	// Place wide chars at positions 1-2 and 4-5
	b.SetRune(1, 0, '好', style)
	b.SetRune(4, 0, '你', style)

	// Clear rect starting at continuation (2) and ending at wide char start (4)
	rect := NewRect(2, 0, 3, 1) // clears columns 2, 3, 4
	b.ClearRect(rect)

	// Position 1 should be cleared (was start of wide char, continuation was in clear zone)
	if b.Cell(1, 0).Text != " " {
		t.Errorf("Cell(1, 0).Rune = %q, want ' ' (wide char cleared)", b.Cell(1, 0).Text)
	}

	// Position 5 should be cleared (was continuation, start was in clear zone)
	if b.Cell(5, 0).Text != " " {
		t.Errorf("Cell(5, 0).Rune = %q, want ' ' (continuation cleared)", b.Cell(5, 0).Text)
	}
}

func TestBuffer_Resize_Grow(t *testing.T) {
	b := NewBuffer(3, 2)
	style := NewStyle()

	// Set some content
	b.SetRune(0, 0, 'A', style)
	b.SetRune(2, 1, 'B', style)

	// Grow
	b.Resize(5, 4)

	if b.Width() != 5 || b.Height() != 4 {
		t.Errorf("Size = (%d, %d), want (5, 4)", b.Width(), b.Height())
	}

	// Original content should be preserved
	if b.Cell(0, 0).Text != "A" {
		t.Errorf("Cell(0, 0).Rune = %q, want 'A'", b.Cell(0, 0).Text)
	}
	if b.Cell(2, 1).Text != "B" {
		t.Errorf("Cell(2, 1).Rune = %q, want 'B'", b.Cell(2, 1).Text)
	}

	// New area should be spaces
	if b.Cell(4, 3).Text != " " {
		t.Errorf("Cell(4, 3).Rune = %q, want ' '", b.Cell(4, 3).Text)
	}
}

func TestBuffer_Resize_Shrink(t *testing.T) {
	b := NewBuffer(5, 4)
	style := NewStyle()

	// Set content including outside new bounds
	b.SetRune(0, 0, 'A', style)
	b.SetRune(4, 3, 'Z', style)
	b.SetRune(2, 1, 'M', style)

	// Shrink
	b.Resize(3, 2)

	if b.Width() != 3 || b.Height() != 2 {
		t.Errorf("Size = (%d, %d), want (3, 2)", b.Width(), b.Height())
	}

	// Content within new bounds preserved
	if b.Cell(0, 0).Text != "A" {
		t.Errorf("Cell(0, 0).Rune = %q, want 'A'", b.Cell(0, 0).Text)
	}
	if b.Cell(2, 1).Text != "M" {
		t.Errorf("Cell(2, 1).Rune = %q, want 'M'", b.Cell(2, 1).Text)
	}

	// Old position (4, 3) is now out of bounds
	if b.Cell(4, 3).Text != "" {
		t.Error("Cell outside new bounds should return empty")
	}
}

func TestBuffer_Resize_SameSize(t *testing.T) {
	b := NewBuffer(5, 3)
	style := NewStyle()

	b.SetRune(2, 1, 'X', style)

	// Resize to same size - should be no-op
	b.Resize(5, 3)

	if b.Width() != 5 || b.Height() != 3 {
		t.Errorf("Size changed unexpectedly")
	}
	if b.Cell(2, 1).Text != "X" {
		t.Errorf("Content changed unexpectedly")
	}
}

func TestBuffer_Resize_PreservesFrontBuffer(t *testing.T) {
	b := NewBuffer(3, 2)
	style := NewStyle()

	// Make changes and swap
	b.SetRune(0, 0, 'A', style)
	b.Swap()

	// Make more changes (not swapped)
	b.SetRune(1, 0, 'B', style)

	// Resize
	b.Resize(4, 3)

	// Front buffer content should be preserved
	// After resize, Diff should still show 'B' as the only change
	b.SetRune(0, 0, 'A', style) // Reset to match front buffer

	changes := b.Diff()
	// Should have change for 'B' at (1,0) since it wasn't swapped
	found := false
	for _, c := range changes {
		if c.X == 1 && c.Y == 0 && c.Cell.Text == "B" {
			found = true
		}
	}
	if !found {
		t.Error("Resize didn't preserve pending changes")
	}
}

func TestBuffer_SetStringGradient(t *testing.T) {
	type tc struct {
		text     string
		gradient Gradient
		wantLen  int
	}

	tests := map[string]tc{
		"simple gradient": {
			text:     "Hello",
			gradient: NewGradient(Red, Blue),
			wantLen:  5,
		},
		"single char": {
			text:     "A",
			gradient: NewGradient(Red, Blue),
			wantLen:  1,
		},
		"empty string": {
			text:     "",
			gradient: NewGradient(Red, Blue),
			wantLen:  0,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			buf := NewBuffer(20, 5)
			baseStyle := NewStyle()
			got := buf.SetStringGradient(0, 0, tt.text, tt.gradient, baseStyle)
			if got != tt.wantLen {
				t.Errorf("SetStringGradient() = %d, want %d", got, tt.wantLen)
			}
			// Verify first character has start color
			if len(tt.text) > 0 {
				cell := buf.Cell(0, 0)
				if cell.Text != tt.text[:1] {
					t.Errorf("First cell rune = %s, want %s", cell.Text, tt.text[:1])
				}
			}
		})
	}
}

func TestBuffer_ApplyDim(t *testing.T) {
	buf := NewBuffer(3, 2)
	buf.SetRune(0, 0, 'A', NewStyle().Bold())
	buf.SetRune(1, 0, 'B', NewStyle())

	buf.ApplyDim()

	cell := buf.Cell(0, 0)
	if !cell.Style.HasAttr(AttrDim) {
		t.Error("expected cell to have dim attribute after ApplyDim")
	}
	if !cell.Style.HasAttr(AttrBold) {
		t.Error("expected cell to retain bold attribute after ApplyDim")
	}
	if cell.Text != "A" {
		t.Errorf("expected rune 'A', got %q", cell.Text)
	}
}

func TestBuffer_FillBlank(t *testing.T) {
	buf := NewBuffer(3, 2)
	buf.SetRune(0, 0, 'A', NewStyle().Bold())

	buf.FillBlank()

	cell := buf.Cell(0, 0)
	if cell.Text != " " {
		t.Errorf("expected space after FillBlank, got %q", cell.Text)
	}
	if cell.Style != NewStyle() {
		t.Error("expected default style after FillBlank")
	}
}

func TestBuffer_FillGradient(t *testing.T) {
	type tc struct {
		rect     Rect
		gradient Gradient
	}

	tests := map[string]tc{
		"horizontal gradient": {
			rect:     NewRect(0, 0, 10, 5),
			gradient: NewGradient(Red, Blue).WithDirection(GradientHorizontal),
		},
		"vertical gradient": {
			rect:     NewRect(0, 0, 10, 5),
			gradient: NewGradient(Red, Blue).WithDirection(GradientVertical),
		},
		"diagonal down": {
			rect:     NewRect(0, 0, 10, 5),
			gradient: NewGradient(Red, Blue).WithDirection(GradientDiagonalDown),
		},
		"diagonal up": {
			rect:     NewRect(0, 0, 10, 5),
			gradient: NewGradient(Red, Blue).WithDirection(GradientDiagonalUp),
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			buf := NewBuffer(20, 10)
			baseStyle := NewStyle()
			buf.FillGradient(tt.rect, ' ', tt.gradient, baseStyle)
			// Verify that cells have gradient colors applied
			cell := buf.Cell(tt.rect.X, tt.rect.Y)
			if cell.Style.Bg.IsDefault() {
				t.Error("FillGradient should set background color")
			}
		})
	}
}

func TestSetRuneLink(t *testing.T) {
	buf := NewBuffer(4, 1)
	buf.SetRuneLink(0, 0, 'a', NewStyle(), "https://example.com")
	if got := buf.Cell(0, 0); got.Text != "a" || got.Link != "https://example.com" {
		t.Errorf("got rune=%q link=%q, want 'a' / the URL", got.Text, got.Link)
	}
	// Empty link leaves Link clear (and still writes the rune).
	buf.SetRuneLink(1, 0, 'b', NewStyle(), "")
	if got := buf.Cell(1, 0); got.Text != "b" || got.Link != "" {
		t.Errorf("got rune=%q link=%q, want 'b' / empty", got.Text, got.Link)
	}
}