gitstack

grindlemire/go-tui code browser

14.1 KB Go 570 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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package tui

import (
	"testing"
)

func TestBorderStyle_Chars_Single(t *testing.T) {
	chars := BorderSingle.Chars()

	expected := BorderChars{
		TopLeft:     '┌',
		Top:         '─',
		TopRight:    '┐',
		Left:        '│',
		Right:       '│',
		BottomLeft:  '└',
		Bottom:      '─',
		BottomRight: '┘',
	}

	if chars != expected {
		t.Errorf("BorderSingle.Chars() = %+v, want %+v", chars, expected)
	}
}

func TestBorderStyle_Chars_Double(t *testing.T) {
	chars := BorderDouble.Chars()

	expected := BorderChars{
		TopLeft:     '╔',
		Top:         '═',
		TopRight:    '╗',
		Left:        '║',
		Right:       '║',
		BottomLeft:  '╚',
		Bottom:      '═',
		BottomRight: '╝',
	}

	if chars != expected {
		t.Errorf("BorderDouble.Chars() = %+v, want %+v", chars, expected)
	}
}

func TestBorderStyle_Chars_Rounded(t *testing.T) {
	chars := BorderRounded.Chars()

	expected := BorderChars{
		TopLeft:     '╭',
		Top:         '─',
		TopRight:    '╮',
		Left:        '│',
		Right:       '│',
		BottomLeft:  '╰',
		Bottom:      '─',
		BottomRight: '╯',
	}

	if chars != expected {
		t.Errorf("BorderRounded.Chars() = %+v, want %+v", chars, expected)
	}
}

func TestBorderStyle_Chars_Thick(t *testing.T) {
	chars := BorderThick.Chars()

	expected := BorderChars{
		TopLeft:     '┏',
		Top:         '━',
		TopRight:    '┓',
		Left:        '┃',
		Right:       '┃',
		BottomLeft:  '┗',
		Bottom:      '━',
		BottomRight: '┛',
	}

	if chars != expected {
		t.Errorf("BorderThick.Chars() = %+v, want %+v", chars, expected)
	}
}

func TestBorderStyle_Chars_None(t *testing.T) {
	chars := BorderNone.Chars()

	// All characters should be spaces
	expected := BorderChars{
		TopLeft:     ' ',
		Top:         ' ',
		TopRight:    ' ',
		Left:        ' ',
		Right:       ' ',
		BottomLeft:  ' ',
		Bottom:      ' ',
		BottomRight: ' ',
	}

	if chars != expected {
		t.Errorf("BorderNone.Chars() = %+v, want %+v", chars, expected)
	}
}

func TestDrawBox_SingleBorder(t *testing.T) {
	buf := NewBuffer(10, 5)
	style := NewStyle()

	DrawBox(buf, NewRect(1, 1, 5, 3), BorderSingle, style)

	// Check corners
	if buf.Cell(1, 1).Text != "┌" {
		t.Errorf("TopLeft = %q, want '┌'", buf.Cell(1, 1).Text)
	}
	if buf.Cell(5, 1).Text != "┐" {
		t.Errorf("TopRight = %q, want '┐'", buf.Cell(5, 1).Text)
	}
	if buf.Cell(1, 3).Text != "└" {
		t.Errorf("BottomLeft = %q, want '└'", buf.Cell(1, 3).Text)
	}
	if buf.Cell(5, 3).Text != "┘" {
		t.Errorf("BottomRight = %q, want '┘'", buf.Cell(5, 3).Text)
	}

	// Check top edge
	for x := 2; x <= 4; x++ {
		if buf.Cell(x, 1).Text != "─" {
			t.Errorf("Top edge at %d = %q, want '─'", x, buf.Cell(x, 1).Text)
		}
	}

	// Check bottom edge
	for x := 2; x <= 4; x++ {
		if buf.Cell(x, 3).Text != "─" {
			t.Errorf("Bottom edge at %d = %q, want '─'", x, buf.Cell(x, 3).Text)
		}
	}

	// Check left edge
	if buf.Cell(1, 2).Text != "│" {
		t.Errorf("Left edge = %q, want '│'", buf.Cell(1, 2).Text)
	}

	// Check right edge
	if buf.Cell(5, 2).Text != "│" {
		t.Errorf("Right edge = %q, want '│'", buf.Cell(5, 2).Text)
	}

	// Check interior is untouched (still spaces)
	for x := 2; x <= 4; x++ {
		if buf.Cell(x, 2).Text != " " {
			t.Errorf("Interior at (%d, 2) = %q, want ' '", x, buf.Cell(x, 2).Text)
		}
	}
}

func TestDrawBox_MinimalSize(t *testing.T) {
	buf := NewBuffer(10, 5)
	style := NewStyle()

	// Minimal 2x2 box
	DrawBox(buf, NewRect(1, 1, 2, 2), BorderSingle, style)

	// Should draw just corners
	if buf.Cell(1, 1).Text != "┌" {
		t.Errorf("TopLeft = %q, want '┌'", buf.Cell(1, 1).Text)
	}
	if buf.Cell(2, 1).Text != "┐" {
		t.Errorf("TopRight = %q, want '┐'", buf.Cell(2, 1).Text)
	}
	if buf.Cell(1, 2).Text != "└" {
		t.Errorf("BottomLeft = %q, want '└'", buf.Cell(1, 2).Text)
	}
	if buf.Cell(2, 2).Text != "┘" {
		t.Errorf("BottomRight = %q, want '┘'", buf.Cell(2, 2).Text)
	}
}

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

	tests := map[string]tc{
		"1x1": {width: 1, height: 1},
		"1x5": {width: 1, height: 5},
		"5x1": {width: 5, height: 1},
		"0x0": {width: 0, height: 0},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			buf := NewBuffer(10, 10)
			style := NewStyle()

			// Mark a cell to verify nothing was drawn
			buf.SetRune(0, 0, 'X', style)

			DrawBox(buf, NewRect(0, 0, tt.width, tt.height), BorderSingle, style)

			// The 'X' should still be there (nothing drawn)
			if buf.Cell(0, 0).Text != "X" {
				t.Error("DrawBox should do nothing for rect smaller than 2x2")
			}
		})
	}
}

func TestDrawBox_BorderNone(t *testing.T) {
	buf := NewBuffer(10, 5)
	style := NewStyle()

	// Mark a cell to verify nothing was drawn
	buf.SetRune(1, 1, 'X', style)

	DrawBox(buf, NewRect(1, 1, 5, 3), BorderNone, style)

	// The 'X' should still be there
	if buf.Cell(1, 1).Text != "X" {
		t.Error("DrawBox with BorderNone should do nothing")
	}
}

func TestDrawBox_WithStyle(t *testing.T) {
	buf := NewBuffer(10, 5)
	style := NewStyle().Foreground(Red).Bold()

	DrawBox(buf, NewRect(1, 1, 5, 3), BorderSingle, style)

	// Check that corners have the style
	cell := buf.Cell(1, 1)
	if !cell.Style.HasAttr(AttrBold) {
		t.Error("Border should have bold style")
	}
	if !cell.Style.Fg.Equal(Red) {
		t.Error("Border should have red foreground")
	}
}

func TestDrawBox_ClipsToBuffer(t *testing.T) {
	buf := NewBuffer(10, 10)
	style := NewStyle()

	// Draw a box that extends beyond buffer on the right and bottom
	// Box from (5,5) with size 10x10 will clip to buffer bounds (10x10)
	// So visible portion is from (5,5) to (9,9) = 5x5 visible
	DrawBox(buf, NewRect(5, 5, 10, 10), BorderSingle, style)

	// Only the visible portion should be drawn
	// Top-left corner of the box at (5,5)
	if buf.Cell(5, 5).Text != "┌" {
		t.Errorf("Visible top-left corner should be drawn, got %q", buf.Cell(5, 5).Text)
	}
	// Top edge
	if buf.Cell(6, 5).Text != "─" {
		t.Errorf("Visible top edge should be drawn, got %q", buf.Cell(6, 5).Text)
	}
	// Left edge
	if buf.Cell(5, 6).Text != "│" {
		t.Errorf("Visible left edge should be drawn, got %q", buf.Cell(5, 6).Text)
	}
	// The right and bottom edges are clipped but should still draw what's visible
	// Right edge at x=9 (buffer boundary) - clipped to visible part
	if buf.Cell(9, 5).Text != "─" && buf.Cell(9, 5).Text != "┐" {
		t.Errorf("Right boundary should have border char, got %q", buf.Cell(9, 5).Text)
	}
}

func TestDrawBoxWithTitle_Centered(t *testing.T) {
	buf := NewBuffer(20, 5)
	style := NewStyle()

	DrawBoxWithTitle(buf, NewRect(0, 0, 15, 3), BorderSingle, "Test", style)

	// Title "Test" should be centered in the top border
	// Available width = 15 - 2 = 13
	// Title width = 4
	// Start position = 1 + (13-4)/2 = 1 + 4 = 5

	// Check corners are still correct
	if buf.Cell(0, 0).Text != "┌" {
		t.Errorf("TopLeft = %q, want '┌'", buf.Cell(0, 0).Text)
	}
	if buf.Cell(14, 0).Text != "┐" {
		t.Errorf("TopRight = %q, want '┐'", buf.Cell(14, 0).Text)
	}

	// Check title is present
	title := "Test"
	startX := 1 + (13-4)/2 // = 5
	for i, r := range title {
		cell := buf.Cell(startX+i, 0)
		if cell.Text != string(r) {
			t.Errorf("Title at %d = %q, want %q", startX+i, cell.Text, r)
		}
	}
}

func TestDrawBoxWithTitle_LongTitle(t *testing.T) {
	buf := NewBuffer(10, 5)
	style := NewStyle()

	// Title longer than available space
	DrawBoxWithTitle(buf, NewRect(0, 0, 6, 3), BorderSingle, "VeryLongTitle", style)

	// Available width = 6 - 2 = 4
	// Title should be truncated to fit
	// Check that corners are intact
	if buf.Cell(0, 0).Text != "┌" {
		t.Errorf("TopLeft = %q, want '┌'", buf.Cell(0, 0).Text)
	}
	if buf.Cell(5, 0).Text != "┐" {
		t.Errorf("TopRight = %q, want '┐'", buf.Cell(5, 0).Text)
	}

	// Check that some of the title is visible
	if buf.Cell(1, 0).Text != "V" {
		t.Errorf("First title char = %q, want 'V'", buf.Cell(1, 0).Text)
	}
}

func TestDrawBoxWithTitle_EmptyTitle(t *testing.T) {
	buf := NewBuffer(10, 5)
	style := NewStyle()

	DrawBoxWithTitle(buf, NewRect(0, 0, 6, 3), BorderSingle, "", style)

	// Should just draw a normal box
	if buf.Cell(0, 0).Text != "┌" {
		t.Errorf("TopLeft = %q, want '┌'", buf.Cell(0, 0).Text)
	}

	// Top edge should be all horizontal lines (no title)
	for x := 1; x < 5; x++ {
		if buf.Cell(x, 0).Text != "─" {
			t.Errorf("Top edge at %d = %q, want '─'", x, buf.Cell(x, 0).Text)
		}
	}
}

func TestDrawBoxWithTitle_TooSmallForTitle(t *testing.T) {
	buf := NewBuffer(10, 5)
	style := NewStyle()

	// Box too small to fit any title
	DrawBoxWithTitle(buf, NewRect(0, 0, 2, 2), BorderSingle, "X", style)

	// Should still draw the box
	if buf.Cell(0, 0).Text != "┌" {
		t.Errorf("TopLeft = %q, want '┌'", buf.Cell(0, 0).Text)
	}
	if buf.Cell(1, 0).Text != "┐" {
		t.Errorf("TopRight = %q, want '┐'", buf.Cell(1, 0).Text)
	}
}

func TestDrawBoxWithTitle_WideCharTitle(t *testing.T) {
	buf := NewBuffer(20, 5)
	style := NewStyle()

	// Title with wide characters
	DrawBoxWithTitle(buf, NewRect(0, 0, 10, 3), BorderSingle, "你好", style)

	// "你好" takes 4 columns
	// Available width = 10 - 2 = 8
	// Start position = 1 + (8-4)/2 = 1 + 2 = 3

	// Check corners
	if buf.Cell(0, 0).Text != "┌" {
		t.Errorf("TopLeft = %q, want '┌'", buf.Cell(0, 0).Text)
	}

	// Check that the wide characters are present
	// They should be centered
	found := false
	for x := 1; x < 9; x++ {
		if buf.Cell(x, 0).Text == "你" {
			found = true
			// Next cell should be continuation
			if !buf.Cell(x+1, 0).IsContinuation() {
				t.Error("Wide char should have continuation")
			}
			break
		}
	}
	if !found {
		t.Error("Wide character title not found")
	}
}

func TestDrawBoxClipped(t *testing.T) {
	type tc struct {
		boxRect  Rect
		clipRect Rect
		// positions that SHOULD have border chars
		wantDrawn map[[2]int]rune
		// positions that should remain spaces (clipped away)
		wantSpace [][2]int
	}

	chars := BorderSingle.Chars()

	tests := map[string]tc{
		"fully visible": {
			boxRect:  NewRect(1, 1, 5, 3),
			clipRect: NewRect(0, 0, 10, 10),
			wantDrawn: map[[2]int]rune{
				{1, 1}: chars.TopLeft,
				{5, 1}: chars.TopRight,
				{1, 3}: chars.BottomLeft,
				{5, 3}: chars.BottomRight,
				{3, 1}: chars.Top,
				{3, 3}: chars.Bottom,
				{1, 2}: chars.Left,
				{5, 2}: chars.Right,
			},
		},
		"top clipped": {
			boxRect:  NewRect(1, 0, 5, 4),
			clipRect: NewRect(0, 1, 10, 9),
			wantDrawn: map[[2]int]rune{
				// bottom row visible
				{1, 3}: chars.BottomLeft,
				{5, 3}: chars.BottomRight,
				{3, 3}: chars.Bottom,
				// side edges visible at y=1,2
				{1, 1}: chars.Left,
				{5, 1}: chars.Right,
				{1, 2}: chars.Left,
				{5, 2}: chars.Right,
			},
			wantSpace: [][2]int{
				{1, 0}, // top-left corner clipped
				{5, 0}, // top-right corner clipped
				{3, 0}, // top edge clipped
			},
		},
		"bottom clipped": {
			boxRect:  NewRect(1, 1, 5, 4),
			clipRect: NewRect(0, 0, 10, 4),
			wantDrawn: map[[2]int]rune{
				// top row visible
				{1, 1}: chars.TopLeft,
				{5, 1}: chars.TopRight,
				{3, 1}: chars.Top,
				// side edges at y=2,3
				{1, 2}: chars.Left,
				{5, 2}: chars.Right,
				{1, 3}: chars.Left,
				{5, 3}: chars.Right,
			},
			wantSpace: [][2]int{
				{1, 4}, // bottom-left corner clipped
				{5, 4}, // bottom-right corner clipped
				{3, 4}, // bottom edge clipped
			},
		},
		"entirely outside": {
			boxRect:   NewRect(0, 0, 5, 3),
			clipRect:  NewRect(10, 10, 5, 5),
			wantDrawn: map[[2]int]rune{},
			wantSpace: [][2]int{
				{0, 0}, {4, 0}, {0, 2}, {4, 2},
			},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			buf := NewBuffer(15, 10)
			style := NewStyle()

			DrawBoxClipped(buf, tt.boxRect, BorderSingle, style, tt.clipRect)

			for pos, wantRune := range tt.wantDrawn {
				got := buf.Cell(pos[0], pos[1]).Text
				if got != string(wantRune) {
					t.Errorf("(%d,%d) = %q, want %q", pos[0], pos[1], got, wantRune)
				}
			}

			for _, pos := range tt.wantSpace {
				got := buf.Cell(pos[0], pos[1]).Text
				if got != " " {
					t.Errorf("clipped (%d,%d) = %q, want ' '", pos[0], pos[1], got)
				}
			}
		})
	}
}

func TestDrawBoxGradientClipped(t *testing.T) {
	buf := NewBuffer(15, 10)
	style := NewStyle()
	g := NewGradient(Red, Blue)

	boxRect := NewRect(1, 0, 5, 4)
	clipRect := NewRect(0, 1, 15, 9) // clip top row

	DrawBoxGradientClipped(buf, boxRect, BorderSingle, g, style, clipRect)

	chars := BorderSingle.Chars()

	// Top row (y=0) should be clipped
	if buf.Cell(1, 0).Text != " " {
		t.Errorf("clipped top-left = %q, want ' '", buf.Cell(1, 0).Text)
	}

	// Bottom row (y=3) should be drawn
	if buf.Cell(1, 3).Text != string(chars.BottomLeft) {
		t.Errorf("bottom-left = %q, want %q", buf.Cell(1, 3).Text, chars.BottomLeft)
	}
	if buf.Cell(5, 3).Text != string(chars.BottomRight) {
		t.Errorf("bottom-right = %q, want %q", buf.Cell(5, 3).Text, chars.BottomRight)
	}

	// Side edges should be drawn at visible rows
	if buf.Cell(1, 1).Text != string(chars.Left) {
		t.Errorf("left edge at y=1 = %q, want %q", buf.Cell(1, 1).Text, chars.Left)
	}
	if buf.Cell(5, 2).Text != string(chars.Right) {
		t.Errorf("right edge at y=2 = %q, want %q", buf.Cell(5, 2).Text, chars.Right)
	}

	// Verify gradient colors are non-default on visible border chars
	cell := buf.Cell(1, 3)
	if cell.Style.Fg.IsDefault() {
		t.Error("gradient border should have non-default foreground color")
	}
}

func TestFillBox(t *testing.T) {
	buf := NewBuffer(10, 5)
	style := NewStyle().Foreground(Blue)

	// Draw a box first
	DrawBox(buf, NewRect(1, 1, 6, 4), BorderSingle, style)

	// Fill the interior
	fillStyle := NewStyle().Background(Red)
	FillBox(buf, NewRect(1, 1, 6, 4), '.', fillStyle)

	// Check interior is filled
	for y := 2; y <= 3; y++ {
		for x := 2; x <= 5; x++ {
			cell := buf.Cell(x, y)
			if cell.Text != "." {
				t.Errorf("Interior at (%d, %d) = %q, want '.'", x, y, cell.Text)
			}
		}
	}

	// Check border is unchanged
	if buf.Cell(1, 1).Text != "┌" {
		t.Error("Border should be unchanged")
	}
}

func TestFillBox_TooSmall(t *testing.T) {
	buf := NewBuffer(10, 5)
	style := NewStyle()

	// Fill a box that's too small to have an interior
	buf.SetRune(0, 0, 'X', style)
	FillBox(buf, NewRect(0, 0, 2, 2), '.', style)

	// Should do nothing
	if buf.Cell(0, 0).Text != "X" {
		t.Error("FillBox should do nothing for box without interior")
	}
}