From 3d75a2c67e762bae9e7bf00c4f963a366c0663ee Mon Sep 17 00:00:00 2001 From: phranck Date: Fri, 30 Jan 2026 23:04:42 +0100 Subject: [PATCH] Refactor: Eliminate Terminal singleton - Remove Terminal.shared static property - Make Terminal.init() public instead of private - Update default parameters in ViewRenderer and RenderContext to use Terminal() - WindowGroup.renderScene now uses context.terminal instead of Terminal.shared --- Sources/TUIkit/App/App.swift | 4 ++-- Sources/TUIkit/Rendering/Renderable.swift | 2 +- Sources/TUIkit/Rendering/Terminal.swift | 7 ++----- Sources/TUIkit/Rendering/ViewRenderer.swift | 2 +- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/Sources/TUIkit/App/App.swift b/Sources/TUIkit/App/App.swift index fb87eb0..2f9f12e 100644 --- a/Sources/TUIkit/App/App.swift +++ b/Sources/TUIkit/App/App.swift @@ -74,7 +74,7 @@ internal final class AppRunner { init(app: A) { self.app = app - self.terminal = Terminal.shared + self.terminal = Terminal() self.statusBar = StatusBarState() self.focusManager = FocusManager() self.tuiContext = TUIContext() @@ -203,7 +203,7 @@ internal protocol SceneRenderable { extension WindowGroup: SceneRenderable { func renderScene(context: RenderContext) { let buffer = renderToBuffer(content, context: context) - let terminal = Terminal.shared + let terminal = context.terminal let terminalWidth = terminal.width let terminalHeight = context.availableHeight diff --git a/Sources/TUIkit/Rendering/Renderable.swift b/Sources/TUIkit/Rendering/Renderable.swift index 41617ed..d25e4a2 100644 --- a/Sources/TUIkit/Rendering/Renderable.swift +++ b/Sources/TUIkit/Rendering/Renderable.swift @@ -91,7 +91,7 @@ public struct RenderContext { /// - environment: The environment values (defaults to empty). /// - tuiContext: The TUI context (defaults to a fresh instance). public init( - terminal: Terminal = .shared, + terminal: Terminal = Terminal(), availableWidth: Int? = nil, availableHeight: Int? = nil, environment: EnvironmentValues = EnvironmentValues(), diff --git a/Sources/TUIkit/Rendering/Terminal.swift b/Sources/TUIkit/Rendering/Terminal.swift index 854e70a..0f6d62c 100644 --- a/Sources/TUIkit/Rendering/Terminal.swift +++ b/Sources/TUIkit/Rendering/Terminal.swift @@ -32,9 +32,6 @@ import Foundation /// - Raw mode configuration /// - Safe input and output public final class Terminal: @unchecked Sendable { - /// The shared terminal instance. - public static let shared = Terminal() - /// The width of the terminal in characters. public var width: Int { getSize().width @@ -51,8 +48,8 @@ public final class Terminal: @unchecked Sendable { /// The original terminal settings. private var originalTermios: termios? - /// Private initializer for singleton. - private init() {} + /// Creates a new terminal instance. + public init() {} /// Destructor ensures raw mode is disabled. deinit { diff --git a/Sources/TUIkit/Rendering/ViewRenderer.swift b/Sources/TUIkit/Rendering/ViewRenderer.swift index 69e3244..a9ff564 100644 --- a/Sources/TUIkit/Rendering/ViewRenderer.swift +++ b/Sources/TUIkit/Rendering/ViewRenderer.swift @@ -30,7 +30,7 @@ public final class ViewRenderer { /// Creates a new ViewRenderer. /// /// - Parameter terminal: The target terminal. - public init(terminal: Terminal = .shared) { + public init(terminal: Terminal = Terminal()) { self.terminal = terminal }