SPB Git

Repository Public

Presets.swift 8.8 KB 171 lines swift

1import CoreGraphics2import Foundation34/// Built-in workspace arrangements.5public enum WorkspacePreset: String, CaseIterable, Sendable, Identifiable {6    case codeTestsLogs7    case sshFleet8    case agentCommandCenter9    case fourPanes10    case dualMonitor11    case grid2, grid3, grid6, grid81213    public var id: String { rawValue }1415    public var title: String {16        switch self {17        case .codeTestsLogs: return "Code + Tests + Logs"18        case .sshFleet: return "SSH Fleet"19        case .agentCommandCenter: return "Agent Command Center"20        case .fourPanes: return "4 Panes"21        case .dualMonitor: return "Dual Monitor"22        case .grid2: return "2 Panes"23        case .grid3: return "3 Panes"24        case .grid6: return "6 Panes"25        case .grid8: return "8 Panes"26        }27    }2829    public var summary: String {30        switch self {31        case .codeTestsLogs: return "A large editor pane with tests and logs stacked on the right."32        case .sshFleet: return "A grid with one pane per selected host."33        case .agentCommandCenter: return "One supervisor shell and a column of agent panes, each with its own PTY."34        case .fourPanes: return "A 2 × 2 grid of shells."35        case .dualMonitor: return "One window per display: work on the primary, monitoring on the second."36        case .grid2: return "Two shells side by side."37        case .grid3: return "Three columns."38        case .grid6: return "A 3 × 2 grid."39        case .grid8: return "A 4 × 2 grid."40        }41    }4243    /// Number of panes the preset creates in its largest window (before adapting to the screen).44    public var nominalPaneCount: Int {45        switch self {46        case .codeTestsLogs: return 347        case .sshFleet: return 448        case .agentCommandCenter: return 449        case .fourPanes: return 450        case .dualMonitor: return 351        case .grid2: return 252        case .grid3: return 353        case .grid6: return 654        case .grid8: return 855        }56    }57}5859public struct PresetOptions: Sendable {60    /// Working directory given to local panes.61    public var workingDirectory: String?62    /// Host aliases for the SSH Fleet preset (one pane each).63    public var hosts: [String]64    /// Command lines for the Agent Command Center (one pane each); empty panes are plain shells.65    public var agentCommands: [String]66    /// Number of displays currently available (Dual Monitor falls back to one window when 1).67    public var screenCount: Int68    /// Upper bound of panes per window for the target screen (see `PaneCapacity`).69    public var maxPanesPerWindow: Int7071    public init(workingDirectory: String? = nil, hosts: [String] = [], agentCommands: [String] = [],72                screenCount: Int = 1, maxPanesPerWindow: Int = 8) {73        self.workingDirectory = workingDirectory74        self.hosts = hosts75        self.agentCommands = agentCommands76        self.screenCount = screenCount77        self.maxPanesPerWindow = max(1, maxPanesPerWindow)78    }79}8081public enum PresetBuilder {82    /// Builds the windows of a preset. The result is purely logical: frames are assigned by the83    /// window manager from `screenSlot` when the workspace is opened.84    public static func build(_ preset: WorkspacePreset, options: PresetOptions = PresetOptions()) -> WorkspaceSnapshot {85        let cwd = options.workingDirectory86        func shell(_ role: String? = nil) -> PaneDescriptor {87            PaneDescriptor(kind: .localShell(path: nil), workingDirectory: cwd, role: role)88        }89        func gridTab(_ panes: [PaneDescriptor], columns: Int? = nil, title: String? = nil) -> TabModel {90            let limited = Array(panes.prefix(options.maxPanesPerWindow))91            let layout = LayoutNode.grid(limited.map(\.id), columns: columns) ?? .pane(limited[0].id)92            return TabModel(customTitle: title, layout: layout, panes: limited)93        }94        var windows: [WindowModel]95        switch preset {96        case .codeTestsLogs:97            let code = shell("code"), tests = shell("tests"), logs = shell("logs")98            let right = LayoutNode.even([.pane(tests.id), .pane(logs.id)], axis: .vertical)99            let layout = LayoutNode.weighted([(.pane(code.id), 0.6), (right, 0.4)], axis: .horizontal)100            windows = [WindowModel(tabs: [TabModel(customTitle: preset.title, layout: layout,101                                                   panes: [code, tests, logs], focusedPane: code.id)])]102        case .sshFleet:103            let hosts = options.hosts.isEmpty ? [] : options.hosts104            let panes: [PaneDescriptor] = hosts.isEmpty105                ? (0..<4).map { _ in shell("host") }106                : hosts.map { PaneDescriptor(kind: .ssh(hostAlias: $0), customTitle: $0, role: "host", receivesBroadcast: false) }107            // Hosts beyond the per-window capacity go to additional tabs rather than being dropped.108            let chunks = stride(from: 0, to: panes.count, by: options.maxPanesPerWindow).map {109                Array(panes[$0..<min(panes.count, $0 + options.maxPanesPerWindow)])110            }111            windows = [WindowModel(tabs: chunks.enumerated().map { index, chunk in112                gridTab(chunk, title: chunks.count > 1 ? "\(preset.title) \(index + 1)" : preset.title)113            })]114        case .agentCommandCenter:115            let supervisor = shell("supervisor")116            let commands = options.agentCommands.isEmpty ? ["", "", ""] : options.agentCommands117            let agents: [PaneDescriptor] = commands.prefix(max(1, options.maxPanesPerWindow - 1)).enumerated().map { index, line in118                PaneDescriptor(kind: line.isEmpty ? .localShell(path: nil) : .command(line: line),119                               customTitle: "Agent \(index + 1)", workingDirectory: cwd, role: "agent")120            }121            let column = LayoutNode.even(agents.map { .pane($0.id) }, axis: .vertical)122            let layout = LayoutNode.weighted([(.pane(supervisor.id), 0.45), (column, 0.55)], axis: .horizontal)123            windows = [WindowModel(tabs: [TabModel(customTitle: preset.title, layout: layout,124                                                   panes: [supervisor] + agents, focusedPane: supervisor.id)])]125        case .fourPanes:126            windows = [WindowModel(tabs: [gridTab((0..<4).map { _ in shell() }, columns: 2, title: preset.title)])]127        case .dualMonitor:128            let code = shell("code"), tests = shell("tests")129            let primary = TabModel(customTitle: "Work",130                                   layout: LayoutNode.weighted([(.pane(code.id), 0.62), (.pane(tests.id), 0.38)], axis: .horizontal),131                                   panes: [code, tests], focusedPane: code.id)132            let logs = shell("logs"), monitor = shell("monitor")133            let secondary = TabModel(customTitle: "Monitoring",134                                     layout: LayoutNode.even([.pane(logs.id), .pane(monitor.id)], axis: .vertical),135                                     panes: [logs, monitor], focusedPane: logs.id)136            // With a single display both windows are still created and tiled on that display:137            // nothing is hidden, and plugging a second display later lets the user move one over.138            windows = [WindowModel(tabs: [primary], screenSlot: 0),139                       WindowModel(tabs: [secondary], screenSlot: options.screenCount > 1 ? 1 : 0)]140        case .grid2:141            windows = [WindowModel(tabs: [gridTab((0..<2).map { _ in shell() }, columns: 2, title: preset.title)])]142        case .grid3:143            windows = [WindowModel(tabs: [gridTab((0..<3).map { _ in shell() }, columns: 3, title: preset.title)])]144        case .grid6:145            windows = [WindowModel(tabs: [gridTab((0..<6).map { _ in shell() }, columns: 3, title: preset.title)])]146        case .grid8:147            windows = [WindowModel(tabs: [gridTab((0..<8).map { _ in shell() }, columns: 4, title: preset.title)])]148        }149        return WorkspaceSnapshot(name: preset.title, windows: windows)150    }151}152153/// How many terminal panes a given area can host while each stays usable.154public enum PaneCapacity {155    /// Minimum useful pane: 60 × 12 cells.156    public static let minimumColumns = 60157    public static let minimumRows = 12158159    /// Largest pane count among the supported grids (1, 2, 3, 4, 6, 8) whose cells keep at least160    /// `minimumColumns × minimumRows` characters in `size` (points) for the given cell size.161    public static func recommendedMaximum(for size: CGSize, cellSize: CGSize = CGSize(width: 7.8, height: 16)) -> Int {162        let columns = Int(size.width / max(cellSize.width, 1))163        let rows = Int(size.height / max(cellSize.height, 1))164        let grids: [(count: Int, columns: Int, rows: Int)] = [(8, 4, 2), (6, 3, 2), (4, 2, 2), (3, 3, 1), (2, 2, 1), (1, 1, 1)]165        for grid in grids where columns / grid.columns >= minimumColumns && rows / grid.rows >= minimumRows {166            return grid.count167        }168        return 1169    }170}171
Files main
Menu

Theme