@visulima/ansi
Version:
ANSI escape codes for some terminal swag.
28 lines (27 loc) • 1.21 kB
TypeScript
/**
* Removes ANSI escape codes from a string.
*
* The implementation is a single linear pass over the input (O(n)) — it never
* relies on a backtracking regular expression, so it is safe to run on
* untrusted subprocess/log output without risking polynomial ReDoS on
* adversarial input (e.g. many unterminated `ESC ]` prefixes).
*
* Recognised sequences: CSI (`ESC [ ... final`, final byte `0x40`-`0x7e`),
* OSC (`ESC ] ... BEL` or terminated by ST, covering window titles and
* hyperlinks), string sequences DCS/SOS/PM/APC (`ESC P|X|^|_ ... ST`), and
* two-character escapes (`ESC` plus a single byte, e.g. `ESC 7`, `ESC c`).
* The 8-bit C1 single-byte introducers (`0x9b` CSI, `0x9d` OSC, `0x90` DCS,
* `0x98` SOS, `0x9e` PM, `0x9f` APC) are recognised as equivalents of their
* `ESC`-prefixed forms.
* @param input The string from which to remove ANSI escape codes.
* @returns The input string with all ANSI escape codes stripped.
* @example
* ```typescript
* import { strip } from "@visulima/ansi";
*
* const textWithAnsi = "\x1b[32mHello\x1b[0m";
* console.log(strip(textWithAnsi)); // "Hello"
* ```
*/
declare const strip: (input: string) => string;
export { strip as default };