| 17 | |
| 18 | // Strips all ANSI C0 and C1 control characters (except for SGR up to 8 bit) |
| 19 | function STRIP_C01 (str) { |
| 20 | if (!HAS_C01.test(str)) { |
| 21 | return str |
| 22 | } |
| 23 | let result = '' |
| 24 | for (let i = 0; i < str.length; i++) { |
| 25 | const char = str[i] |
| 26 | const code = char.charCodeAt(0) |
| 27 | if (!HAS_C01.test(char)) { |
| 28 | // Most characters are in this set so continue early if we can |
| 29 | result = `${result}${char}` |
| 30 | } else if (code === 27 && ALLOWED_SGR.test(str.slice(i + 1, i + SGR_MAX_LEN + 1))) { |
| 31 | // \x1b with allowed SGR |
| 32 | result = `${result}\x1b` |
| 33 | } else if (code <= 31) { |
| 34 | // escape all other C0 control characters besides \x7f |
| 35 | result = `${result}^${String.fromCharCode(code + 64)}` |
| 36 | } else { |
| 37 | // hasC01 ensures this is now a C1 control character or \x7f |
| 38 | result = `${result}^${String.fromCharCode(code - 64)}` |
| 39 | } |
| 40 | } |
| 41 | return result |
| 42 | } |
| 43 | |
| 44 | const formatWithOptions = ({ prefix: prefixes = [], eol = '\n', redact = true, ...options }, ...args) => { |
| 45 | const prefix = prefixes.filter(p => p != null).join(' ') |