| 1 | package ansi |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/charmbracelet/x/ansi/parser" |
| 9 | ) |
| 10 | |
| 11 | func TestDcsSequence(t *testing.T) { |
| 12 | cases := []testCase{ |
| 13 | { |
| 14 | name: "max_params", |
| 15 | input: fmt.Sprintf("\x1bP%sp\x1b\\", strings.Repeat("1;", 33)), |
| 16 | expected: []any{ |
| 17 | dcsSequence{ |
| 18 | Cmd: 'p', |
| 19 | Params: Params{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, |
| 20 | Data: []byte{}, |
| 21 | }, |
| 22 | Cmd('\\'), |
| 23 | }, |
| 24 | }, |
| 25 | { |
| 26 | name: "reset", |
| 27 | input: "\x1b[3;1\x1bP1$tx\x9c", |
| 28 | expected: []any{ |
| 29 | dcsSequence{ |
| 30 | Cmd: 't' | '$'<<parser.IntermedShift, |
| 31 | Params: Params{1}, |
| 32 | Data: []byte{'x'}, |
| 33 | }, |
| 34 | }, |
| 35 | }, |
| 36 | { |
| 37 | name: "parse", |
| 38 | input: "\x1bP0;1|17/ab\x9c", |
| 39 | expected: []any{ |
| 40 | dcsSequence{ |
| 41 | Cmd: '|', |
| 42 | Params: Params{0, 1}, |
| 43 | Data: []byte("17/ab"), |
| 44 | }, |
| 45 | }, |
| 46 | }, |
| 47 | { |
| 48 | name: "intermediate_reset_on_exit", |
| 49 | input: "\x1bP=1sZZZ\x1b+\x5c", |
| 50 | expected: []any{ |
| 51 | dcsSequence{ |
| 52 | Cmd: 's' | '='<<parser.PrefixShift, |
| 53 | Params: Params{1}, |
| 54 | Data: []byte("ZZZ"), |
| 55 | }, |
| 56 | Cmd(0x5c | '+'<<parser.IntermedShift), |
| 57 | }, |
| 58 | }, |
| 59 | { |
| 60 | name: "put_utf8", |
| 61 | input: "\x1bP+r😃\x1b\\", |
| 62 | expected: []any{ |
| 63 | dcsSequence{ |
| 64 | Cmd: 'r' | '+'<<parser.IntermedShift, |
| 65 | Params: Params{}, |
| 66 | Data: []byte("😃"), |
| 67 | }, |
| 68 | Cmd('\\'), |
| 69 | }, |
| 70 | }, |
| 71 | } |
| 72 | |
| 73 | for _, c := range cases { |
| 74 | t.Run(c.name, func(t *testing.T) { |
| 75 | dispatcher := &testDispatcher{} |
| 76 | parser := testParser(dispatcher) |
| 77 | parser.Parse([]byte(c.input)) |
| 78 | assertEqual(t, len(c.expected), len(dispatcher.dispatched)) |
| 79 | assertEqual(t, c.expected, dispatcher.dispatched) |
| 80 | }) |
| 81 | } |
| 82 | } |