( string: string, terminalWidth: number, )
| 8 | // word-wrap a string that contains ANSI escape sequences. |
| 9 | // ANSI escape sequences do not add to the string length. |
| 10 | export default function wrapAnsiString( |
| 11 | string: string, |
| 12 | terminalWidth: number, |
| 13 | ): string { |
| 14 | if (terminalWidth === 0) { |
| 15 | // if the terminal width is zero, don't bother word-wrapping |
| 16 | return string; |
| 17 | } |
| 18 | |
| 19 | const ANSI_REGEXP = /[\u001B\u009B]\[\d{1,2}m/gu; |
| 20 | const tokens = []; |
| 21 | let lastIndex = 0; |
| 22 | let match; |
| 23 | |
| 24 | while ((match = ANSI_REGEXP.exec(string))) { |
| 25 | const ansi = match[0]; |
| 26 | const index = match.index; |
| 27 | if (index !== lastIndex) { |
| 28 | tokens.push(['string', string.slice(lastIndex, index)]); |
| 29 | } |
| 30 | tokens.push(['ansi', ansi]); |
| 31 | lastIndex = index + ansi.length; |
| 32 | } |
| 33 | |
| 34 | if (lastIndex !== string.length - 1) { |
| 35 | tokens.push(['string', string.slice(lastIndex)]); |
| 36 | } |
| 37 | |
| 38 | let lastLineLength = 0; |
| 39 | |
| 40 | return tokens |
| 41 | .reduce( |
| 42 | (lines, [kind, token]) => { |
| 43 | if (kind === 'string') { |
| 44 | if (lastLineLength + token.length > terminalWidth) { |
| 45 | while (token.length > 0) { |
| 46 | const chunk = token.slice(0, terminalWidth - lastLineLength); |
| 47 | const remaining = token.slice(terminalWidth - lastLineLength); |
| 48 | lines[lines.length - 1] += chunk; |
| 49 | lastLineLength += chunk.length; |
| 50 | token = remaining; |
| 51 | if (token.length > 0) { |
| 52 | lines.push(''); |
| 53 | lastLineLength = 0; |
| 54 | } |
| 55 | } |
| 56 | } else { |
| 57 | lines[lines.length - 1] += token; |
| 58 | lastLineLength += token.length; |
| 59 | } |
| 60 | } else { |
| 61 | lines[lines.length - 1] += token; |
| 62 | } |
| 63 | |
| 64 | return lines; |
| 65 | }, |
| 66 | [''], |
| 67 | ) |
no test coverage detected