light_csi_test.go
This file
Path
src/tui/light_csi_test.go
274 lines · 8.3 KiB · Go
Last changed by
Check escape sequence bytes before counting them (#4929)Junegunn Choibefore adoptionb563e20
History of this fileTake it
Drop unrecognized escape sequences instead of typing them (#4926)Junegunn Choibefore adoptionc009c10
lines 1–233why, one step back1package tui23import "testing"45// An unrecognized escape sequence must be consumed whole, CSI and string6// sequences alike. Consuming only part of one leaves the rest to be read as7// input and typed into the query.8func TestUnknownEscapeSequence(t *testing.T) {9 for _, c := range []struct {10 sequence string11 event EventType12 size int13 }{14 // Key encodings fzf does not implement15 {"\x1b[97;5u", Invalid, 7},16 {"\x1b[70;5u", Invalid, 7}, // not Home, which its prefix \e[7 matches17 {"\x1b[42;5u", Invalid, 7}, // nor End18 {"\x1b[4;5~", Invalid, 6},19 {"\x1b[127;5u", Invalid, 8},20 {"\x1b[27;5;127~", Invalid, 11},21 {"\x1b[57441;1u", Invalid, 10},22 {"\x1b\x1b[97;5u", Invalid, 7}, // ALT prefixed, the first ESC is dropped2324 // Replies to queries fzf did not send, or sent and stopped waiting for25 {"\x1b[?1;2c", Invalid, 7},26 {"\x1b[>0;95;0c", Invalid, 10},2728 // Mouse report arriving while mouse input is off29 {"\x1b[<0;1;1M", Invalid, 9},3031 // String sequences32 {"\x1b]11;rgb:4a4a/4a4a/4a4a\x1b\\", Invalid, 25}, // background color reply33 {"\x1b]0;a title\a", Invalid, 12}, // BEL terminated34 {"\x1bP>|kitty(0.48.2)\x1b\\", Invalid, 19}, // XTVERSION reply35 {"\x1b_Gi=1;OK\x1b\\", Invalid, 11}, // Kitty graphics reply36 {"\x1bP1$r\a\x1b\\", Invalid, 8}, // BEL is payload in a DCS37 {"\x1b]11;x\x1bX\x1b\\", Invalid, 6}, // ESC ends it, framing "\e]11;x"3839 // SS3 is framed like CSI, since the parser routes both the same way40 {"\x1bO9;5u", Invalid, 6},41 {"\x1bO1;5X", Invalid, 6}, // unknown final byte42 {"\x1bO1;5A", CtrlUp, 6}, // recognized, unchanged43 {"\x1bOP", F1, 3},4445 // Left alone: this is how ALT-[, ALT-O, ALT-], ALT-P and ALT-_ arrive46 {"\x1b[a", Alt, 2},47 {"\x1b[9A", Alt, 2}, // ALT-[ and two characters can look like a CSI48 {"\x1b[1m", Alt, 2},49 {"\x1b[ x", Alt, 2},50 {"\x1bOx", Alt, 2},51 {"\x1bO9A", Alt, 2},5253 // rxvt keys fzf does not know: dropped, not typed54 {"\x1b[3^", Invalid, 4}, // CTRL-DELETE55 {"\x1b[5^", Invalid, 4}, // CTRL-PAGEUP56 {"\x1b[3@", Invalid, 4}, // CTRL-SHIFT-DELETE57 {"\x1b[11^", Invalid, 5},5859 // rxvt ends keys with $ after digits, so what follows is typed60 {"\x1b[7$x", Home, 4}, // SHIFT-HOME, then x61 {"\x1b[7$1x", Home, 4},62 {"\x1b[3$x", Invalid, 4},63 {"\x1b[3$1x", Invalid, 4},64 {"\x1b[23$x", Invalid, 5}, // SHIFT-F11, then x65 {"\x1b[24$1x", Invalid, 5},6667 // A parse that gives up past the frame consumes only the frame, so the68 // typed ~ after these stays in the buffer69 {"\x1b[3;1u~", Invalid, 6},70 {"\x1b[5;1u~", Invalid, 6},71 {"\x1b[2$~", Invalid, 4}, // rxvt SHIFT-INSERT72 {"\x1b[2^~", Invalid, 4}, // rxvt CTRL-INSERT73 {"\x1b[2A~", Invalid, 4},7475 // Elsewhere $ and other intermediate bytes do not end a sequence76 {"\x1b[4;0;10;20;0&w", Invalid, 15}, // not End, which \e[4 matches77 {"\x1b[4;2$y", Invalid, 7},78 {"\x1b[2 q", Invalid, 5},79 {"\x1b]abc", Alt, 2},80 {"\x1b]11;rgb:", Alt, 2}, // terminator has not arrived81 {"\x1b]\x1b]", Alt, 2}, // a second sequence must not swallow the ALT key82 {"\x1b]\x1b[A", Alt, 2},83 {"\x1bP\x1bP", Alt, 2},84 {"\x1b_\x1b_", Alt, 2},85 {"\x1b]\x1b\\", Alt, 2}, // ALT-] then ALT-backslash, not an empty OSC86 {"\x1b]\a", Alt, 2}, // ALT-] then CTRL-G, which must still abort8788 // Typed text after the introducer is not a reply, even when a89 // terminator follows in the same read90 {"\x1b]a\a", Alt, 2},91 {"\x1b]1\a", Alt, 2},92 {"\x1b]a\x1b[A", Alt, 2},93 {"\x1bPx\x1b[A", Alt, 2},94 {"\x1b_ab\x1bb", Alt, 2},9596 // SOS and PM are not framed97 {"\x1bXsos\x1b\\", Alt, 2},98 {"\x1b^status\x1b\\", Alt, 2},99100 // Left alone: no final byte yet, so the sequence may still be arriving101 {"\x1b[", Invalid, 2},102103 // Recognized sequences keep their existing parsing104 {"\x1b[1;5A", CtrlUp, 6},105 {"\x1b[3;5~", CtrlDelete, 6},106 {"\x1b[2~", Insert, 4},107 {"\x1b[200~", BracketedPasteBegin, 6},108 {"\x1b[Z", ShiftTab, 3},109 {"\x1bOA", Up, 3},110 {"\x1b[12;34R", Invalid, 8},111 {"\x1b[?2004;2$y", Invalid, 11},112 } {113 r := &LightRenderer{buffer: []byte(c.sequence)}114 sz := 1115 event := r.escSequence(&sz)116 if event.Type != c.event {117 t.Errorf("escSequence(%q) = %s, want %s",118 c.sequence, event.Type.String(), c.event.String())119 }120 if sz != c.size {121 t.Errorf("escSequence(%q) consumed %d bytes, want %d", c.sequence, sz, c.size)122 }123 }124}125126func TestStringEnd(t *testing.T) {127 for _, c := range []struct {128 buffer string129 want int130 }{131 // Terminated132 {"\x1b]0;t\a", 6},133 {"\x1b]0;t\x1b\\", 7},134 {"\x1b_G\x1b\\", 5},135 {"\x1bP1$r\a\x1b\\", 8}, // BEL is payload in a DCS, ST ends it136137 // BEL terminates an OSC only138 {"\x1bP\a", 0},139 {"\x1b_Gi=1\a", 0},140141 // An ESC ends the string and introduces a sequence of its own142 {"\x1b]foo\x1bX\x1b\\", 5},143 {"\x1b]foo\x1bP", 5},144 {"\x1b]\x1b]", 2},145146 // Nothing has ended it yet147 {"\x1b]0;t", 0},148 {"\x1b]0;t\x1b", 0}, // ST half arrived149 {"\x1b]foo\x1b", 0},150 {"\x1b]", 0},151 {"\x1b", 0}, // shorter than an introducer152153 } {154 if got := stringEnd([]byte(c.buffer)); got != c.want {155 t.Errorf("stringEnd(%q) = %d, want %d", c.buffer, got, c.want)156 }157 }158}159160// A dropped sequence followed by ALT-[ is not waiting for anything, so the161// bytes in between must not be held back until the next keystroke162func TestStillArriving(t *testing.T) {163 for _, c := range []struct {164 buffer string165 want bool166 }{167 {"\x1b[97;5u\a\x1b[", false},168 {"\x1b[97;5u\x1b[", false},169 {"\x1b[3;\a\x1b[", false}, // the BEL ended it, the trailing \e[ is another one170 {"\x1b[1;\a", false}, // the BEL has already ended it171 {"\x1b[1;", true},172 {"\x1b[", true},173 {"\x1bO1", true},174 {"\x1b[<0;1", true},175 } {176 if got := stillArriving([]byte(c.buffer)); got != c.want {177 t.Errorf("stillArriving(%q) = %v, want %v", c.buffer, got, c.want)178 }179 }180}181182func TestStringReply(t *testing.T) {183 for _, c := range []struct {184 buffer string185 want bool186 }{187 {"\x1b]11;rgb:1/2/3\a", true},188 {"\x1b]0;title\a", true},189 {"\x1b]52;c;YWJj\x1b\\", true},190 {"\x1bP>|kitty(0.48.2)\x1b\\", true},191 {"\x1bP!|0\x1b\\", true},192 {"\x1bP1$r0m\x1b\\", true},193 {"\x1bP0+r\x1b\\", true},194 {"\x1b_Gi=1;OK\x1b\\", true},195196 // What typed text after ALT-], ALT-P or ALT-_ looks like197 {"\x1b]\a", false},198 {"\x1b]a\a", false},199 {"\x1b]1\a", false},200 {"\x1b];\a", false},201 {"\x1bPx\x1b\\", false},202 {"\x1bP1\x1b\\", false},203 {"\x1b_ab\x1b\\", false},204 {"\x1b_G\x1b\\", false},205 } {206 if got := stringReply([]byte(c.buffer)); got != c.want {207 t.Errorf("stringReply(%q) = %v, want %v", c.buffer, got, c.want)208 }209 }210}211212func TestCsiEnd(t *testing.T) {213 for _, c := range []struct {214 buffer string215 want int216 }{217 {"\x1b[97;5u", 7},218 {"\x1b[A", 3},219 {"\x1b[<0;1;1M", 9},220 {"\x1b[?2004;2$y", 11},221 {"\x1b[7$x", 4}, // rxvt ends keys with $ after digits222 {"\x1b[23$", 5},223 {"\x1b[4;2$y", 7}, // but $ is intermediate elsewhere224 {"\x1b[97;5", 0}, // no final byte225 {"\x1b[", 0}, // no final byte226 {"\x1b[1\x01A", -1}, // malformed, do not frame it227 {"\x1b[\a", -1},228 } {229 if got := csiEnd([]byte(c.buffer)); got != c.want {230 t.Errorf("csiEnd(%q) = %d, want %d", c.buffer, got, c.want)231 }232 }233}Check escape sequence bytes before counting them (#4929)Junegunn Choibefore adoptionb563e20
lines 234–274why, one step back234235// A key that follows a sequence in the same read must not be consumed with it236func TestKeyAfterSequence(t *testing.T) {237 for _, c := range []struct {238 sequence string239 mouse bool240 event EventType241 size int242 }{243 // Recognized when more input follows, as from a paste or tmux send-keys244 {"\x1b[15~x", false, F5, 5},245 {"\x1b[3;5~x", false, CtrlDelete, 6},246 {"\x1b[5;13~x", false, CtrlAltPageUp, 7},247 {"\x1b[6;2~\x1b[A", false, ShiftPageDown, 6},248249 // ALT-[ or ALT-O and a digit, then a key that cannot continue a sequence250 {"\x1b[2\r", false, Alt, 2},251 {"\x1b[12\x03", false, Alt, 2},252 {"\x1bO2\a", false, Alt, 2},253 {"\x1b[4x", false, Alt, 2}, // not End254 {"\x1b[7\r", false, Alt, 2}, // not Home255 {"\x1b[7~x", false, Home, 4},256 {"\x1b[8^x", false, End, 4}, // rxvt CTRL-END257258 // A mouse report ends at its own final byte259 {"\x1b[<0;5;3Mx", true, Mouse, 9},260 {"\x1b[<0;1;\x1b[AM", true, Invalid, 3},261 {"\x1b[<0;1;1xM", true, Invalid, 9},262 } {263 r := &LightRenderer{buffer: []byte(c.sequence), mouse: c.mouse}264 sz := 1265 event := r.escSequence(&sz)266 if event.Type != c.event {267 t.Errorf("escSequence(%q) = %s, want %s",268 c.sequence, event.Type.String(), c.event.String())269 }270 if sz != c.size {271 t.Errorf("escSequence(%q) consumed %d bytes, want %d", c.sequence, sz, c.size)272 }273 }274}