(str: string, pos: number)
| 314 | * Extract ANSI escape sequences from a string at the given position. |
| 315 | */ |
| 316 | export function extractAnsiCode(str: string, pos: number): { code: string; length: number } | null { |
| 317 | if (pos >= str.length || str[pos] !== "\x1b") return null; |
| 318 | |
| 319 | const next = str[pos + 1]; |
| 320 | |
| 321 | // CSI sequence: ESC [ ... m/G/K/H/J |
| 322 | if (next === "[") { |
| 323 | let j = pos + 2; |
| 324 | while (j < str.length && !/[mGKHJ]/.test(str[j]!)) j++; |
| 325 | if (j < str.length) return { code: str.substring(pos, j + 1), length: j + 1 - pos }; |
| 326 | return null; |
| 327 | } |
| 328 | |
| 329 | // OSC sequence: ESC ] ... BEL or ESC ] ... ST (ESC \) |
| 330 | // Used for hyperlinks (OSC 8), window titles, etc. |
| 331 | if (next === "]") { |
| 332 | let j = pos + 2; |
| 333 | while (j < str.length) { |
| 334 | if (str[j] === "\x07") return { code: str.substring(pos, j + 1), length: j + 1 - pos }; |
| 335 | if (str[j] === "\x1b" && str[j + 1] === "\\") return { code: str.substring(pos, j + 2), length: j + 2 - pos }; |
| 336 | j++; |
| 337 | } |
| 338 | return null; |
| 339 | } |
| 340 | |
| 341 | // APC sequence: ESC _ ... BEL or ESC _ ... ST (ESC \) |
| 342 | // Used for cursor marker and application-specific commands |
| 343 | if (next === "_") { |
| 344 | let j = pos + 2; |
| 345 | while (j < str.length) { |
| 346 | if (str[j] === "\x07") return { code: str.substring(pos, j + 1), length: j + 1 - pos }; |
| 347 | if (str[j] === "\x1b" && str[j + 1] === "\\") return { code: str.substring(pos, j + 2), length: j + 2 - pos }; |
| 348 | j++; |
| 349 | } |
| 350 | return null; |
| 351 | } |
| 352 | |
| 353 | return null; |
| 354 | } |
| 355 | |
| 356 | type Osc8Terminator = "\x07" | "\x1b\\"; |
| 357 |
no outgoing calls
no test coverage detected