MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / extractAnsiCode

Function extractAnsiCode

packages/pi-tui/src/utils.ts:316–354  ·  view source on GitHub ↗
(str: string, pos: number)

Source from the content-addressed store, hash-verified

314 * Extract ANSI escape sequences from a string at the given position.
315 */
316export 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
356type Osc8Terminator = "\x07" | "\x1b\\";
357

Callers 9

truncateFragmentToWidthFunction · 0.85
visibleWidthFunction · 0.85
asciiVisibleWidthFunction · 0.85
updateTrackerFromTextFunction · 0.85
splitIntoTokensWithAnsiFunction · 0.85
breakLongWordFunction · 0.85
truncateToWidthFunction · 0.85
sliceWithWidthFunction · 0.85
extractSegmentsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected