gitstack

grindlemire/go-tui code browser

11.4 KB Go 415 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
package tui

import (
	"strings"
	"testing"
	"unicode/utf8"
)

func TestTextArea_SetText_UsesRuneCursorPosition(t *testing.T) {
	ta := NewTextArea()
	ta.BindApp(testApp)
	ta.SetText("a界")

	if got := ta.cursorPos.Get(); got != 2 {
		t.Fatalf("cursorPos = %d, want 2", got)
	}
}

func TestTextArea_Edit_MultibyteRunes(t *testing.T) {
	ta := NewTextArea()
	ta.BindApp(testApp)
	ta.SetText("a界")
	ta.cursorPos.Set(1)

	ta.insertChar(KeyEvent{Key: KeyRune, Rune: '🙂'})
	if got := ta.Text(); got != "a🙂界" {
		t.Fatalf("text after insert = %q, want %q", got, "a🙂界")
	}

	ta.backspace(KeyEvent{Key: KeyBackspace})
	if got := ta.Text(); got != "a界" {
		t.Fatalf("text after backspace = %q, want %q", got, "a界")
	}

	ta.delete(KeyEvent{Key: KeyDelete})
	if got := ta.Text(); got != "a" {
		t.Fatalf("text after delete = %q, want %q", got, "a")
	}
}

func TestTextArea_MoveRight_UsesRuneLength(t *testing.T) {
	ta := NewTextArea()
	ta.BindApp(testApp)
	ta.SetText("é界")
	ta.cursorPos.Set(0)

	ta.moveRight(KeyEvent{Key: KeyRight})
	ta.moveRight(KeyEvent{Key: KeyRight})
	ta.moveRight(KeyEvent{Key: KeyRight})

	if got := ta.cursorPos.Get(); got != 2 {
		t.Fatalf("cursorPos = %d, want 2", got)
	}
}

func TestTextArea_WrapText_DisplayWidth(t *testing.T) {
	type tc struct {
		width  int
		border BorderStyle
		text   string
		want   []string
	}

	tests := map[string]tc{
		"ascii wraps at width": {
			width: 10,
			text:  "abcdefghijklmnop",
			want:  []string{"abcdefghij", "klmnop"},
		},
		"cjk wraps at display columns not rune count": {
			width: 10,
			text:  "一二三四五六七八九十",
			want:  []string{"一二三四五", "六七八九十"},
		},
		"mixed ascii and cjk": {
			width: 10,
			text:  "ab界cd界ef界",
			want:  []string{"ab界cd界ef", "界"},
		},
		"wide char that does not fit moves to next line": {
			width: 5,
			text:  "ab界界",
			want:  []string{"ab界", "界"},
		},
		"border reduces wrap width by two": {
			width:  10,
			border: BorderSingle,
			text:   "abcdefghijklmnop",
			want:   []string{"abcdefgh", "ijklmnop"},
		},
		"embedded newlines preserved": {
			width: 10,
			text:  "一二三\n\nab",
			want:  []string{"一二三", "", "ab"},
		},
		"emoji wraps at display columns": {
			width: 5,
			text:  "🎉🎉🎉",
			want:  []string{"🎉🎉", "🎉"},
		},
		"rune wider than wrap width gets its own line": {
			width:  3,
			border: BorderSingle,
			text:   "界界",
			want:   []string{"界", "界"},
		},
		"zero width disables wrapping": {
			width: 0,
			text:  "abcdef\ngh",
			want:  []string{"abcdef", "gh"},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			opts := []TextAreaOption{WithTextAreaWidth(tt.width)}
			if tt.border != BorderNone {
				opts = append(opts, WithTextAreaBorder(tt.border))
			}
			ta := NewTextArea(opts...)
			ta.BindApp(testApp)
			ta.SetText(tt.text)

			lines := ta.wrapText()
			if len(lines) != len(tt.want) {
				t.Fatalf("wrapText() = %q, want %q", lines, tt.want)
			}
			for i := range lines {
				if lines[i] != tt.want[i] {
					t.Fatalf("wrapText()[%d] = %q, want %q", i, lines[i], tt.want[i])
				}
			}
		})
	}
}

func TestTextArea_CursorRowCol_WideChars(t *testing.T) {
	type tc struct {
		pos     int
		wantRow int
		wantCol int
	}

	// width 10, "一二三四五六七八九十" wraps to ["一二三四五", "六七八九十"].
	// Both wrapped lines are display-full, so boundary positions move to the
	// next visual line (downstream affinity); end of text lands on a phantom
	// row past the last line.
	tests := map[string]tc{
		"start of text":              {pos: 0, wantRow: 0, wantCol: 0},
		"soft wrap boundary":         {pos: 5, wantRow: 1, wantCol: 0},
		"after first rune of second": {pos: 6, wantRow: 1, wantCol: 1},
		"end of text":                {pos: 10, wantRow: 2, wantCol: 0},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			ta := NewTextArea(WithTextAreaWidth(10))
			ta.BindApp(testApp)
			ta.SetText("一二三四五六七八九十")
			ta.cursorPos.Set(tt.pos)

			row, col := ta.cursorRowCol(ta.wrapText())
			if row != tt.wantRow || col != tt.wantCol {
				t.Fatalf("cursorRowCol() = (%d, %d), want (%d, %d)", row, col, tt.wantRow, tt.wantCol)
			}
		})
	}
}

// renderedRows renders a component into a standalone buffer and returns each
// row as a plain string (continuation cells skipped, empty cells as spaces).
func renderedRows(t *testing.T, c Component, width int) []string {
	t.Helper()
	buf, height := renderElementToBuffer(c.Render(testApp), width, Capabilities{})
	if buf == nil {
		t.Fatal("renderElementToBuffer returned nil buffer")
	}
	rows := make([]string, 0, height)
	for y := range height {
		var sb strings.Builder
		for x := range width {
			cell := buf.Cell(x, y)
			if cell.IsContinuation() {
				continue
			}
			if cell.Text == "" {
				sb.WriteRune(' ')
			} else {
				sb.WriteString(cell.Text)
			}
		}
		rows = append(rows, sb.String())
	}
	return rows
}

func TestTextArea_Render_NoClippedContent(t *testing.T) {
	type tc struct {
		width  int
		border BorderStyle
		text   string
	}

	tests := map[string]tc{
		"cjk without border": {width: 10, text: "一二三四五六七八九十"},
		"cjk with border":    {width: 10, border: BorderSingle, text: "一二三四五六七八"},
		"ascii with border":  {width: 10, border: BorderSingle, text: "abcdefghijklmnop"},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			opts := []TextAreaOption{WithTextAreaWidth(tt.width)}
			if tt.border != BorderNone {
				opts = append(opts, WithTextAreaBorder(tt.border))
			}
			ta := NewTextArea(opts...)
			ta.BindApp(testApp)
			ta.SetText(tt.text)

			rendered := strings.Join(renderedRows(t, ta, tt.width), "\n")
			for _, r := range tt.text {
				if !strings.ContainsRune(rendered, r) {
					t.Errorf("rune %q clipped out of rendered output:\n%s", r, rendered)
				}
			}
		})
	}
}

func TestTextArea_CursorRowCol_WrapBoundaryAffinity(t *testing.T) {
	type tc struct {
		text    string
		width   int
		pos     int
		wantRow int
		wantCol int
	}

	tests := map[string]tc{
		"soft boundary on full line moves to next line start": {
			text: "abcdefgh", width: 4, pos: 4, wantRow: 1, wantCol: 0,
		},
		"soft boundary on non-full line stays at line end": {
			// "ab界" is 4 columns in width 5, so the cursor still fits there
			text: "ab界界", width: 5, pos: 3, wantRow: 0, wantCol: 3,
		},
		"hard newline after full line stays at line end": {
			text: "abcd\nef", width: 4, pos: 4, wantRow: 0, wantCol: 4,
		},
		"end of text on full last line moves to phantom row": {
			text: "abcd", width: 4, pos: 4, wantRow: 1, wantCol: 0,
		},
		"end of text on full cjk line moves to phantom row": {
			text: "一二", width: 4, pos: 2, wantRow: 1, wantCol: 0,
		},
		"end of text on non-full line stays at line end": {
			text: "abc", width: 4, pos: 3, wantRow: 0, wantCol: 3,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			ta := NewTextArea(WithTextAreaWidth(tt.width))
			ta.BindApp(testApp)
			ta.SetText(tt.text)
			ta.cursorPos.Set(tt.pos)

			row, col := ta.cursorRowCol(ta.wrapText())
			if row != tt.wantRow || col != tt.wantCol {
				t.Fatalf("cursorRowCol() = (%d, %d), want (%d, %d)", row, col, tt.wantRow, tt.wantCol)
			}
		})
	}
}

func TestTextArea_Move_WrapBoundary(t *testing.T) {
	type tc struct {
		text    string
		width   int
		pos     int
		move    func(*TextArea)
		wantPos int
	}

	tests := map[string]tc{
		// posFromRowCol could not resolve (row, 0) targets on soft-wrapped
		// lines, so moving down from column 0 jumped past the target line.
		"down from column zero crosses soft wrap": {
			text: "abcdef", width: 4, pos: 0,
			move:    func(ta *TextArea) { ta.moveDown(KeyEvent{Key: KeyDown}) },
			wantPos: 4,
		},
		// The cursor at a full-line soft boundary displays on the next line,
		// so moving up from there lands on the line above it.
		"up from wrap boundary lands on previous line": {
			text: "abcdef", width: 4, pos: 4,
			move:    func(ta *TextArea) { ta.moveUp(KeyEvent{Key: KeyUp}) },
			wantPos: 0,
		},
		// End with the cursor on the phantom row resolves against the last
		// real line instead of indexing past the lines slice.
		"end on phantom row stays at end of text": {
			text: "abcd", width: 4, pos: 4,
			move:    func(ta *TextArea) { ta.moveEnd(KeyEvent{Key: KeyEnd}) },
			wantPos: 4,
		},
		"home on phantom row stays at boundary": {
			text: "abcd", width: 4, pos: 4,
			move:    func(ta *TextArea) { ta.moveHome(KeyEvent{Key: KeyHome}) },
			wantPos: 4,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			ta := NewTextArea(WithTextAreaWidth(tt.width))
			ta.BindApp(testApp)
			ta.SetText(tt.text)
			ta.cursorPos.Set(tt.pos)

			tt.move(ta)
			if got := ta.cursorPos.Get(); got != tt.wantPos {
				t.Fatalf("cursorPos after move = %d, want %d", got, tt.wantPos)
			}
		})
	}
}

func TestTextArea_CursorVisibleAtWrapBoundary(t *testing.T) {
	type tc struct {
		text       string
		width      int
		pos        int
		wantHeight int
	}

	tests := map[string]tc{
		"soft boundary on full line":      {text: "abcdefgh", width: 4, pos: 4, wantHeight: 2},
		"end of text on full last line":   {text: "abcd", width: 4, pos: 4, wantHeight: 2},
		"end of text on full cjk line":    {text: "一二三四五", width: 10, pos: 5, wantHeight: 2},
		"end of text on non-full line":    {text: "abc", width: 4, pos: 3, wantHeight: 1},
		"end of text after hard newline":  {text: "abcd\n", width: 4, pos: 5, wantHeight: 2},
		"mid-line cursor away from edges": {text: "abcdef", width: 4, pos: 2, wantHeight: 2},
		// The cursor overlays the last cell when a display-full line ends in
		// a hard newline, since there is no continuation line to move it to.
		"full line before hard newline": {text: "abcd\nef", width: 4, pos: 4, wantHeight: 2},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			ta := NewTextArea(WithTextAreaWidth(tt.width))
			ta.BindApp(testApp)
			ta.SetText(tt.text)
			ta.Focus()
			ta.cursorPos.Set(tt.pos)
			ta.blink.Set(true)

			rows := renderedRows(t, ta, tt.width)
			if len(rows) != tt.wantHeight {
				t.Fatalf("rendered height = %d, want %d\n%s", len(rows), tt.wantHeight, strings.Join(rows, "\n"))
			}
			if !strings.ContainsRune(strings.Join(rows, "\n"), ta.cursorRune) {
				t.Fatalf("cursor not visible in rendered output:\n%s", strings.Join(rows, "\n"))
			}
		})
	}
}

func TestTextArea_Height_PhantomCursorRow(t *testing.T) {
	ta := NewTextArea(WithTextAreaWidth(4))
	ta.BindApp(testApp)
	ta.SetText("abcd")

	if got := ta.Height(); got != 1 {
		t.Fatalf("unfocused Height() = %d, want 1", got)
	}
	ta.Focus()
	if got := ta.Height(); got != 2 {
		t.Fatalf("focused Height() = %d, want 2", got)
	}
}

func TestTextArea_HideVirtualCursor(t *testing.T) {
	type tc struct {
		text    string
		cursor  int
		wantLen int
	}

	tests := map[string]tc{
		"returns line unchanged":          {text: "hello", cursor: 3, wantLen: 5},
		"returns space on empty line":     {text: "", cursor: 0, wantLen: 1},
		"line width matches wrapped text": {text: "hello world", cursor: 5, wantLen: 11},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			ta := NewTextArea(
				WithTextAreaVirtualCursor(false),
			)
			ta.BindApp(testApp)
			ta.SetText(tt.text)
			ta.Focus()
			ta.cursorPos.Set(tt.cursor)

			lines := ta.wrapText()
			for i := range lines {
				rendered := ta.lineWithCursor(i)
				if utf8.RuneCountInString(rendered) != tt.wantLen {
					t.Fatalf("lineWithCursor(%d) = %q (len=%d), want len=%d", i, rendered, utf8.RuneCountInString(rendered), tt.wantLen)
				}
			}
		})
	}
}