diff --git a/Sources/TUIKit/App/App.swift b/Sources/TUIKit/App/App.swift index 7859408..31508d2 100644 --- a/Sources/TUIKit/App/App.swift +++ b/Sources/TUIKit/App/App.swift @@ -472,8 +472,6 @@ internal final class AppRunner { } private func render() { - terminal.clear() - // Clear event handlers before re-rendering KeyEventDispatcher.shared.clearHandlers() focusManager.clear() @@ -501,6 +499,9 @@ internal final class AppRunner { // Update global environment storage EnvironmentStorage.shared.environment = environment + + // Fill the entire screen with the theme background color + terminal.fillBackground(themeManager.currentTheme.background) // Render main content let scene = app.body diff --git a/Sources/TUIKit/Rendering/ANSIRenderer.swift b/Sources/TUIKit/Rendering/ANSIRenderer.swift index 271b307..b2b0a38 100644 --- a/Sources/TUIKit/Rendering/ANSIRenderer.swift +++ b/Sources/TUIKit/Rendering/ANSIRenderer.swift @@ -116,6 +116,17 @@ public enum ANSIRenderer { return ["48", "2", "\(red)", "\(green)", "\(blue)"] } } + + /// Generates the ANSI escape sequence for a background color. + /// + /// Use this to set only the background color without other styles. + /// + /// - Parameter color: The background color. + /// - Returns: The ANSI escape sequence. + public static func backgroundCode(for color: Color) -> String { + let codes = backgroundCodes(for: color) + return "\(csi)\(codes.joined(separator: ";"))m" + } // MARK: - Cursor Control diff --git a/Sources/TUIKit/Rendering/Terminal.swift b/Sources/TUIKit/Rendering/Terminal.swift index 00f8039..6c6fb81 100644 --- a/Sources/TUIKit/Rendering/Terminal.swift +++ b/Sources/TUIKit/Rendering/Terminal.swift @@ -152,6 +152,30 @@ public final class Terminal: @unchecked Sendable { public func clear() { write(ANSIRenderer.clearScreen + ANSIRenderer.moveCursor(toRow: 1, column: 1)) } + + /// Fills the entire screen with a background color. + /// + /// This clears the screen and fills every cell with the specified color. + /// Use this to set a consistent background before rendering content. + /// + /// - Parameter color: The background color to fill. + public func fillBackground(_ color: Color) { + let size = getSize() + let bgCode = ANSIRenderer.backgroundCode(for: color) + let reset = ANSIRenderer.reset + + // Move to top-left and fill each line + var output = ANSIRenderer.moveCursor(toRow: 1, column: 1) + let emptyLine = bgCode + String(repeating: " ", count: size.width) + reset + + for _ in 0..