Fix ANSI styles not preserved across newlines in text wrapping

wrapTextWithAnsi() was processing each line independently after splitting
on newlines, losing ANSI state. When styled text contained embedded newlines
(e.g. from markdown paragraphs), subsequent lines would lose their styling.

Fixed by tracking ANSI state across lines and prepending active codes to
lines after the first.

Fixes #197
This commit is contained in:
Mario Zechner 2025-12-15 23:00:25 +01:00
parent fbda78bfb3
commit ce9ffaff91

View file

@ -312,11 +312,17 @@ export function wrapTextWithAnsi(text: string, width: number): string[] {
}
// Handle newlines by processing each line separately
// Track ANSI state across lines so styles carry over after literal newlines
const inputLines = text.split("\n");
const result: string[] = [];
const tracker = new AnsiCodeTracker();
for (const inputLine of inputLines) {
result.push(...wrapSingleLine(inputLine, width));
// Prepend active ANSI codes from previous lines (except for first line)
const prefix = result.length > 0 ? tracker.getActiveCodes() : "";
result.push(...wrapSingleLine(prefix + inputLine, width));
// Update tracker with codes from this line for next iteration
updateTrackerFromText(inputLine, tracker);
}
return result.length > 0 ? result : [""];