Repository Public
1import Foundation2import Observation3import WorkspaceCore45/// Owns every live `TerminalSession`. The registry is the single owner of process lifetimes:6/// UI views come and go, sessions only end when `close` is called or the process exits.7@MainActor8@Observable9public final class SessionRegistry {10 public private(set) var sessions: [SessionID: TerminalSession] = [:]11 public var appearance: TerminalAppearance {12 didSet { for session in sessions.values { session.apply(appearance: appearance) } }13 }14 public var planner: any LaunchPlanning1516 public init(appearance: TerminalAppearance = .default, planner: any LaunchPlanning = LocalLaunchPlanner()) {17 self.appearance = appearance18 self.planner = planner19 }2021 public var orderedSessions: [TerminalSession] {22 sessions.values.sorted { $0.createdAt < $1.createdAt }23 }2425 public var liveCount: Int { sessions.values.filter { $0.status.isAlive }.count }2627 public func session(_ id: SessionID) -> TerminalSession? { sessions[id] }2829 /// Creates and starts a session. Launch failures are recorded on the session (status `.failed`)30 /// rather than thrown, so the pane still exists and can show the error.31 @discardableResult32 public func create(id: SessionID = SessionID(), profile: SessionProfile, paneID: PaneID? = nil) -> TerminalSession {33 if let existing = sessions[id] { return existing }34 let session = TerminalSession(id: id, profile: profile, appearance: appearance)35 session.paneID = paneID36 sessions[id] = session37 do {38 let spec = try planner.launchSpec(for: profile, sessionID: id)39 session.start(with: spec)40 } catch {41 session.recordLaunchFailure(error.localizedDescription)42 }43 return session44 }4546 /// Terminates and forgets a session.47 public func close(_ id: SessionID) {48 guard let session = sessions.removeValue(forKey: id) else { return }49 session.terminate()50 session.view.removeFromSuperview()51 }5253 public func terminateAll() {54 for id in Array(sessions.keys) { close(id) }55 }56}5758extension TerminalSession {59 func recordLaunchFailure(_ message: String) {60 view.feed(text: "\r\n\u{1b}[31mNEXTERM could not start this session: \(message)\u{1b}[0m\r\n")61 markFailed(message)62 }63}64