1package vt
2
3import (
4 "strings"
5 "testing"
6)
7
8func TestCharsetsAndRepeat(t *testing.T) {
9 tests := []struct {
10 name string
11 input string
12 want string
13 }{
14 {name: "G0 DEC special graphics draws lines", input: "\x1b(0lqqk\x1b(Bq", want: "┌──┐q"},
15 {name: "G1 via SO and SI", input: "\x1b)0q\x0eqx\x0fq", want: "q─│q"},
16 {name: "SO without G1 designation stays ASCII", input: "\x0eq\x0f", want: "q"},
17 {name: "chars outside the graphics range pass through", input: "\x1b(0Aa~", want: "A▒·"},
18 {name: "DECSC and DECRC restore charsets", input: "\x1b(0\x1b7\x1b(B\x1b8q", want: "─"},
19 {name: "DECSTR resets charsets", input: "\x1b(0q\x1b[!pq", want: "─q"},
20 {name: "RIS resets charsets", input: "\x1b(0\x1bcq", want: "q"},
21 {name: "REP repeats the last graphic char", input: "\x1b(0q\x1b[4b", want: "─────"},
22 {name: "REP defaults to one", input: "a\x1b[b", want: "aa"},
23 {name: "REP without a prior char is ignored", input: "\x1b[3bx", want: "x"},
24 {name: "REP after a control is ignored", input: "ab\r\x1b[2b", want: "ab"},
25 {name: "REP after another sequence is ignored", input: "a\x1b[1m\x1b[2b", want: "a"},
26 {name: "consecutive REP repeats once", input: "a\x1b[b\x1b[b", want: "aa"},
27 {name: "DECSC and DECRC restore SO invocation", input: "\x1b)0\x0e\x1b7\x0f\x1b8q", want: "─"},
28 {name: "leaving 1049 restores charsets", input: "\x1b(0\x1b[?1049h\x1b(B\x1b[?1049lq", want: "─"},
29 {name: "leaving 47 keeps current charsets", input: "\x1b(0\x1b[?47h\x1b(B\x1b[?47lq", want: "q"},
30 {name: "leaving 1047 keeps current charsets", input: "\x1b(0\x1b[?1047h\x1b(B\x1b[?1047lq", want: "q"},
31 {name: "DECSTR saved cursor restores home", input: "\x1b[1;5Hx\x1b[!p\x1b8y", want: "y x"},
32 {name: "partial charset escape across writes", input: "\x1b(", want: ""},
33 }
34 for _, tt := range tests {
35 t.Run(tt.name, func(t *testing.T) {
36 s := NewScreen(10, 3)
37 s.Write([]byte(tt.input))
38 if got := strings.TrimRight(lineText(s, 0), " "); got != tt.want {
39 t.Fatalf("line = %q, want %q", got, tt.want)
40 }
41 })
42 }
43}
44
45func TestCharsetDesignationSplitAcrossWrites(t *testing.T) {
46 s := NewScreen(10, 1)
47 s.Write([]byte("\x1b("))
48 s.Write([]byte("0q"))
49 if got := strings.TrimRight(lineText(s, 0), " "); got != "─" {
50 t.Fatalf("line = %q, want %q", got, "─")
51 }
52}
53
54func TestSoftResetCursorAppearance(t *testing.T) {
55 s := NewScreen(4, 1)
56 s.Write([]byte("\x1b[6 q\x1b[?25l\x1b[!p"))
57 if s.cursorStyleSet || s.cursorStyle != 0 || !s.cursorVisible {
58 t.Fatalf("cursor style=%d set=%v visible=%v, want defaults", s.cursorStyle, s.cursorStyleSet, s.cursorVisible)
59 }
60}
61
62func TestRepeatSplitAcrossWrites(t *testing.T) {
63 tests := []struct {
64 name string
65 writes []string
66 }{
67 {name: "split inside CSI", writes: []string{"a\x1b[", "2b"}},
68 {name: "empty write after lone ESC", writes: []string{"a\x1b", "", "[2b"}},
69 }
70 for _, tt := range tests {
71 t.Run(tt.name, func(t *testing.T) {
72 s := NewScreen(10, 1)
73 for _, w := range tt.writes {
74 s.Write([]byte(w))
75 }
76 if got := strings.TrimRight(lineText(s, 0), " "); got != "aaa" {
77 t.Fatalf("line = %q, want %q", got, "aaa")
78 }
79 })
80 }
81}
82
83func TestRepeatAfterFragmentedKittyAPCIsIgnored(t *testing.T) {
84 s := NewScreen(10, 1)
85 s.Write([]byte("a\x1b_G"))
86 s.Write([]byte("a=q,i=1;\x1b\\"))
87 s.Write([]byte("\x1b[2b"))
88 if got := strings.TrimRight(lineText(s, 0), " "); got != "a" {
89 t.Fatalf("line = %q, want %q", got, "a")
90 }
91}
92
93func TestRepeatIsBounded(t *testing.T) {
94 s := NewScreen(4, 2)
95 s.Write([]byte("x\x1b[999999999b"))
96 if got := lineText(s, 0); got != "xxxx" {
97 t.Fatalf("line = %q, want %q", got, "xxxx")
98 }
99}