* Try to recognize a sequence token as a terminal response. * Returns null if the sequence is not a known response pattern * (i.e. it should be treated as a keypress). * * These patterns are syntactically distinguishable from keyboard input — * no physical key produces CSI ? ... c or CSI ? ...
(s: string)
| 120 | * safely parsed out of the input stream at any time. |
| 121 | */ |
| 122 | function parseTerminalResponse(s: string): TerminalResponse | null { |
| 123 | // CSI-prefixed responses |
| 124 | if (s.startsWith('\x1b[')) { |
| 125 | let m: RegExpExecArray | null |
| 126 | |
| 127 | if ((m = DECRPM_RE.exec(s))) { |
| 128 | return { |
| 129 | type: 'decrpm', |
| 130 | mode: parseInt(m[1]!, 10), |
| 131 | status: parseInt(m[2]!, 10), |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if ((m = DA1_RE.exec(s))) { |
| 136 | return { type: 'da1', params: splitNumericParams(m[1]!) } |
| 137 | } |
| 138 | |
| 139 | if ((m = DA2_RE.exec(s))) { |
| 140 | return { type: 'da2', params: splitNumericParams(m[1]!) } |
| 141 | } |
| 142 | |
| 143 | if ((m = KITTY_FLAGS_RE.exec(s))) { |
| 144 | return { type: 'kittyKeyboard', flags: parseInt(m[1]!, 10) } |
| 145 | } |
| 146 | |
| 147 | if ((m = CURSOR_POSITION_RE.exec(s))) { |
| 148 | return { |
| 149 | type: 'cursorPosition', |
| 150 | row: parseInt(m[1]!, 10), |
| 151 | col: parseInt(m[2]!, 10), |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return null |
| 156 | } |
| 157 | |
| 158 | // OSC responses (e.g. OSC 11 ; rgb:... for bg color query) |
| 159 | if (s.startsWith('\x1b]')) { |
| 160 | const m = OSC_RESPONSE_RE.exec(s) |
| 161 | if (m) { |
| 162 | return { type: 'osc', code: parseInt(m[1]!, 10), data: m[2]! } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // DCS responses (e.g. XTVERSION: DCS > | name ST) |
| 167 | if (s.startsWith('\x1bP')) { |
| 168 | const m = XTVERSION_RE.exec(s) |
| 169 | if (m) { |
| 170 | return { type: 'xtversion', name: m[1]! } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return null |
| 175 | } |
| 176 | |
| 177 | function splitNumericParams(params: string): number[] { |
| 178 | if (!params) return [] |
no test coverage detected