From ce9ffaff9152bede4c30037e2f8137a7d76e4c50 Mon Sep 17 00:00:00 2001 From: Mario Zechner Date: Mon, 15 Dec 2025 23:00:25 +0100 Subject: [PATCH] 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 --- packages/tui/src/utils.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/tui/src/utils.ts b/packages/tui/src/utils.ts index a724cf7f..0603beaa 100644 --- a/packages/tui/src/utils.ts +++ b/packages/tui/src/utils.ts @@ -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 : [""];