Debian Codegraph @

vt/tests/mock_test.go

raw symbols SCIP
1 // Copyright 2026 The TCell Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package tests
16
17 import (
18 "strings"
19 "testing"
20 "time"
21
22 "github.com/gdamore/tcell/v3/color"
23 "github.com/gdamore/tcell/v3/vt"
24 )
25
26 // TestCursorMove tests several aspects of cursor movement.
27 func TestCursorMovement(t *testing.T) {
28 term := vt.NewMockTerm(vt.MockOptSize{X: 5, Y: 3}, vt.MockOptColors(0))
29 defer MustClose(t, term)
30
31 MustStart(t, term)
32
33 if size, err := term.WindowSize(); err != nil {
34 t.Fatalf("failed getting window size: %v", err)
35 } else if size.Height != 3 || size.Width != 5 {
36 t.Fatalf("wrong window size X %d Y %d", size.Width, size.Height)
37 }
38 WriteF(t, term, "\x1b[2;3H")
39 CheckPos(t, term, 2, 1)
40
41 WriteF(t, term, "\x1b[20A") // up 20
42 CheckPos(t, term, 2, 0)
43
44 WriteF(t, term, "\x1b[20B") // down 20
45 CheckPos(t, term, 2, 2)
46
47 WriteF(t, term, "\x1b[A") // up 1
48 CheckPos(t, term, 2, 1)
49
50 WriteF(t, term, "\x1b[2C") // right 2
51 CheckPos(t, term, 4, 1)
52
53 WriteF(t, term, "\x1b[3D") // left 3
54 CheckPos(t, term, 1, 1)
55
56 WriteF(t, term, "\x1b[100D") // left 100
57 CheckPos(t, term, 0, 1)
58
59 // Now try the next line and previous line
60 WriteF(t, term, "\x1b[2;3H")
61 CheckPos(t, term, 2, 1)
62
63 WriteF(t, term, "\x1b[1E")
64 CheckPos(t, term, 0, 2)
65
66 WriteF(t, term, "\x1b[2;3H")
67 CheckPos(t, term, 2, 1)
68
69 WriteF(t, term, "\x1b[1F")
70 CheckPos(t, term, 0, 0)
71
72 WriteF(t, term, "\x1b9")
73 CheckPos(t, term, 1, 0)
74
75 WriteF(t, term, "\x1b6")
76 CheckPos(t, term, 0, 0)
77 }
78
79 // TestDECALN tests the DEC alignment test (screen filled with 'E').
80 func TestDECALN(t *testing.T) {
81 term := vt.NewMockTerm(vt.MockOptSize{X: 5, Y: 3}, vt.MockOptColors(0))
82 defer MustClose(t, term)
83
84 MustStart(t, term)
85
86 WriteF(t, term, "\x1b#8")
87
88 for y := range Row(3) {
89 for x := range Col(5) {
90 CheckAttrs(t, term, x, y, vt.Plain)
91 CheckContent(t, term, x, y, "E")
92 }
93 }
94
95 // clear screen
96 WriteF(t, term, "\x1b[H\x1b[J")
97
98 for y := range Row(3) {
99 for x := range Col(5) {
100 CheckAttrs(t, term, x, y, vt.Plain)
101 CheckContent(t, term, x, y, "")
102 }
103 }
104
105 WriteF(t, term, "\x1b[1m\x1b#8") // bold, DECALN
106 for y := range Row(3) {
107 for x := range Col(5) {
108 CheckAttrs(t, term, x, y, vt.Plain)
109 CheckContent(t, term, x, y, "E")
110 }
111 }
112 }
113
114 // TestBell tests the bell.
115 func TestBell(t *testing.T) {
116 term := vt.NewMockTerm(vt.MockOptSize{X: 5, Y: 3}, vt.MockOptColors(0))
117 defer MustClose(t, term)
118 MustStart(t, term)
119
120 if term.Bells() != 0 {
121 t.Errorf("wrong bell count: %d", term.Bells())
122 }
123 WriteF(t, term, "\x07")
124 if term.Bells() != 1 {
125 t.Errorf("wrong bell count: %d", term.Bells())
126 }
127 WriteF(t, term, "\x07")
128 if term.Bells() != 2 {
129 t.Errorf("wrong bell count: %d", term.Bells())
130 }
131 }
132
133 // TestPrimaryDA tests primary device attributes using several mechanisms.
134 func TestPrimaryDA(t *testing.T) {
135 term := vt.NewMockTerm(vt.MockOptSize{X: 5, Y: 3}, vt.MockOptColors(0))
136 defer MustClose(t, term)
137
138 MustStart(t, term)
139
140 buf := make([]byte, 32)
141 WriteF(t, term, "\x1b[c")
142
143 n, err := term.Read(buf)
144 if err != nil {
145 t.Errorf("failed read: %v", err)
146 }
147 result := string(buf[:n])
148 if !strings.HasSuffix(result, "c") {
149 t.Errorf("Missing suffix 'c': %q", result)
150 }
151 if !strings.HasPrefix(result, "\x1b[?63") {
152 t.Errorf("Missing prefix '\x1b[?63': %q", result)
153 }
154
155 // Legacy version
156 buf = make([]byte, 32) // to start over
157 WriteF(t, term, "\x1bZ")
158
159 n, err = term.Read(buf)
160 if err != nil {
161 t.Errorf("failed read: %v", err)
162 }
163 result = string(buf[:n])
164 if !strings.HasSuffix(result, "c") {
165 t.Errorf("Missing suffix 'c': %q", result)
166 }
167 if !strings.HasPrefix(result, "\x1b[?63") {
168 t.Errorf("Missing prefix '\x1b[?63': %q", result)
169 }
170 }
171
172 // TestExtendedAttr requests the terminal ID using CSI > q.
173 func TestExtendedAttr(t *testing.T) {
174 term := vt.NewMockTerm(vt.MockOptSize{X: 5, Y: 3}, vt.MockOptColors(0))
175 defer MustClose(t, term)
176
177 MustStart(t, term)
178
179 buf := make([]byte, 64)
180 WriteF(t, term, "\x1b[>q")
181
182 n, err := term.Read(buf)
183 AssertF(t, err == nil, "read failed: %v", err)
184
185 result := string(buf[:n])
186
187 VerifyF(t, strings.HasSuffix(result, "\x1b\\"), "missing suffix ST: %q", result)
188 VerifyF(t, strings.HasPrefix(result, "\x1bP>|"), "missing prefix 'ESC P>|': %q", result)
189 VerifyF(t, strings.Contains(result, "TCellMock 1.0"), "missing terminal identification: %q", result)
190 }
191
192 // TestCursorReport verifies that cursor position reporting works.
193 func TestCursorReport(t *testing.T) {
194 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24}, vt.MockOptColors(0))
195 defer MustClose(t, term)
196
197 MustStart(t, term)
198
199 WriteF(t, term, "\x1b[5;10H") // fifth row, tenth column
200 CheckPos(t, term, 9, 4)
201
202 WriteF(t, term, "\x1b[6n") // cursor position report
203
204 buf := make([]byte, 32)
205 n, err := term.Read(buf)
206 if err != nil {
207 t.Errorf("failed read: %v", err)
208 }
209 result := string(buf[:n])
210 if result != "\x1b[5;10R" {
211 t.Errorf("wrong report: %q", result)
212 }
213
214 buf = make([]byte, 32)
215 // move the cursor back one
216 WriteF(t, term, "\b\x1b[6n")
217 CheckPos(t, term, 8, 4)
218 n, err = term.Read(buf)
219 if err != nil {
220 t.Errorf("failed read: %v", err)
221 }
222 result = string(buf[:n])
223 if result != "\x1b[5;9R" {
224 t.Errorf("wrong report: %q", result)
225 }
226 }
227
228 // TestAnsiModes tests the private mode feature.
229 func TestAnsiModes(t *testing.T) {
230 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24}, vt.MockOptColors(0))
231 defer MustClose(t, term)
232
233 MustStart(t, term)
234
235 WriteF(t, term, "\x1b[20$p") // query for newline mode
236 WriteF(t, term, "\x1b[20h") // turn it on
237 WriteF(t, term, "\x1b[20$p") // should read back on
238 WriteF(t, term, "\x1b[20l") // turn it back off
239 WriteF(t, term, "\x1b[20$p") // should read back off
240
241 buf := make([]byte, 128)
242 n, err := term.Read(buf)
243 if err != nil {
244 t.Errorf("failed read: %v", err)
245 }
246 result := string(buf[:n])
247 want := "\x1b[20;2$y" + "\x1b[20;1$y" + "\x1b[20;2$y"
248 if result != want {
249 t.Errorf("wrong response: %q != %q", result, want)
250 }
251 }
252
253 // TestPrivateModes tests the private mode feature.
254 func TestPrivateModes(t *testing.T) {
255 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24}, vt.MockOptColors(0))
256 defer MustClose(t, term)
257
258 MustStart(t, term)
259
260 WriteF(t, term, "\x1b[?7$p") // query for auto-margin (should start on by default)
261 WriteF(t, term, "\x1b[?7l") // turn it off
262 WriteF(t, term, "\x1b[?7$p") // should read back positive
263 WriteF(t, term, "\x1b[?7h") // put it back on
264 WriteF(t, term, "\x1b[?7$p") // should read back negative
265 WriteF(t, term, "\x1b[?1919$p") // read invalid mode
266 WriteF(t, term, "\x1b[?1919h\x1b[?1919l") // togle invalid mode
267 WriteF(t, term, "\x1b[?1919$p") // read invalid mode one more time
268
269 buf := make([]byte, 128)
270 n, err := term.Read(buf)
271 if err != nil {
272 t.Errorf("failed read: %v", err)
273 }
274 result := string(buf[:n])
275 want := "\x1b[?7;1$y" + "\x1b[?7;2$y" + "\x1b[?7;1$y" + "\x1b[?1919;0$y" + "\x1b[?1919;0$y"
276 if result != want {
277 t.Errorf("wrong response: %q != %q", result, want)
278 }
279
280 // Lets also test the cursor (show vs hide)
281 WriteF(t, term, "\x1b[?25$p")
282 WriteF(t, term, "\x1b[?25l")
283 WriteF(t, term, "\x1b[?25$p")
284 WriteF(t, term, "\x1b[?25h")
285 WriteF(t, term, "\x1b[?25$p")
286
287 buf = make([]byte, 128)
288 n, err = term.Read(buf)
289 if err != nil {
290 t.Errorf("failed read: %v", err)
291 }
292 result = string(buf[:n])
293
294 want = "\x1b[?25;1$y" + "\x1b[?25;2$y" + "\x1b[?25;1$y"
295 if result != want {
296 t.Errorf("wrong response: %q != %q", result, want)
297 }
298 }
299
300 // TestAutoMargin tests the auto margin feature.
301 func TestAutoMargin(t *testing.T) {
302 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24}, vt.MockOptColors(0))
303 defer MustClose(t, term)
304 MustStart(t, term)
305
306 // default is auto-margin is enabled
307 WriteF(t, term, "\x1b[2J") // clear the screen
308 WriteF(t, term, "\x1b[1;80HAB")
309 CheckPos(t, term, 1, 1)
310 if s := string(term.GetCell(Coord{X: 79, Y: 0}).C); s != "A" {
311 t.Errorf("last column wrong: %q", s)
312 }
313 if s := string(term.GetCell(Coord{X: 0, Y: 1}).C); s != "B" {
314 t.Errorf("auto wrap did not work: %q", s)
315 }
316
317 // now turn it off
318 WriteF(t, term, "\x1b[?7l")
319
320 // mess with 3rd row
321 WriteF(t, term, "\x1b[3;80HCD")
322 CheckPos(t, term, 79, 2)
323 if s := string(term.GetCell(Coord{X: 79, Y: 2}).C); s != "D" {
324 t.Errorf("last column wrong: %q", s)
325 }
326
327 // turn it back on
328 WriteF(t, term, "\x1b[?7h")
329
330 // demonstrate that writing to the last column does not advance (pending)
331 WriteF(t, term, "\x1b[1;80HA")
332 CheckPos(t, term, 79, 0)
333
334 // but one more character does advance
335 WriteF(t, term, "\x1b[1;80HAB")
336 CheckPos(t, term, 1, 1)
337
338 // tab does not advance, but leaves pending state
339 WriteF(t, term, "\x1b[1;80HA\t")
340 CheckPos(t, term, 79, 0)
341 WriteF(t, term, "\x1b[1;80HA\tb")
342 CheckPos(t, term, 1, 1)
343
344 // up or down movement resets the pending state
345 WriteF(t, term, "\x1b[1;80HA\x1b[AB")
346 CheckPos(t, term, 79, 0)
347 WriteF(t, term, "\x1b[1;80HA\x1b[BB")
348 CheckPos(t, term, 79, 1)
349
350 // forward also resets pending state (which is clipped)
351 WriteF(t, term, "\x1b[1;80HA\x1b[CB")
352 CheckPos(t, term, 79, 0)
353 WriteF(t, term, "\x1b[1;80HA\x1b[CBC")
354 CheckPos(t, term, 1, 1)
355
356 // explicit column also resets pending state (which is clipped)
357 WriteF(t, term, "\x1b[1;80HA\x1b[80GB")
358 CheckPos(t, term, 79, 0)
359 WriteF(t, term, "\x1b[1;80HA\x1b[80GBC")
360 CheckPos(t, term, 1, 1)
361
362 // newline of course as well (and also VF and FF)
363 WriteF(t, term, "\x1b[1;80HA\nB")
364 CheckPos(t, term, 79, 1)
365 WriteF(t, term, "\x1b[1;80HA\fB")
366 CheckPos(t, term, 79, 1)
367 WriteF(t, term, "\x1b[1;80HA\vB")
368 CheckPos(t, term, 79, 1)
369
370 // and also index
371 WriteF(t, term, "\x1b[1;80HA\x1bDB")
372 CheckPos(t, term, 79, 1)
373
374 // and also reverse index
375 WriteF(t, term, "\x1b[2;80HA\x1bMB")
376 CheckPos(t, term, 79, 0)
377 }
378
379 // TestUnicode tests basic placement of unicode glyphs.
380 // For now it assumes that the terminal itself supports unicode / latin 1.
381 func TestUnicode(t *testing.T) {
382 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24}, vt.MockOptColors(0))
383 defer MustClose(t, term)
384 MustStart(t, term)
385
386 WriteF(t, term, "\x1b[2J") // clear the screen
387 WriteF(t, term, "\x1b[2;2H")
388 CheckPos(t, term, 1, 1)
389 WriteF(t, term, "åßcπ")
390 CheckPos(t, term, 5, 1)
391 if s := string(term.GetCell(Coord{X: 1, Y: 1}).C); s != "Ã¥" {
392 t.Errorf("decode error wrong: %q", s)
393 }
394 if s := string(term.GetCell(Coord{X: 2, Y: 1}).C); s != "ß" {
395 t.Errorf("decode error wrong: %q", s)
396 }
397 if s := string(term.GetCell(Coord{X: 3, Y: 1}).C); s != "c" {
398 t.Errorf("decode error wrong: %q", s)
399 }
400 if s := string(term.GetCell(Coord{X: 4, Y: 1}).C); s != "Ï€" {
401 t.Errorf("decode error wrong: %q", s)
402 }
403 }
404
405 // TestUnicodeWide tests a wide unicode glyph.
406 func TestUnicodeWide(t *testing.T) {
407 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24}, vt.MockOptColors(0))
408 defer MustClose(t, term)
409 MustStart(t, term)
410
411 WriteF(t, term, "\x1b#8") // fill it with E's (so we can see that wide clears the next cell)
412 WriteF(t, term, "\x1b[2;2H")
413 CheckPos(t, term, 1, 1)
414 WriteF(t, term, "å宽cπ")
415 CheckPos(t, term, 6, 1)
416 if s := string(term.GetCell(Coord{X: 1, Y: 1}).C); s != "Ã¥" {
417 t.Errorf("decode error wrong: %q", s)
418 }
419 if s := string(term.GetCell(Coord{X: 2, Y: 1}).C); s != "宽" {
420 t.Errorf("decode error wrong: %q", s)
421 }
422 if s := string(term.GetCell(Coord{X: 3, Y: 1}).C); s != "" {
423 t.Errorf("decode error wrong: %q", s)
424 }
425 if s := string(term.GetCell(Coord{X: 4, Y: 1}).C); s != "c" {
426 t.Errorf("decode error wrong: %q", s)
427 }
428 if s := string(term.GetCell(Coord{X: 5, Y: 1}).C); s != "Ï€" {
429 t.Errorf("decode error wrong: %q", s)
430 }
431 }
432
433 // TestKeyEventLegacy tests key events when using the default legacy key protocol.
434 func TestKeyEventLegacy(t *testing.T) {
435 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24}, vt.MockOptColors(0))
436 defer MustClose(t, term)
437 MustStart(t, term)
438
439 term.KeyTap(vt.KeyA)
440 term.KeyTap(vt.KeyLShift, vt.KeyB)
441 term.KeyTap(vt.KeyEnter)
442 term.KeyTap(vt.KeyRCtrl, vt.KeyI)
443 term.KeyTap(vt.KeyEsc)
444
445 buf := make([]byte, 256)
446 n, err := term.Read(buf)
447 if err != nil {
448 t.Errorf("failed read: %v", err)
449 }
450 result := string(buf[:n])
451 want := "aB\r\x09\x1B"
452 if result != want {
453 t.Errorf("key responses failed: %q != %q", result, want)
454 }
455
456 // SS3 based F-keys
457 clear(buf)
458 term.KeyTap(vt.KeyF1) // SS3 P
459 term.KeyTap(vt.KeyLShift, vt.KeyF1) // CSI 1 ; 2 P
460 term.KeyTap(vt.KeyLCtrl, vt.KeyF2) // CSI 1 ; 5 Q
461 term.KeyTap(vt.KeyLAlt, vt.KeyLCtrl, vt.KeyLShift, vt.KeyF3) // ESC CSI 1 ; 6 R
462 term.KeyTap(vt.KeyRAlt, vt.KeyRCtrl, vt.KeyF4) // ESC CSI 1 ; 5 S
463
464 want = "\x1bOP"
465 want += "\x1b[1;2P"
466 want += "\x1b[1;5Q"
467 want += "\x1b\x1b[1;6R"
468 want += "\x1b\x1b[1;5S"
469 n, err = term.Read(buf)
470 if err != nil {
471 t.Errorf("failed read: %v", err)
472 }
473 result = string(buf[:n])
474 if result != want {
475 t.Errorf("key responses failed: %q != %q", result, want)
476 }
477
478 // CSI based F-keys
479 buf = make([]byte, 256)
480 term.KeyTap(vt.KeyF5) // CSI 15 ~
481 term.KeyTap(vt.KeyRShift, vt.KeyF6) // CSI 17 ; 2 ~
482 term.KeyTap(vt.KeyLCtrl, vt.KeyF7) // CSI 18 ; 5 ~
483 term.KeyTap(vt.KeyLAlt, vt.KeyRCtrl, vt.KeyLShift, vt.KeyF8) // ESC CSI 19 ; 6 ~
484 term.KeyTap(vt.KeyRAlt, vt.KeyLCtrl, vt.KeyF9) // ESC CSI 20 ; 5 ~
485 term.KeyTap(vt.KeyF20) // CSI 34 ~
486 term.KeyTap(vt.KeyF15) // CSI 28 ~
487 term.KeyTap(vt.KeyMenu) // CSI 29 ~
488 want = "\x1b[15~"
489 want += "\x1b[17;2~"
490 want += "\x1b[18;5~"
491 want += "\x1b\x1b[19;6~"
492 want += "\x1b\x1b[20;5~"
493 want += "\x1b[34~"
494 want += "\x1b[28~"
495 want += "\x1b[29~"
496 n, err = term.Read(buf)
497 AssertF(t, err == nil, "failed read: %v", err)
498
499 result = string(buf[:n])
500 VerifyF(t, result == want, "key responses failed: %q != %q", result, want)
501
502 // Misc other keys
503 clear(buf)
504 term.KeyTap(vt.KeyEnter) // \r
505 term.KeyTap(vt.KeyTab) // \t
506 term.KeyTap(vt.KeyLShift, vt.KeyTab) // CSI Z
507 term.KeyTap(vt.KeyLCtrl, vt.KeyM) // \r
508 term.KeyTap(vt.KeyLCtrl, vt.KeyL) // \f
509 term.KeyTap(vt.KeyBackspace) // \x7f
510 term.KeyTap(vt.KeyRCtrl, vt.KeyBackspace) // \x08
511 term.KeyTap(vt.KeyRCtrl, vt.KeyLShift, vt.KeyBackspace) // none
512 term.KeyTap(vt.KeyRCtrl, vt.KeySpace) // \x00
513 term.KeyTap(vt.KeySpace) // ' '
514 term.KeyTap(vt.KeyRAlt, vt.KeyA) // \x1b a
515 term.KeyTap(vt.KeyRHyper, vt.KeyA) // none
516 term.KeyTap(vt.KeyRMeta, vt.KeyA) // none
517 term.KeyTap(vt.KeyRAlt, vt.KeyLCtrl, vt.KeyJ) // \x1b\n
518 term.KeyTap(vt.KeyRCtrl, vt.KeyL) // \x0c
519 term.KeyTap(vt.KeyLCtrl, vt.KeyLBrace) // \x0c
520
521 want = "\r\t\x1b[Z\r\f\x7f\x08\x00 \x1ba\x1b\n\x0c\x1b"
522 n, err = term.Read(buf)
523 AssertF(t, err == nil, "failed read: %v", err)
524
525 result = string(buf[:n])
526 VerifyF(t, result == want, "key responses failed: %q != %q", result, want)
527
528 // Legacy control key mappings (weird ones)
529 // Declining a few of the strange ones (control-?)
530 clear(buf)
531 term.KeyTap(vt.KeyLCtrl, vt.Key8) // \x7F
532 term.KeyTap(vt.KeyLCtrl, vt.Key4) // \x1c
533 term.KeyTap(vt.KeyLCtrl, vt.Key7) // \x1f
534 term.KeyTap(vt.Key7) // 7
535 term.KeyTap(vt.KeyLShift, vt.KeySlash) // ?
536 term.KeyTap(vt.KeyRCtrl, vt.KeyLBrace) // \x1b
537
538 want = "\x7f\x1c\x1f7?\x1b"
539 n, err = term.Read(buf)
540 AssertF(t, err == nil, "failed read: %v", err)
541
542 result = string(buf[:n])
543 VerifyF(t, result == want, "key responses failed: %q != %q", result, want)
544
545 // Application cursor keys
546 term.KeyTap(vt.KeyUp)
547 WriteF(t, term, "\x1b[?1h")
548 term.KeyTap(vt.KeyDown)
549 want = "\x1b[A\x1bOB"
550 n, err = term.Read(buf)
551 AssertF(t, err == nil, "failed read: %v", err)
552
553 result = string(buf[:n])
554 VerifyF(t, result == want, "key responses failed: %q != %q", result, want)
555
556 // Special Tab
557 term.KeyTap(vt.KeyLShift, vt.KeyLCtrl, vt.KeyTab)
558 term.KeyTap(vt.KeySpace)
559 CheckRead(t, term, "\x1b[Z ")
560 }
561
562 // TestSgrAttr tests a variety of combinations of Sgr settings.
563 func TestSgrAttr(t *testing.T) {
564 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24})
565 defer MustClose(t, term)
566 MustStart(t, term)
567
568 WriteF(t, term, "\x1b[H")
569 WriteF(t, term, "\x1b[1mA") // bold
570 CheckAttrs(t, term, 0, 0, vt.Bold)
571 CheckContent(t, term, 0, 0, "A")
572
573 WriteF(t, term, "\x1b[2mB") // dim
574 CheckAttrs(t, term, 1, 0, vt.Dim)
575 CheckContent(t, term, 1, 0, "B")
576
577 WriteF(t, term, "\x1b[22mC") // clear both
578 CheckAttrs(t, term, 2, 0, vt.Plain)
579 CheckContent(t, term, 2, 0, "C")
580
581 WriteF(t, term, "\x1b[3;2mD") // italic, dim
582 CheckAttrs(t, term, 3, 0, vt.Italic|vt.Dim)
583 CheckContent(t, term, 3, 0, "D")
584
585 WriteF(t, term, "\x1b[22mE") // remove dim, should leave italic
586 CheckAttrs(t, term, 4, 0, vt.Italic)
587 CheckContent(t, term, 4, 0, "E")
588
589 WriteF(t, term, "\x1b[23mF") // clear italic
590 CheckAttrs(t, term, 5, 0, vt.Plain)
591 CheckContent(t, term, 5, 0, "F")
592
593 WriteF(t, term, "\x1b[3;4mG") // simple underline
594 CheckAttrs(t, term, 6, 0, vt.Italic|vt.Underline)
595 CheckContent(t, term, 6, 0, "G")
596
597 WriteF(t, term, "\x1b[21mH") // double underline (ECMA)
598 CheckAttrs(t, term, 7, 0, vt.Italic|vt.DoubleUnderline)
599 CheckContent(t, term, 7, 0, "H")
600
601 WriteF(t, term, "\x1b[4mI") // simple underline
602 CheckAttrs(t, term, 8, 0, vt.Italic|vt.Underline)
603 CheckContent(t, term, 8, 0, "I")
604
605 WriteF(t, term, "\x1b[4:2mJ") // double underline
606 CheckAttrs(t, term, 9, 0, vt.Italic|vt.DoubleUnderline)
607 CheckContent(t, term, 9, 0, "J")
608
609 WriteF(t, term, "\x1b[4:3mK") // curly underline
610 CheckAttrs(t, term, 10, 0, vt.Italic|vt.CurlyUnderline)
611 CheckContent(t, term, 10, 0, "K")
612
613 WriteF(t, term, "\x1b[4:4mL") // dotted underline
614 CheckAttrs(t, term, 11, 0, vt.Italic|vt.DottedUnderline)
615 CheckContent(t, term, 11, 0, "L")
616
617 WriteF(t, term, "\x1b[4:5mM") // dashed underline
618 CheckAttrs(t, term, 12, 0, vt.Italic|vt.DashedUnderline)
619 CheckContent(t, term, 12, 0, "M")
620
621 WriteF(t, term, "\x1b[4:9mN") // junk treats as plain underline
622 CheckAttrs(t, term, 13, 0, vt.Italic|vt.Underline)
623 CheckContent(t, term, 13, 0, "N")
624
625 WriteF(t, term, "\x1b[4:5;24mO") // clustering, clear it
626 CheckAttrs(t, term, 14, 0, vt.Italic)
627 CheckContent(t, term, 14, 0, "O")
628
629 WriteF(t, term, "\x1b[0;9;7;53mP") // clear, strike-through, reverse, over-lined
630 CheckAttrs(t, term, 15, 0, vt.StrikeThrough|vt.Reverse|vt.Overline)
631 CheckContent(t, term, 15, 0, "P")
632
633 WriteF(t, term, "\x1b[5;27;29;55mQ")
634 CheckAttrs(t, term, 16, 0, vt.Blink)
635 CheckContent(t, term, 16, 0, "Q")
636
637 WriteF(t, term, "\x1b[25mR")
638 CheckAttrs(t, term, 17, 0, vt.Plain)
639 CheckContent(t, term, 17, 0, "R")
640 }
641
642 // TestSgrColor8 tests simple ECMA 48 ANSI color (only 8 possible color values.)
643 func TestSgrColor8(t *testing.T) {
644 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24})
645 defer MustClose(t, term)
646 MustStart(t, term)
647
648 WriteF(t, term, "\x1b[36;42m\x1b#8")
649 CheckColors(t, term, 0, 0, color.Teal, color.Green)
650
651 WriteF(t, term, "\x1b[H\x1b[39mA")
652 CheckColors(t, term, 0, 0, color.Silver, color.Green)
653
654 WriteF(t, term, "\x1b[49mA")
655 CheckColors(t, term, 1, 0, color.Silver, color.Black)
656
657 // verify zero clears colors, first write some non zero colors
658 WriteF(t, term, "\x1b[36;42mD")
659 CheckColors(t, term, 2, 0, color.Teal, color.Green)
660
661 // then send zero, should go to default colors
662 WriteF(t, term, "\x1b[0mA")
663 CheckColors(t, term, 3, 0, color.Silver, color.Black)
664 }
665
666 // TestSgrMalformedParam verifies that malformed SGR parameters are ignored
667 // individually without dropping the rest of the sequence.
668 func TestSgrMalformedParam(t *testing.T) {
669 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24})
670 defer MustClose(t, term)
671 MustStart(t, term)
672
673 WriteF(t, term, "\x1b[31;<mA")
674 CheckColors(t, term, 0, 0, color.Maroon, color.Black)
675
676 WriteF(t, term, "\x1b[31;<;32mB")
677 CheckColors(t, term, 1, 0, color.Green, color.Black)
678 }
679
680 // TestSgrColor256 tests simple ECMA 48 ANSI color (256 possible color values.)
681 func TestSgrColor256(t *testing.T) {
682 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24}, vt.MockOptColors(256))
683 defer MustClose(t, term)
684 MustStart(t, term)
685
686 // foreground
687 WriteF(t, term, "\x1b[38:5:6m\x1b[42m\x1b#8")
688 CheckColors(t, term, 0, 0, color.Teal, color.Green)
689 WriteF(t, term, "\x1b[38;5;5m\x1b[42mA")
690 CheckColors(t, term, 0, 0, color.XTerm5, color.Green)
691
692 WriteF(t, term, "\x1b[38;5;212;1mB")
693 CheckColors(t, term, 1, 0, color.XTerm212, color.Green)
694 CheckAttrs(t, term, 1, 0, vt.Bold)
695
696 // background
697 WriteF(t, term, "\x1b[48;5;2mA")
698 CheckColors(t, term, 2, 0, color.XTerm212, color.XTerm2)
699 CheckAttrs(t, term, 2, 0, vt.Bold)
700
701 WriteF(t, term, "\x1b[48:5:134;2mC")
702 CheckColors(t, term, 3, 0, color.XTerm212, color.XTerm134)
703 CheckAttrs(t, term, 3, 0, vt.Dim)
704
705 // mix background and foreground using colons
706 WriteF(t, term, "\x1b[48:5:135;38:5:22mC")
707 CheckColors(t, term, 4, 0, color.XTerm22, color.XTerm135)
708
709 // and using semicolons
710 WriteF(t, term, "\x1b[48;5;136;38;5;23mC")
711 CheckColors(t, term, 5, 0, color.XTerm23, color.XTerm136)
712
713 // underline colors - it uses the same parser so we won't check
714 // all the variations
715 WriteF(t, term, "\x1b[58;5;21;4mC")
716 VerifyF(t, term.GetCell(Coord{X: 6, Y: 0}).S.Uc() == color.XTerm21, "underline color is wrong")
717
718 WriteF(t, term, "\x1b[H\x1b[39;49mA")
719 CheckColors(t, term, 0, 0, color.Silver, color.Black)
720
721 // verify zero clears colors, first write some non zero colors
722 WriteF(t, term, "\x1b[H\x1b[36;42mA")
723 CheckColors(t, term, 0, 0, color.Teal, color.Green)
724
725 // then send zero, should go to default colors
726 WriteF(t, term, "\x1b[H\x1b[0mA")
727 CheckColors(t, term, 0, 0, color.Silver, color.Black)
728
729 // fuzz some things
730 WriteF(t, term, "\x1b[m\x1b[H")
731 WriteF(t, term, "\x1b[38:3m")
732 WriteF(t, term, "\x1b[38;3m")
733 WriteF(t, term, "\x1b[38;2m")
734 WriteF(t, term, "\x1b[38;2:300m")
735 WriteF(t, term, "\x1b[38;5m")
736 WriteF(t, term, "\x1b[38;5:300m")
737 WriteF(t, term, "\x1b[38:2m")
738 WriteF(t, term, "\x1b[38:5m")
739 WriteF(t, term, "\x1b[38:2;1;1;1m")
740 WriteF(t, term, "\x1b[38:2:1:1m")
741 WriteF(t, term, "A")
742 CheckColors(t, term, 0, 0, color.Silver, color.Black)
743 }
744
745 // TestSgrColorRGB tests simple ECMA 48 ANSI color (full 24-bit color.)
746 func TestSgrColorRGB(t *testing.T) {
747 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24}, vt.MockOptColors(1<<24))
748 defer MustClose(t, term)
749 MustStart(t, term)
750
751 // foreground
752 WriteF(t, term, "\x1b[m\x1b[H\x1b[38:2:255:0:0mA")
753 CheckColors(t, term, 0, 0, color.NewRGBColor(255, 0, 0), color.Black)
754
755 WriteF(t, term, "\x1b[m\x1b[H\x1b[38;2;2;0;0mA")
756 CheckColors(t, term, 0, 0, color.NewRGBColor(2, 0, 0), color.Black)
757
758 // background
759 WriteF(t, term, "\x1b[m\x1b[H\x1b[48:2:1:2:3mA")
760 CheckColors(t, term, 0, 0, color.Silver, color.NewRGBColor(1, 2, 3))
761
762 WriteF(t, term, "\x1b[m\x1b[H\x1b[48;2;4;5;6mA")
763 CheckColors(t, term, 0, 0, color.Silver, color.NewRGBColor(4, 5, 6))
764
765 // full colors
766 WriteF(t, term, "\x1b[m\x1b[H\x1b[38;2;99;88;77;48;2;4;5;6;58;2;99;98;91;1mA")
767 CheckColors(t, term, 0, 0, color.NewRGBColor(99, 88, 77), color.NewRGBColor(4, 5, 6))
768 VerifyF(t, term.GetCell(Coord{X: 0, Y: 0}).S.Uc() == color.NewRGBColor(99, 98, 91), "underline color is wrong")
769 CheckAttrs(t, term, 0, 0, vt.Bold)
770 }
771
772 // TestTitles tests that we can set a window title.
773 func TestTitles(t *testing.T) {
774 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24})
775 defer MustClose(t, term)
776 MustStart(t, term)
777 WriteF(t, term, "\x1b]2;Test Application\x1b\\")
778 if s := term.GetTitle(); s != "Test Application" {
779 t.Errorf("wrong title: %q", s)
780 }
781
782 // test ST termination using legacy bell character
783 WriteF(t, term, "\x1b]2;Bell Ring\x07")
784 if s := term.GetTitle(); s != "Bell Ring" {
785 t.Errorf("wrong title: %q", s)
786 }
787
788 // 8-bit C1 controls are disabled by default.
789 WriteF(t, term, "\x9d2;Eight Bits\x9c")
790 if s := term.GetTitle(); s != "Bell Ring" {
791 t.Errorf("wrong title: %q", s)
792 }
793
794 // ESC SP G cannot enable 8-bit C1 controls unless the emulator permits them.
795 WriteF(t, term, "\x1b G\x9d2;Eight Bits\x9c")
796 if s := term.GetTitle(); s != "Bell Ring" {
797 t.Errorf("wrong title: %q", s)
798 }
799 }
800
801 func Test8BitControls(t *testing.T) {
802 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24}, vt.MockOpt8BitControls{})
803 defer MustClose(t, term)
804 MustStart(t, term)
805
806 WriteF(t, term, "\x9d2;Eight Bits\x9c")
807 if s := term.GetTitle(); s != "Eight Bits" {
808 t.Errorf("wrong title: %q", s)
809 }
810
811 WriteF(t, term, "\x1b F\x9d2;Disabled\x9c")
812 if s := term.GetTitle(); s != "Eight Bits" {
813 t.Errorf("wrong title: %q", s)
814 }
815
816 WriteF(t, term, "\x1b G\xc2\x9d2;UTF-8 Eight Bits\xc2\x9c")
817 if s := term.GetTitle(); s != "UTF-8 Eight Bits" {
818 t.Errorf("wrong title: %q", s)
819 }
820
821 WriteF(t, term, "\xc2\x9b2;3H")
822 CheckPos(t, term, 2, 1)
823
824 WriteF(t, term, "\x1b F\x1b[H\xc2\x9b3;4H")
825 CheckPos(t, term, 4, 0)
826
827 WriteF(t, term, "\x1b G\x9b4;5H")
828 CheckPos(t, term, 4, 3)
829 }
830
831 func Test8BitControlsInString(t *testing.T) {
832 t.Run("DefaultDisabled", func(t *testing.T) {
833 term := vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 1})
834 defer MustClose(t, term)
835 MustStart(t, term)
836
837 WriteF(t, term, "\x1bPdiscard\x9cA\x07B")
838 CheckContent(t, term, 0, 0, "B")
839 CheckContent(t, term, 1, 0, "")
840
841 WriteF(t, term, "\x1b[H\x1bPdiscard\xc2\x9cC\x07D")
842 CheckContent(t, term, 0, 0, "D")
843 CheckContent(t, term, 1, 0, "")
844 })
845
846 t.Run("Enabled", func(t *testing.T) {
847 term := vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 1}, vt.MockOpt8BitControls{})
848 defer MustClose(t, term)
849 MustStart(t, term)
850
851 WriteF(t, term, "\x1bPdiscard\x9cA")
852 CheckContent(t, term, 0, 0, "A")
853
854 WriteF(t, term, "\x1b[H\x1bPdiscard\xc2\x9cB")
855 CheckContent(t, term, 0, 0, "B")
856 })
857
858 t.Run("RuntimeDisabled", func(t *testing.T) {
859 term := vt.NewMockTerm(vt.MockOptSize{X: 8, Y: 1}, vt.MockOpt8BitControls{})
860 defer MustClose(t, term)
861 MustStart(t, term)
862
863 WriteF(t, term, "\x1b F\x1bPdiscard\x9cA\x07B")
864 CheckContent(t, term, 0, 0, "B")
865 CheckContent(t, term, 1, 0, "")
866
867 WriteF(t, term, "\x1b G\x1b[H\x1bPdiscard\xc2\x9cC")
868 CheckContent(t, term, 0, 0, "C")
869 })
870 }
871
872 // TestResize tests resizing the terminal
873 func TestResize(t *testing.T) {
874 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24})
875 defer MustClose(t, term)
876 MustStart(t, term)
877
878 // with E, and enable notifications
879 WriteF(t, term, "\x1b#8\x1b[?2048h")
880 resizeQ := make(chan bool, 1)
881 term.NotifyResize(resizeQ)
882
883 term.SetSize(Coord{X: 132, Y: 24})
884 if sz, err := term.WindowSize(); err != nil || sz.Height != 24 || sz.Width != 132 {
885 t.Errorf("resize did not occur: %v %d %d", err, sz.Height, sz.Width)
886 }
887 for y := range Row(24) {
888 for x := range Col(80) {
889 if s := string(term.GetCell(Coord{X: x, Y: y}).C); s != "E" {
890 t.Errorf("resize content at %d,%d wrong: %q", x, y, s)
891 }
892 }
893 }
894 for y := range Row(24) {
895 for x := Col(80); x < 132; x++ {
896 if s := string(term.GetCell(Coord{X: x, Y: y}).C); s != "" {
897 t.Errorf("resize content at %d,%d wrong: %q", x, y, s)
898 }
899 }
900 }
901 select {
902 case <-resizeQ:
903 case <-time.After(time.Millisecond * 100):
904 t.Errorf("resize signal failure")
905 }
906 // this forces a flush of the write queue
907 term.Write([]byte{})
908
909 buf := make([]byte, 128)
910 n, err := term.Read(buf)
911 if err != nil {
912 t.Errorf("failed read: %v", err)
913 }
914 result := string(buf[:n])
915 if result != "\x1b[48;24;132;0;0t" && result != "\x1b[48;24;132t" {
916 t.Errorf("key responses failed: %q", result)
917 }
918 }
919
920 // TestCSISetSize tests that CSI 8;h;wt updates mock terminal size.
921 func TestCSISetSize(t *testing.T) {
922 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24})
923 defer MustClose(t, term)
924 MustStart(t, term)
925
926 resizeQ := make(chan bool, 1)
927 term.NotifyResize(resizeQ)
928
929 WriteF(t, term, "\x1b[8;32;128t")
930 if sz, err := term.WindowSize(); err != nil || sz.Height != 32 || sz.Width != 128 {
931 t.Errorf("resize did not occur: %v %d %d", err, sz.Height, sz.Width)
932 }
933
934 select {
935 case <-resizeQ:
936 case <-time.After(time.Millisecond * 100):
937 t.Errorf("resize signal failure")
938 }
939 }
940
941 // TestTabs tests tab stop functionality.
942 func TestTabs(t *testing.T) {
943 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24})
944 defer MustClose(t, term)
945 MustStart(t, term)
946
947 WriteF(t, term, "a\tC")
948 if s := string(term.GetCell(Coord{X: 8, Y: 0}).C); s != "C" {
949 t.Errorf("tab did not work: %q", s)
950 }
951 WriteF(t, term, "\x1b[3I")
952 if x := term.Pos().X; x != 32 {
953 t.Errorf("wrong position %d", x)
954 }
955
956 WriteF(t, term, "\x1b[2Z")
957 if x := term.Pos().X; x != 16 {
958 t.Errorf("wrong position %d", x)
959 }
960
961 WriteF(t, term, "\x1b[3g") // clear all tabs
962 WriteF(t, term, "\x1b[I") // one tab, should go to right margin
963 if x := term.Pos().X; x != 79 {
964 t.Errorf("wrong position: %d", x)
965 }
966
967 WriteF(t, term, "\x1b[Z")
968 if x := term.Pos().X; x != 0 {
969 t.Errorf("wrong position: %d", x)
970 }
971
972 // reset tabs
973 WriteF(t, term, "\x1b[?5W")
974
975 WriteF(t, term, "\t")
976 if x := term.Pos().X; x != 8 {
977 t.Errorf("wrong position: %d", x)
978 }
979 // clear this position, advance one
980 WriteF(t, term, "\x1b[gA")
981 if x := term.Pos().X; x != 9 {
982 t.Errorf("wrong position: %d", x)
983 }
984 WriteF(t, term, "\x1bH")
985 WriteF(t, term, "\t")
986 if x := term.Pos().X; x != 16 {
987 t.Errorf("wrong position: %d", x)
988 }
989 WriteF(t, term, "\x1b[Z")
990 if x := term.Pos().X; x != 9 {
991 t.Errorf("wrong position: %d", x)
992 }
993 WriteF(t, term, "\x1b[Z")
994 if x := term.Pos().X; x != 0 {
995 t.Errorf("wrong position: %d", x)
996 }
997 WriteF(t, term, "\x1b[1;10H") // goto position 9
998 if x := term.Pos().X; x != 9 {
999 t.Errorf("wrong position: %d", x)
1000 }
1001 // delete this one (do it twice to exercise the does not exist flow)
1002 WriteF(t, term, "\x1b[0g")
1003 WriteF(t, term, "\x1b[0g")
1004
1005 // advance to next tab, then back, we should go to 0
1006 WriteF(t, term, "\t")
1007 if x := term.Pos().X; x != 16 {
1008 t.Errorf("wrong position: %d", x)
1009 }
1010 WriteF(t, term, "\x1b[Z")
1011 if x := term.Pos().X; x != 0 {
1012 t.Errorf("wrong position: %d", x)
1013 }
1014 WriteF(t, term, "\x1b[20I")
1015 WriteF(t, term, "\t\t")
1016 if pos := term.Pos(); pos.X != 79 || pos.Y != 0 {
1017 t.Errorf("wrong position: %d %d", pos.X, pos.Y)
1018 }
1019
1020 // now backwards
1021 WriteF(t, term, "\x1b[20Z")
1022 WriteF(t, term, "\x1b[Z")
1023 if pos := term.Pos(); pos.X != 0 || pos.Y != 0 {
1024 t.Errorf("wrong position: %d %d", pos.X, pos.Y)
1025 }
1026 }
1027
1028 func TestVerticalPos(t *testing.T) {
1029 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24})
1030 defer MustClose(t, term)
1031 MustStart(t, term)
1032
1033 WriteF(t, term, "\x1b[2;2H")
1034 WriteF(t, term, "\x1b[10d")
1035 if pos := term.Pos(); pos.X != 1 || pos.Y != 9 {
1036 t.Errorf("wrong position: %d %d", pos.X, pos.Y)
1037 }
1038 WriteF(t, term, "\x1b[2e")
1039 if pos := term.Pos(); pos.X != 1 || pos.Y != 11 {
1040 t.Errorf("wrong position; %d %d", pos.X, pos.Y)
1041 }
1042 WriteF(t, term, "\x1b[e")
1043 if pos := term.Pos(); pos.X != 1 || pos.Y != 12 {
1044 t.Errorf("wrong position; %d %d", pos.X, pos.Y)
1045 }
1046 WriteF(t, term, "\x1b[0e")
1047 if pos := term.Pos(); pos.X != 1 || pos.Y != 13 {
1048 t.Errorf("wrong position; %d %d", pos.X, pos.Y)
1049 }
1050 WriteF(t, term, "\x1b[50d")
1051 if pos := term.Pos(); pos.X != 1 || pos.Y != 23 {
1052 t.Errorf("wrong position; %d %d", pos.X, pos.Y)
1053 }
1054 WriteF(t, term, "\x1b[50e")
1055 if pos := term.Pos(); pos.X != 1 || pos.Y != 23 {
1056 t.Errorf("wrong position; %d %d", pos.X, pos.Y)
1057 }
1058 WriteF(t, term, "\x1b[0d")
1059 if pos := term.Pos(); pos.X != 1 || pos.Y != 0 {
1060 t.Errorf("wrong position; %d %d", pos.X, pos.Y)
1061 }
1062 WriteF(t, term, "\x1b[10d")
1063 if pos := term.Pos(); pos.X != 1 || pos.Y != 9 {
1064 t.Errorf("wrong position; %d %d", pos.X, pos.Y)
1065 }
1066 WriteF(t, term, "\x1b[1d")
1067 if pos := term.Pos(); pos.X != 1 || pos.Y != 0 {
1068 t.Errorf("wrong position; %d %d", pos.X, pos.Y)
1069 }
1070 }
1071
1072 func TestSaveCursorPosition(t *testing.T) {
1073 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24})
1074 defer MustClose(t, term)
1075 MustStart(t, term)
1076
1077 WriteF(t, term, "\x1b[1;1H\x1b[J")
1078 WriteF(t, term, "\x1b[1;5H")
1079 WriteF(t, term, "A")
1080 WriteF(t, term, "\x1b7")
1081 WriteF(t, term, "\x1b[1;1H")
1082 WriteF(t, term, "B")
1083 WriteF(t, term, "\x1b8")
1084 WriteF(t, term, "X")
1085
1086 CheckContent(t, term, 0, 0, "B")
1087 CheckContent(t, term, 4, 0, "A")
1088 CheckContent(t, term, 5, 0, "X")
1089 }
1090
1091 func TestSaveCursorWrapState(t *testing.T) {
1092 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24})
1093 defer MustClose(t, term)
1094 MustStart(t, term)
1095
1096 WriteF(t, term, "\x1b[1;1H\x1b[J")
1097 WriteF(t, term, "\x1b[80G")
1098 WriteF(t, term, "A")
1099 WriteF(t, term, "\x1b7") // save cursor
1100 WriteF(t, term, "\x1b[1;1H")
1101 WriteF(t, term, "B")
1102 WriteF(t, term, "\x1b8") // restore cursor
1103 WriteF(t, term, "X")
1104 CheckContent(t, term, 0, 0, "B")
1105 CheckContent(t, term, 79, 0, "A")
1106 CheckContent(t, term, 0, 1, "X")
1107 }
1108
1109 func TestSaveCursorSgr(t *testing.T) {
1110 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24})
1111 defer MustClose(t, term)
1112 MustStart(t, term)
1113 WriteF(t, term, "\x1b[1;1H\x1b[0J")
1114 WriteF(t, term, "\x1b[1;4;33;44m")
1115 WriteF(t, term, "A")
1116 CheckPos(t, term, 1, 0)
1117 WriteF(t, term, "\x1b7")
1118 CheckPos(t, term, 1, 0)
1119 WriteF(t, term, "\x1b[0m")
1120 CheckPos(t, term, 1, 0)
1121 WriteF(t, term, "BE")
1122 CheckPos(t, term, 3, 0)
1123 WriteF(t, term, "\x1b8")
1124 CheckPos(t, term, 1, 0)
1125 WriteF(t, term, "X")
1126 CheckContent(t, term, 0, 0, "A")
1127 CheckContent(t, term, 1, 0, "X")
1128 CheckContent(t, term, 2, 0, "E")
1129 CheckAttrs(t, term, 0, 0, vt.Bold|vt.Underline)
1130 CheckAttrs(t, term, 1, 0, vt.Bold|vt.Underline)
1131 CheckAttrs(t, term, 2, 0, vt.Plain)
1132 CheckColors(t, term, 0, 0, color.XTerm3, color.XTerm4)
1133 CheckColors(t, term, 1, 0, color.XTerm3, color.XTerm4)
1134 CheckColors(t, term, 2, 0, color.Silver, color.Black)
1135 }
1136
1137 func TestReset(t *testing.T) {
1138 term := vt.NewMockTerm(vt.MockOptSize{X: 80, Y: 24})
1139 defer MustClose(t, term)
1140 MustStart(t, term)
1141
1142 // write a bunch of stuff to create state (so we can verify it gets reset)
1143 WriteF(t, term, "\x1b[1;4;33;44m")
1144 WriteF(t, term, "\x1b#8")
1145 WriteF(t, term, "\x1b[1;80HX")
1146 WriteF(t, term, "\x1b7") // save cursor
1147 WriteF(t, term, "\x1b[?7l") // disable automargin
1148 WriteF(t, term, "\x1bc")
1149 CheckPos(t, term, 0, 0)
1150 for row := range Row(24) {
1151 for col := range Col(80) {
1152 CheckAttrs(t, term, col, row, vt.Plain)
1153 CheckContent(t, term, col, row, "")
1154 CheckColors(t, term, col, row, color.Silver, color.Black)
1155 }
1156 }
1157 WriteF(t, term, "\x1b8") // restore cursor
1158 CheckPos(t, term, 0, 0)
1159 WriteF(t, term, "X")
1160 CheckAttrs(t, term, 0, 0, vt.Plain)
1161 CheckContent(t, term, 0, 0, "X")
1162 CheckColors(t, term, 0, 0, color.Silver, color.Black)
1163 WriteF(t, term, "\x1b[?7$p")
1164
1165 // verify mode reset
1166 want := "\x1b[?7;1$y"
1167 buf := make([]byte, 128)
1168 n, err := term.Read(buf)
1169 if err != nil {
1170 t.Errorf("failed read: %v", err)
1171 }
1172 result := string(buf[:n])
1173 VerifyF(t, result == want, "wrong mode: %q != %q", result, want)
1174 }
1175
1176 // backendBox makes a backend box, filled with increasing letters (modulo 16)
1177 func backendBox(t *testing.T, mb vt.MockBackend, tl Coord, br Coord, attr Attr) {
1178 t.Helper()
1179 style := vt.BaseStyle.WithAttr(attr)
1180 hex := []rune{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P'}
1181 AssertF(t, len(hex) == 16, "wrong hex string")
1182 i := 0
1183 for row := tl.Y; row <= br.Y; row++ {
1184 for col := tl.X; col <= br.X; col++ {
1185 mb.Put(Coord{Y: row, X: col}, vt.Cell{C: string(hex[i%16]), W: 1, S: style})
1186 i++
1187 }
1188 }
1189 }
1190
1191 func TestBackendBlit(t *testing.T) {
1192 term := vt.NewMockTerm(vt.MockOptSize{X: 10, Y: 5})
1193 defer MustClose(t, term)
1194 MustStart(t, term)
1195
1196 mb := term.Backend()
1197 AssertF(t, mb != nil, "backend is nil")
1198
1199 backendBox(t, mb, Coord{X: 0, Y: 0}, Coord{X: 1, Y: 1}, vt.Bold)
1200 CheckContent(t, term, 0, 0, "A")
1201 CheckContent(t, term, 1, 0, "B")
1202 CheckContent(t, term, 2, 0, "")
1203 CheckContent(t, term, 0, 1, "C")
1204 CheckContent(t, term, 1, 1, "D")
1205 CheckContent(t, term, 2, 1, "")
1206 CheckContent(t, term, 0, 2, "")
1207 CheckContent(t, term, 1, 2, "")
1208 CheckContent(t, term, 2, 2, "")
1209 CheckAttrs(t, term, 2, 2, vt.Plain)
1210
1211 // blit the entire box down and right 1
1212 mb.(vt.Blitter).Blit(Coord{X: 0, Y: 0}, Coord{X: 1, Y: 1}, Coord{X: 2, Y: 2})
1213 CheckContent(t, term, 0, 0, "A")
1214 CheckContent(t, term, 1, 0, "B")
1215 CheckContent(t, term, 2, 0, "")
1216 CheckContent(t, term, 0, 1, "C")
1217 CheckContent(t, term, 1, 1, "A")
1218 CheckContent(t, term, 2, 1, "B")
1219 CheckContent(t, term, 0, 2, "")
1220 CheckContent(t, term, 1, 2, "C")
1221 CheckContent(t, term, 2, 2, "D")
1222
1223 // spot check attributes
1224 CheckAttrs(t, term, 2, 2, vt.Bold)
1225 }
1226
1227 func TestBackendBlitReverse(t *testing.T) {
1228 term := vt.NewMockTerm(vt.MockOptSize{X: 10, Y: 5})
1229 defer MustClose(t, term)
1230 MustStart(t, term)
1231
1232 mb := term.Backend()
1233 AssertF(t, mb != nil, "backend is nil")
1234
1235 backendBox(t, mb, Coord{X: 1, Y: 1}, Coord{X: 2, Y: 2}, vt.Bold)
1236 CheckContent(t, term, 0, 0, "")
1237 CheckContent(t, term, 1, 0, "")
1238 CheckContent(t, term, 2, 0, "")
1239 CheckContent(t, term, 0, 1, "")
1240 CheckContent(t, term, 1, 1, "A")
1241 CheckContent(t, term, 2, 1, "B")
1242 CheckContent(t, term, 0, 2, "")
1243 CheckContent(t, term, 1, 2, "C")
1244 CheckContent(t, term, 2, 2, "D")
1245 CheckAttrs(t, term, 0, 0, vt.Plain)
1246
1247 // blit the entire box down and right 1
1248 mb.(vt.Blitter).Blit(Coord{X: 1, Y: 1}, Coord{X: 0, Y: 0}, Coord{X: 2, Y: 2})
1249 CheckContent(t, term, 0, 0, "A")
1250 CheckContent(t, term, 1, 0, "B")
1251 CheckContent(t, term, 2, 0, "")
1252 CheckContent(t, term, 0, 1, "C")
1253 CheckContent(t, term, 1, 1, "D")
1254 CheckContent(t, term, 2, 1, "B")
1255 CheckContent(t, term, 0, 2, "")
1256 CheckContent(t, term, 1, 2, "C")
1257 CheckContent(t, term, 2, 2, "D")
1258
1259 // spot check attributes
1260 CheckAttrs(t, term, 2, 2, vt.Bold)
1261 }
1262
1263 func TestGraphemeCluster(t *testing.T) {
1264 term := vt.NewMockTerm(vt.MockOptSize{X: 10, Y: 5})
1265 defer MustClose(t, term)
1266 MustStart(t, term)
1267
1268 WriteF(t, term, "\x1b[H")
1269 // grapheme clustering is off
1270 WriteF(t, term, "🇨🇭") // flag + regional indicator C + regional indicator H
1271 WriteF(t, term, "A")
1272
1273 // we advanced by four columns (two wide emoji), and a single character
1274 CheckPos(t, term, 5, 0)
1275 CheckContent(t, term, 0, 0, "\U0001f1e8") // regional indicator C
1276 CheckContent(t, term, 1, 0, "") // empty
1277 CheckContent(t, term, 2, 0, "\U0001f1ed") // regional indicator H
1278 CheckContent(t, term, 3, 0, "") // empty
1279 CheckContent(t, term, 4, 0, "A") // empty
1280
1281 // now turn on grapheme clustering
1282 WriteF(t, term, "\x1b[?2027h")
1283 WriteF(t, term, "\x1b[H\x1b[J")
1284 CheckPos(t, term, 0, 0)
1285 WriteF(t, term, "\U0001f1e8\U0001f1ed")
1286 CheckPos(t, term, 2, 0)
1287 WriteF(t, term, "A")
1288 CheckPos(t, term, 3, 0)
1289 CheckContent(t, term, 0, 0, "🇨🇭")
1290 CheckContent(t, term, 1, 0, "")
1291 CheckContent(t, term, 2, 0, "A")
1292
1293 // lets also verify it works with automargin
1294 // RECALL: Maximum width is 10
1295 WriteF(t, term, "\x1b[7h") // should already be on
1296 WriteF(t, term, "\x1b[1;10H") // last position in first row
1297 WriteF(t, term, "🇨🇭A") // flag + regional indicator C + regional indicator H
1298 CheckPos(t, term, 1, 1)
1299 CheckContent(t, term, 9, 0, "🇨🇭")
1300 CheckContent(t, term, 0, 1, "A")
1301 }
1302
1303 func TestEraseAbove(t *testing.T) {
1304 term := vt.NewMockTerm(vt.MockOptSize{X: 10, Y: 5})
1305 defer MustClose(t, term)
1306 MustStart(t, term)
1307
1308 WriteF(t, term, "\x1b#8")
1309 WriteF(t, term, "\x1b[3;5H")
1310
1311 WriteF(t, term, "\x1b[31;1;42m") // set some colors and bold
1312 WriteF(t, term, "\x1b[1J")
1313 // cursor is at 4,2
1314 for row := range Row(5) {
1315 for col := range Col(10) {
1316 if row < 2 || row == 2 && col < 5 {
1317 CheckContent(t, term, col, row, "")
1318 CheckAttrs(t, term, col, row, vt.Plain)
1319 CheckColors(t, term, col, row, color.XTerm1, color.XTerm2)
1320 } else {
1321 CheckContent(t, term, col, row, "E")
1322 CheckAttrs(t, term, col, row, vt.Plain)
1323 CheckColors(t, term, col, row, color.Silver, color.Black)
1324 }
1325 }
1326 }
1327 }
1328
1329 func TestEraseLine(t *testing.T) {
1330 term := vt.NewMockTerm(vt.MockOptSize{X: 10, Y: 5})
1331 defer MustClose(t, term)
1332 MustStart(t, term)
1333
1334 WriteF(t, term, "\x1b#8")
1335
1336 // erase to end
1337 WriteF(t, term, "\x1b[31;1;42m") // set some colors and bold
1338 WriteF(t, term, "\x1b[1;5H")
1339 WriteF(t, term, "\x1b[0K")
1340 // cursor is at 4,0
1341 CheckPos(t, term, 4, 0)
1342 for col := range Col(10) {
1343 row := Row(0)
1344 if col < 4 {
1345 CheckContent(t, term, col, row, "E")
1346 CheckAttrs(t, term, col, row, vt.Plain)
1347 CheckColors(t, term, col, row, color.Silver, color.Black)
1348 } else {
1349 CheckContent(t, term, col, row, "")
1350 CheckAttrs(t, term, col, row, vt.Plain)
1351 CheckColors(t, term, col, row, color.XTerm1, color.XTerm2)
1352 }
1353 }
1354 CheckPos(t, term, 4, 0)
1355
1356 // erase to beginning
1357 WriteF(t, term, "\x1b[2;5H")
1358 WriteF(t, term, "\x1b[1K")
1359 // cursor is at 4,1
1360 CheckPos(t, term, 4, 1)
1361 for col := range Col(10) {
1362 row := Row(1)
1363 if col > 4 {
1364 CheckContent(t, term, col, row, "E")
1365 CheckAttrs(t, term, col, row, vt.Plain)
1366 CheckColors(t, term, col, row, color.Silver, color.Black)
1367 } else {
1368 CheckContent(t, term, col, row, "")
1369 CheckAttrs(t, term, col, row, vt.Plain)
1370 CheckColors(t, term, col, row, color.XTerm1, color.XTerm2)
1371 }
1372 }
1373 CheckPos(t, term, 4, 1)
1374
1375 // erase entire line
1376 WriteF(t, term, "\x1b[3;5H")
1377 WriteF(t, term, "\x1b[2K")
1378 // cursor is at 4,2
1379 CheckPos(t, term, 4, 2)
1380 for col := range Col(10) {
1381 row := Row(2)
1382 CheckContent(t, term, col, row, "")
1383 CheckAttrs(t, term, col, row, vt.Plain)
1384 CheckColors(t, term, col, row, color.XTerm1, color.XTerm2)
1385 }
1386 CheckPos(t, term, 4, 2)
1387 }
1388
1389 // TestNewLineScroll tests scrolling with a new line.
1390 // This is one of the most fundamental operations for a terminal.
1391 func TestNewLineScroll(t *testing.T) {
1392 term := vt.NewMockTerm(vt.MockOptSize{X: 10, Y: 5})
1393 defer MustClose(t, term)
1394 MustStart(t, term)
1395
1396 WriteF(t, term, "\x1b[H\x1b[J") // home and clear
1397 WriteF(t, term, "\x1b[1;1HA")
1398 WriteF(t, term, "\x1b[2;1HB")
1399 WriteF(t, term, "\x1b[5;1HC") // first column on last row
1400 CheckContent(t, term, 0, 4, "C")
1401 WriteF(t, term, "\n") // new line should scroll
1402 CheckPos(t, term, 1, 4)
1403 CheckContent(t, term, 0, 0, "B")
1404 CheckContent(t, term, 0, 1, "")
1405 CheckContent(t, term, 0, 4, "")
1406 CheckContent(t, term, 0, 3, "C")
1407 }
1408
1409 // TestNewLineScrollNoBlitter tests scrolling with a new line,
1410 // using the fallback copy for backends without Blit support.
1411 func TestNewLineScrollNoBlitter(t *testing.T) {
1412 term := vt.NewMockTerm(vt.MockOptSize{X: 10, Y: 5}, vt.MockOptNoBlit{})
1413 defer MustClose(t, term)
1414 MustStart(t, term)
1415
1416 WriteF(t, term, "\x1b[H\x1b[J") // home and clear
1417 WriteF(t, term, "\x1b[1;1HA")
1418 WriteF(t, term, "\x1b[2;1HB")
1419 WriteF(t, term, "\x1b[5;1HC") // first column on last row
1420 CheckContent(t, term, 0, 4, "C")
1421 WriteF(t, term, "\n") // new line should scroll
1422 CheckPos(t, term, 1, 4)
1423 CheckContent(t, term, 0, 0, "B")
1424 CheckContent(t, term, 0, 1, "")
1425 CheckContent(t, term, 0, 4, "")
1426 CheckContent(t, term, 0, 3, "C")
1427 }
1428
1429 func TestNewLineModes(t *testing.T) {
1430 term := vt.NewMockTerm(vt.MockOptSize{X: 10, Y: 4})
1431 defer MustClose(t, term)
1432 MustStart(t, term)
1433 WriteF(t, term, "\x1b[H\x1b[J")
1434 WriteF(t, term, "ABC\n")
1435 CheckPos(t, term, 3, 1)
1436 term.KeyTap(vt.KeyEnter)
1437 WriteF(t, term, "\x1b[20h")
1438 term.KeyTap(vt.KeyEnter)
1439 WriteF(t, term, "DEF")
1440 CheckPos(t, term, 6, 1)
1441 WriteF(t, term, "\n")
1442 CheckPos(t, term, 0, 2)
1443 WriteF(t, term, "GHI")
1444 CheckPos(t, term, 3, 2)
1445 WriteF(t, term, "\x1b[20l")
1446 WriteF(t, term, "\n")
1447 CheckPos(t, term, 3, 3)
1448 term.KeyTap(vt.KeyEnter)
1449
1450 // |ABC_____|
1451 // |___DEF__|
1452 // |GHI_____|
1453 // |___c____|
1454 //
1455 // input stream contains \r\r\n\r
1456
1457 CheckContent(t, term, 0, 0, "A")
1458 CheckContent(t, term, 1, 0, "B")
1459 CheckContent(t, term, 2, 0, "C")
1460 CheckContent(t, term, 3, 0, "")
1461 CheckContent(t, term, 4, 0, "")
1462 CheckContent(t, term, 5, 0, "")
1463 CheckContent(t, term, 0, 1, "")
1464 CheckContent(t, term, 1, 1, "")
1465 CheckContent(t, term, 2, 1, "")
1466 CheckContent(t, term, 3, 1, "D")
1467 CheckContent(t, term, 4, 1, "E")
1468 CheckContent(t, term, 5, 1, "F")
1469 CheckContent(t, term, 0, 2, "G")
1470 CheckContent(t, term, 1, 2, "H")
1471 CheckContent(t, term, 2, 2, "I")
1472 CheckContent(t, term, 3, 2, "")
1473 CheckContent(t, term, 4, 2, "")
1474 CheckContent(t, term, 5, 2, "")
1475
1476 result := ReadF(t, term)
1477 want := "\r\r\n\r"
1478 VerifyF(t, result == want, "response incorrect: %q != %q", result, want)
1479 }
1480
1481 // TestScrollUp tests scrolling up. The cursor position is retained.
1482 func TestScrollUp(t *testing.T) {
1483 term := vt.NewMockTerm(vt.MockOptSize{X: 10, Y: 5})
1484 defer MustClose(t, term)
1485 MustStart(t, term)
1486
1487 WriteF(t, term, "\x1b[H\x1b[J") // home and clear
1488 WriteF(t, term, "\x1b[1;1HA")
1489 WriteF(t, term, "\x1b[2;1HB")
1490 WriteF(t, term, "\x1b[5;1HC") // first column on last row
1491 CheckContent(t, term, 0, 4, "C")
1492 WriteF(t, term, "\x1b[5;5H") // fifth column on last row
1493 CheckPos(t, term, 4, 4)
1494 WriteF(t, term, "\x1bD")
1495 CheckPos(t, term, 4, 4)
1496 CheckContent(t, term, 0, 0, "B")
1497 CheckContent(t, term, 0, 1, "")
1498 CheckContent(t, term, 0, 4, "")
1499 CheckContent(t, term, 0, 3, "C")
1500 WriteF(t, term, "\x1bE") // this is like a newline
1501 CheckPos(t, term, 0, 4)
1502 CheckContent(t, term, 0, 2, "C")
1503 CheckContent(t, term, 0, 3, "")
1504 CheckContent(t, term, 0, 4, "")
1505
1506 WriteF(t, term, "\x1b[H\x1bJ")
1507 WriteF(t, term, "\x1b[1;1HA")
1508 WriteF(t, term, "\x1b[2;1HB")
1509 WriteF(t, term, "\x1b[3;1HC")
1510 WriteF(t, term, "\x1b[4;1HD")
1511 WriteF(t, term, "\x1b[5;1HE")
1512 WriteF(t, term, "\x1b[3;3H")
1513 WriteF(t, term, "\x1b[3S") // scroll in place, leaves cursor where it is
1514 CheckContent(t, term, 0, 0, "D")
1515 CheckContent(t, term, 0, 1, "E")
1516 CheckContent(t, term, 0, 2, "")
1517 CheckContent(t, term, 0, 3, "")
1518 CheckContent(t, term, 0, 4, "")
1519 CheckPos(t, term, 2, 2)
1520 }
1521
1522 // TestScrollDown tests scrolling down. The cursor position is retained.
1523 func TestScrollDown(t *testing.T) {
1524 term := vt.NewMockTerm(vt.MockOptSize{X: 10, Y: 5})
1525 defer MustClose(t, term)
1526 MustStart(t, term)
1527
1528 WriteF(t, term, "\x1b[H\x1b[J") // home and clear
1529 WriteF(t, term, "\x1b[1;1HA")
1530 WriteF(t, term, "\x1b[2;1HB")
1531 WriteF(t, term, "\x1b[4;1HC") // first column on penultimate row
1532 WriteF(t, term, "\x1b[5;1HD") // first column on last row
1533 CheckContent(t, term, 0, 3, "C")
1534 CheckContent(t, term, 0, 4, "D")
1535 WriteF(t, term, "\x1b[1;5H") // fifth column on first row
1536 CheckPos(t, term, 4, 0)
1537 WriteF(t, term, "\x1bM")
1538 CheckPos(t, term, 4, 0)
1539 CheckContent(t, term, 0, 0, "")
1540 CheckContent(t, term, 0, 1, "A")
1541 CheckContent(t, term, 0, 2, "B")
1542 CheckContent(t, term, 0, 3, "")
1543 CheckContent(t, term, 0, 4, "C")
1544
1545 WriteF(t, term, "\x1b[H\x1bJ")
1546 WriteF(t, term, "\x1b[1;1HA")
1547 WriteF(t, term, "\x1b[2;1HB")
1548 WriteF(t, term, "\x1b[3;1HC")
1549 WriteF(t, term, "\x1b[4;1HD")
1550 WriteF(t, term, "\x1b[5;1HE")
1551 WriteF(t, term, "\x1b[3;3H")
1552 WriteF(t, term, "\x1b[3T") // scroll in place, leaves cursor where it is
1553 CheckContent(t, term, 0, 0, "")
1554 CheckContent(t, term, 0, 1, "")
1555 CheckContent(t, term, 0, 2, "")
1556 CheckContent(t, term, 0, 3, "A")
1557 CheckContent(t, term, 0, 4, "B")
1558 CheckPos(t, term, 2, 2)
1559 }