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
|
package tui
import (
"strings"
"testing"
)
func TestWrapText(t *testing.T) {
type tc struct {
text string
maxWidth int
want []string
}
tests := map[string]tc{
"empty string": {
text: "",
maxWidth: 10,
want: []string{""},
},
"fits on one line": {
text: "hello",
maxWidth: 10,
want: []string{"hello"},
},
"wraps at word boundary": {
text: "hello world",
maxWidth: 7,
want: []string{"hello", "world"},
},
"multiple wraps": {
text: "the quick brown fox",
maxWidth: 10,
want: []string{"the quick", "brown fox"},
},
"long word breaks mid-word": {
text: "abcdefghij",
maxWidth: 5,
want: []string{"abcde", "fghij"},
},
"long word after short word": {
text: "hi abcdefghij",
maxWidth: 5,
want: []string{"hi", "abcde", "fghij"},
},
"preserves newlines": {
text: "line1\nline2",
maxWidth: 20,
want: []string{"line1", "line2"},
},
"wraps within newline sections": {
text: "hello world\nfoo bar",
maxWidth: 7,
want: []string{"hello", "world", "foo bar"},
},
"zero width": {
text: "hello",
maxWidth: 0,
want: []string{""},
},
"width of 1": {
text: "hi",
maxWidth: 1,
want: []string{"h", "i"},
},
"exact fit": {
text: "hello",
maxWidth: 5,
want: []string{"hello"},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := wrapText(tt.text, tt.maxWidth)
if len(got) != len(tt.want) {
t.Fatalf("wrapText(%q, %d) = %v (len %d), want %v (len %d)",
tt.text, tt.maxWidth, got, len(got), tt.want, len(tt.want))
}
for i := range got {
if got[i] != tt.want[i] {
t.Errorf("line %d: got %q, want %q", i, got[i], tt.want[i])
}
}
})
}
}
func TestWrapSpans_KeepsStyleAcrossWrap(t *testing.T) {
bold := NewStyle().Bold()
spans := []TextSpan{
{Text: "aa "},
{Text: "bbbb cccc", Style: bold},
{Text: " dd"},
}
lines := wrapSpans(spans, 6)
if len(lines) < 2 {
t.Fatalf("expected multiple lines, got %d: %+v", len(lines), lines)
}
for li, line := range lines {
for _, seg := range line {
if seg.Text == "bbbb" || seg.Text == "cccc" {
if seg.Style.Attrs&AttrBold == 0 {
t.Errorf("line %d: %q lost bold", li, seg.Text)
}
}
}
}
}
func TestWrapSpans_MergesAdjacentSameStyle(t *testing.T) {
spans := []TextSpan{{Text: "foo "}, {Text: "bar"}}
lines := wrapSpans(spans, 40)
if len(lines) != 1 {
t.Fatalf("want 1 line, got %d", len(lines))
}
if len(lines[0]) != 1 {
t.Errorf("adjacent same-style segments should merge into 1, got %d: %+v", len(lines[0]), lines[0])
}
if lines[0][0].Text != "foo bar" {
t.Errorf("merged text = %q, want \"foo bar\"", lines[0][0].Text)
}
}
func TestWrapSpans_Empty(t *testing.T) {
if got := wrapSpans(nil, 10); len(got) != 1 || len(got[0]) != 0 {
t.Errorf("empty spans should give one empty line, got %+v", got)
}
}
func joinSpanLine(line []TextSpan) string {
var s strings.Builder
for _, seg := range line {
s.WriteString(seg.Text)
}
return s.String()
}
func hasControlRune(lines [][]TextSpan) bool {
for _, line := range lines {
for _, seg := range line {
for _, r := range seg.Text {
switch r {
case '\r', '\v', '\f', '\n':
return true
}
}
}
}
return false
}
func TestWrapSpans_ExoticWhitespaceDoesNotLeak(t *testing.T) {
lines := wrapSpans([]TextSpan{{Text: "a\r\nb"}}, 10)
if hasControlRune(lines) {
t.Fatalf("control rune leaked into output: %+v", lines)
}
if len(lines) != 2 {
t.Fatalf("want 2 lines, got %d: %+v", len(lines), lines)
}
if len(lines[0]) != 1 || lines[0][0].Text != "a" {
t.Errorf("line 0 = %+v, want single segment \"a\"", lines[0])
}
if len(lines[1]) != 1 || lines[1][0].Text != "b" {
t.Errorf("line 1 = %+v, want single segment \"b\"", lines[1])
}
}
func TestWrapSpans_VerticalTabAndFormFeedAreSeparators(t *testing.T) {
lines := wrapSpans([]TextSpan{{Text: "a\vb\fc"}}, 20)
if hasControlRune(lines) {
t.Fatalf("control rune leaked into output: %+v", lines)
}
if len(lines) != 1 {
t.Fatalf("want 1 line (no line break), got %d: %+v", len(lines), lines)
}
if got := spanLineWidth(lines[0]); got != 5 {
t.Errorf("line width = %d, want 5", got)
}
}
func TestWrapSpans_SpanBoundaryIsNotWordBoundary_HardBreak(t *testing.T) {
lines := wrapSpans([]TextSpan{{Text: "ab"}, {Text: "cd"}}, 3)
if len(lines) != 2 {
t.Fatalf("want 2 lines, got %d: %+v", len(lines), lines)
}
if joinSpanLine(lines[0]) != "abc" || joinSpanLine(lines[1]) != "d" {
t.Errorf("got %q / %q, want \"abc\" / \"d\" (hard break, not seam split)",
joinSpanLine(lines[0]), joinSpanLine(lines[1]))
}
}
func TestWrapSpans_StylePreservedAcrossHardBreak(t *testing.T) {
bold := NewStyle().Bold()
lines := wrapSpans([]TextSpan{{Text: "go"}, {Text: "lang", Style: bold}}, 4)
if len(lines) != 2 {
t.Fatalf("want 2 lines, got %d: %+v", len(lines), lines)
}
if joinSpanLine(lines[0]) != "gola" || joinSpanLine(lines[1]) != "ng" {
t.Fatalf("got %q / %q, want \"gola\" / \"ng\"", joinSpanLine(lines[0]), joinSpanLine(lines[1]))
}
if len(lines[0]) != 2 || lines[0][0].Text != "go" || lines[0][0].Style.Attrs&AttrBold != 0 {
t.Errorf("line 0 seg 0 should be plain \"go\", got %+v", lines[0])
}
if len(lines[0]) != 2 || lines[0][1].Text != "la" || lines[0][1].Style.Attrs&AttrBold == 0 {
t.Errorf("line 0 seg 1 should be bold \"la\", got %+v", lines[0])
}
if len(lines[1]) != 1 || lines[1][0].Text != "ng" || lines[1][0].Style.Attrs&AttrBold == 0 {
t.Errorf("line 1 should be bold \"ng\", got %+v", lines[1])
}
}
func TestWrapSpans_WideRuneHardBreakDoesNotOverflow(t *testing.T) {
if RuneWidth('世') != 2 {
t.Fatalf("precondition: expected '世' to be width 2, got %d", RuneWidth('世'))
}
lines := wrapSpans([]TextSpan{{Text: "世界世"}}, 3)
for i, line := range lines {
if w := spanLineWidth(line); w > 3 {
t.Errorf("line %d width = %d, overflows maxWidth 3: %+v", i, w, line)
}
}
}
func TestWrapSpans_PreservesLinkAndSplitsOnLinkChange(t *testing.T) {
lines := wrapSpans([]TextSpan{
{Text: "ab", Link: "X"},
{Text: "cd", Link: "Y"},
}, 40)
if len(lines) != 1 || len(lines[0]) != 2 {
t.Fatalf("want one line of two segments, got %+v", lines)
}
if lines[0][0].Text != "ab" || lines[0][0].Link != "X" {
t.Errorf("seg 0 = %+v, want {ab, X}", lines[0][0])
}
if lines[0][1].Text != "cd" || lines[0][1].Link != "Y" {
t.Errorf("seg 1 = %+v, want {cd, Y}", lines[0][1])
}
}
func TestWrapSpans_LinkSpacesStayLinkedAndStyled(t *testing.T) {
link := NewStyle().Underline()
lines := wrapSpans([]TextSpan{
{Text: "go to site", Style: link, Link: "http://x"},
}, 40)
if len(lines) != 1 {
t.Fatalf("want one line, got %d", len(lines))
}
for i, seg := range lines[0] {
if seg.Link != "http://x" {
t.Errorf("segment %d %q lost the link: %+v", i, seg.Text, seg)
}
if seg.Style.Attrs&AttrUnderline == 0 {
t.Errorf("segment %d %q lost the underline: %+v", i, seg.Text, seg)
}
}
}
func TestWrapSpans_NonLinkSeparatorStaysNeutral(t *testing.T) {
lines := wrapSpans([]TextSpan{
{Text: "bold words", Style: NewStyle().Bold()},
}, 40)
if len(lines) != 1 {
t.Fatalf("want one line, got %d", len(lines))
}
var joined strings.Builder
for _, seg := range lines[0] {
if seg.Text == " " && seg.Style.Attrs&AttrBold != 0 {
t.Errorf("separator space should not be bold: %+v", seg)
}
joined.WriteString(seg.Text)
}
if joined.String() != "bold words" {
t.Errorf("joined = %q, want %q", joined.String(), "bold words")
}
}
|