The string to escape.
The string with ANSI escape codes escaped (non-printable characters converted to \xNN format).
import { escapeAnsi, red } from "@nevware21/chromacon";
// Escape ANSI codes in colored text
const coloredText = red("Hello World");
console.log(escapeAnsi(coloredText));
//=> "\\x1b[31mHello World\\x1b[0m"
// Escape ANSI codes in raw escape sequences
escapeAnsi("\u001b[31mError\u001b[0m");
//=> "\\x1b[31mError\\x1b[0m"
// Useful for logging ANSI sequences as visible text
const debugLog = escapeAnsi("\u001b]8;;https://github.com\u0007Click\u001b]8;;\u0007");
console.log(debugLog);
//=> "\\x1b]8;;https://github.com\\x07Click\\x1b]8;;\\x07"
// Regular text remains unchanged
escapeAnsi("Hello World");
//=> "Hello World"
Escape ANSI escape codes in a string by converting non-printable characters within ANSI sequences to their hex representation. This is useful for displaying ANSI escape codes as visible text in logs or documentation.