light_escape_test.go
This file
Path
src/tui/light_escape_test.go
96 lines · 2.6 KiB · Go
Last changed by
Drop unrecognized escape sequences instead of typing them (#4926)Junegunn Choibefore adoptionc009c10
History of this fileTake it
Wait for rest of escape sequence before parsing (#4901)Junegunn Choibefore adoptiond0377ed
lines 1–53why, one step back1package tui23import (4 "strings"5 "testing"6)78func TestIncompleteEscape(t *testing.T) {9 for _, c := range []struct {10 buffer string11 want bool12 }{13 // Complete sequences: nothing to wait for14 {"\x1b[A", false},15 {"\x1bOA", false},16 {"\x1b[1;5A", false},17 {"\x1b[200~", false},18 {"\x1b[<0;1;1M", false},19 {"\x1b[12;34R", false},20 {"\x1b[?2004;2$y", false},21 {"\x1b[?1;2c", false},2223 // Fragments: keep waiting24 {"\x1b[", true},25 {"\x1b[?", true},26 {"\x1b[1;", true},27 {"\x1b[?2004;2$", true},28 {"\x1bO", true},29 {"\x1b[<0;1;", true},3031 // Only the trailing sequence matters32 {"ab\x1b[?2004;2$", true},33 {"\x1b[A\x1b[", true},34 {"\x1b[A\x1b[B", false},3536 // Long buffers: only the tail is scanned, so an introducer further37 // back than escapeLookback is not waited for38 {strings.Repeat("a", 100000), false},39 {"\x1b[" + strings.Repeat("a", 100000), false},40 {strings.Repeat("a", 100000) + "\x1b[1;", true},4142 // Not a sequence fzf waits on43 {"", false},44 {"abc", false},45 {"\x1b", false}, // lone ESC, handled by the existing escDelay branch46 {"\x1ba", false}, // ALT-a47 {"\x1b[\x01", false}, // malformed, do not stall on it48 } {49 if got := incompleteEscape([]byte(c.buffer)); got != c.want {50 t.Errorf("incompleteEscape(%q) = %v, want %v", c.buffer, got, c.want)51 }52 }53}Drop unrecognized escape sequences instead of typing them (#4926)Junegunn Choibefore adoptionc009c10
lines 54–96why, one step back5455// A string sequence is not waited for. Its introducer is also ALT-], ALT-P or56// ALT-_, and holding the read open on those both delayed the key and let typed57// bytes accumulate into something that looked like a sequence.58func TestIncompleteStringEscape(t *testing.T) {59 for _, buffer := range []string{60 "\x1b]11;rgb:",61 "\x1bP>|kitty",62 "\x1b_Gi=1",63 "\x1b]0;t\x1b",64 "\x1b]0;title\a",65 "ab\x1b]11;rgb:",66 } {67 if incompleteEscape([]byte(buffer)) {68 t.Errorf("incompleteEscape(%q) = true, want false", buffer)69 }70 }71}7273// SS3 reaches the same parameterized subcases as CSI in the parser, so it has to74// be judged the same way here. Declaring it finished after one byte made the75// parser give up on a split \eO1;5A and type its tail into the query.76func TestIncompleteSS3(t *testing.T) {77 for _, c := range []struct {78 buffer string79 want bool80 }{81 {"\x1bO", true},82 {"\x1bO1", true},83 {"\x1bO1;", true},84 {"\x1bO1;5", true},85 {"\x1bOA", false},86 {"\x1bOP", false},87 {"\x1bO1;5A", false},88 {"\x1b[23$", false}, // rxvt SHIFT-F11 is complete89 {"\x1b[7$", false},90 {"\x1b[4;2$", true}, // $ after a ; is intermediate91 } {92 if got := incompleteEscape([]byte(c.buffer)); got != c.want {93 t.Errorf("incompleteEscape(%q) = %v, want %v", c.buffer, got, c.want)94 }95 }96}