gitstack

grindlemire/go-tui code browser

4.3 KB Go 186 lines 2026-06-03 · d15bb9f 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
package tui

import (
	"strings"
	"testing"
)

func TestBufferRowToANSI(t *testing.T) {
	type tc struct {
		setup    func(buf *Buffer)
		row      int
		contains []string
		notEmpty bool
	}

	defaultCaps := Capabilities{Colors: ColorTrue, TrueColor: true}

	tests := map[string]tc{
		"plain text": {
			setup: func(buf *Buffer) {
				buf.SetString(0, 0, "hello", NewStyle())
			},
			row:      0,
			contains: []string{"hello"},
			notEmpty: true,
		},
		"styled text emits ANSI": {
			setup: func(buf *Buffer) {
				buf.SetString(0, 0, "red", NewStyle().Foreground(Red))
			},
			row:      0,
			contains: []string{"\x1b[", "red", "\x1b[0m"},
			notEmpty: true,
		},
		"trailing empty cells trimmed": {
			setup: func(buf *Buffer) {
				buf.SetString(0, 0, "hi", NewStyle())
				// rest of the 20-wide buffer is empty
			},
			row:      0,
			contains: []string{"hi"},
			notEmpty: true,
		},
		"wide character skips continuation": {
			setup: func(buf *Buffer) {
				// CJK character '中' is 2 cells wide
				buf.SetRune(0, 0, '中', NewStyle())
			},
			row:      0,
			contains: []string{"中"},
			notEmpty: true,
		},
		"empty row returns empty string": {
			setup:    func(buf *Buffer) {},
			row:      0,
			notEmpty: false,
		},
		"style transition mid-row": {
			setup: func(buf *Buffer) {
				buf.SetString(0, 0, "ab", NewStyle().Bold())
				buf.SetString(2, 0, "cd", NewStyle().Italic())
			},
			row: 0,
			// Both style changes must appear plus the text
			contains: []string{"ab", "cd", "\x1b[0m"},
			notEmpty: true,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			buf := NewBuffer(20, 2)
			tt.setup(buf)

			esc := newEscBuilder(128)
			result := bufferRowToANSI(buf, tt.row, esc, defaultCaps)

			if tt.notEmpty && result == "" {
				t.Fatalf("expected non-empty result, got empty")
			}
			if !tt.notEmpty && result != "" {
				t.Fatalf("expected empty result, got %q", result)
			}

			for _, sub := range tt.contains {
				if !containsStr(result, sub) {
					t.Errorf("result %q does not contain %q", result, sub)
				}
			}
		})
	}
}

// containsStr is a simple substring check helper.
func containsStr(s, sub string) bool {
	return len(sub) <= len(s) && searchStr(s, sub)
}

func searchStr(s, sub string) bool {
	for i := 0; i <= len(s)-len(sub); i++ {
		if s[i:i+len(sub)] == sub {
			return true
		}
	}
	return false
}

func TestRenderElementToBuffer(t *testing.T) {
	type tc struct {
		el         *Element
		width      int
		wantHeight int // expected height; 0 means nil buffer
	}

	defaultCaps := Capabilities{Colors: ColorTrue, TrueColor: true}

	tests := map[string]tc{
		"simple text span": {
			el:         New(WithText("hello")),
			width:      20,
			wantHeight: 1,
		},
		"text with border": {
			el:         New(WithText("hi"), WithBorder(BorderSingle)),
			width:      20,
			wantHeight: 3, // 1 text + 2 border rows
		},
		"empty element": {
			el:         New(),
			width:      20,
			wantHeight: 0,
		},
		"column with children": {
			el: func() *Element {
				parent := New(WithDirection(Column))
				parent.AddChild(
					New(WithText("line 1")),
					New(WithText("line 2")),
				)
				return parent
			}(),
			width:      20,
			wantHeight: 2,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			buf, height := renderElementToBuffer(tt.el, tt.width, defaultCaps)

			if height != tt.wantHeight {
				t.Fatalf("height = %d, want %d", height, tt.wantHeight)
			}
			if tt.wantHeight == 0 {
				if buf != nil {
					t.Fatalf("expected nil buffer for zero height")
				}
				return
			}
			if buf == nil {
				t.Fatalf("expected non-nil buffer for height %d", tt.wantHeight)
			}
			if buf.Width() != tt.width {
				t.Fatalf("buffer width = %d, want %d", buf.Width(), tt.width)
			}
			if buf.Height() != tt.wantHeight {
				t.Fatalf("buffer height = %d, want %d", buf.Height(), tt.wantHeight)
			}
		})
	}
}

func TestBufferRowToANSI_EmitsHyperlink(t *testing.T) {
	buf := NewBuffer(3, 1)
	buf.SetRuneLink(0, 0, 'a', NewStyle(), "https://example.com")
	buf.SetRuneLink(1, 0, 'b', NewStyle(), "https://example.com")
	esc := newEscBuilder(64)
	caps := Capabilities{Colors: Color16, Hyperlinks: true}
	row := bufferRowToANSI(buf, 0, esc, caps)
	if strings.Count(row, "\x1b]8;;https://example.com\x1b\\") != 1 {
		t.Errorf("want one OSC 8 open: %q", row)
	}
	if !strings.Contains(row, "\x1b]8;;\x1b\\") {
		t.Errorf("missing OSC 8 close: %q", row)
	}
}