(text: string, width: number)
| 718 | * @returns Array of wrapped lines (NOT padded to width) |
| 719 | */ |
| 720 | export function wrapTextWithAnsi(text: string, width: number): string[] { |
| 721 | if (!text) { |
| 722 | return [""]; |
| 723 | } |
| 724 | |
| 725 | // Handle newlines by processing each line separately |
| 726 | // Track ANSI state across lines so styles carry over after literal newlines |
| 727 | const inputLines = text.split("\n"); |
| 728 | const result: string[] = []; |
| 729 | const tracker = new AnsiCodeTracker(); |
| 730 | |
| 731 | for (const inputLine of inputLines) { |
| 732 | // Prepend active ANSI codes from previous lines (except for first line) |
| 733 | const prefix = result.length > 0 ? tracker.getActiveCodes() : ""; |
| 734 | const wrappedLines = wrapSingleLine(prefix + inputLine, width); |
| 735 | for (const wrappedLine of wrappedLines) { |
| 736 | result.push(wrappedLine); |
| 737 | } |
| 738 | // Update tracker with codes from this line for next iteration |
| 739 | updateTrackerFromText(inputLine, tracker); |
| 740 | } |
| 741 | |
| 742 | return result.length > 0 ? result : [""]; |
| 743 | } |
| 744 | |
| 745 | function wrapSingleLine(line: string, width: number): string[] { |
| 746 | if (!line) { |
no test coverage detected