* 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)
| 124 | * safely parsed out of the input stream at any time. |
| 125 | */ |
| 126 | function parseTerminalResponse(s: string): TerminalResponse | null { |
| 127 | // CSI-prefixed responses |
| 128 | if (s.startsWith('\x1b[')) { |
| 129 | let m: RegExpExecArray | null |
| 130 | |
| 131 | if ((m = DECRPM_RE.exec(s))) { |
| 132 | return { |
| 133 | type: 'decrpm', |
| 134 | mode: parseInt(m[1]!, 10), |
| 135 | status: parseInt(m[2]!, 10), |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if ((m = DA1_RE.exec(s))) { |
| 140 | return { type: 'da1', params: splitNumericParams(m[1]!) } |
| 141 | } |
| 142 | |
| 143 | if ((m = DA2_RE.exec(s))) { |
| 144 | return { type: 'da2', params: splitNumericParams(m[1]!) } |
| 145 | } |
| 146 | |
| 147 | if ((m = KITTY_FLAGS_RE.exec(s))) { |
| 148 | return { type: 'kittyKeyboard', flags: parseInt(m[1]!, 10) } |
| 149 | } |
| 150 | |
| 151 | if ((m = CURSOR_POSITION_RE.exec(s))) { |
| 152 | return { |
| 153 | type: 'cursorPosition', |
| 154 | row: parseInt(m[1]!, 10), |
| 155 | col: parseInt(m[2]!, 10), |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return null |
| 160 | } |
| 161 | |
| 162 | // OSC responses (e.g. OSC 11 ; rgb:... for bg color query) |
| 163 | if (s.startsWith('\x1b]')) { |
| 164 | const m = OSC_RESPONSE_RE.exec(s) |
| 165 | if (m) { |
| 166 | return { type: 'osc', code: parseInt(m[1]!, 10), data: m[2]! } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // DCS responses (e.g. XTVERSION: DCS > | name ST) |
| 171 | if (s.startsWith('\x1bP')) { |
| 172 | const m = XTVERSION_RE.exec(s) |
| 173 | if (m) { |
| 174 | return { type: 'xtversion', name: m[1]! } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return null |
| 179 | } |
| 180 | |
| 181 | function splitNumericParams(params: string): number[] { |
| 182 | if (!params) return [] |
no test coverage detected