From c39704c0d06df9c996b490b63067b1032b1f5e8d Mon Sep 17 00:00:00 2001 From: Lawrence Chen <54008264+lawrencecchen@users.noreply.github.com> Date: Fri, 20 Feb 2026 02:49:20 -0800 Subject: [PATCH 1/5] Sidebar ports on own line, wider sidebar, CMUX_PORT env vars - Move listening ports to dedicated sidebar row (removed from branch/directory line) - Allow sidebar to resize up to 2/3 of screen width (was capped at 360px) - Add CMUX_PORT, CMUX_PORT_END, CMUX_PORT_RANGE env vars per workspace - Each workspace gets a dedicated port range (default: base 9100, range 10) - Add settings UI for port base and range size - Add portOrdinal to Workspace, monotonic counter in TabManager Closes #129 --- Sources/ContentView.swift | 20 ++++++++++++-------- Sources/GhosttyTerminalView.swift | 14 ++++++++++++++ Sources/Panels/TerminalPanel.swift | 4 +++- Sources/TabManager.swift | 5 +++++ Sources/Workspace.swift | 21 +++++++++++++++------ Sources/cmuxApp.swift | 22 ++++++++++++++++++++++ 6 files changed, 71 insertions(+), 15 deletions(-) diff --git a/Sources/ContentView.swift b/Sources/ContentView.swift index 4e899f92c179..a18fad616a4e 100644 --- a/Sources/ContentView.swift +++ b/Sources/ContentView.swift @@ -462,7 +462,8 @@ struct ContentView: View { isResizerHovering = true } } - let nextWidth = max(186, min(360, value.location.x - sidebarMinX + sidebarHandleWidth / 2)) + let maxSidebarWidth = (NSScreen.main?.frame.width ?? 1920) * 2 / 3 + let nextWidth = max(186, min(maxSidebarWidth, value.location.x - sidebarMinX + sidebarHandleWidth / 2)) withTransaction(Transaction(animation: nil)) { sidebarWidth = nextWidth } @@ -1564,7 +1565,7 @@ private struct TabItemView: View { .transition(.opacity.combined(with: .move(edge: .top))) } - // Branch + directory + ports row + // Branch + directory row if let dirRow = branchDirectoryRow { HStack(spacing: 3) { if sidebarShowGitBranch && tab.gitBranch != nil && sidebarShowGitBranchIcon { @@ -1579,6 +1580,15 @@ private struct TabItemView: View { .truncationMode(.tail) } } + + // Ports row + if sidebarShowPorts, !tab.listeningPorts.isEmpty { + Text(tab.listeningPorts.map { ":\($0)" }.joined(separator: ", ")) + .font(.system(size: 10, design: .monospaced)) + .foregroundColor(isActive ? .white.opacity(0.75) : .secondary) + .lineLimit(1) + .truncationMode(.tail) + } } .animation(.easeInOut(duration: 0.2), value: tab.logEntries.count) .animation(.easeInOut(duration: 0.2), value: tab.progress != nil) @@ -1901,12 +1911,6 @@ private struct TabItemView: View { parts.append(dirs) } - // Ports (if enabled and available) - if sidebarShowPorts, !tab.listeningPorts.isEmpty { - let portsStr = tab.listeningPorts.map { ":\($0)" }.joined(separator: ",") - parts.append(portsStr) - } - let result = parts.joined(separator: " ยท ") return result.isEmpty ? nil : result } diff --git a/Sources/GhosttyTerminalView.swift b/Sources/GhosttyTerminalView.swift index 2507edb79455..610a90c62fd8 100644 --- a/Sources/GhosttyTerminalView.swift +++ b/Sources/GhosttyTerminalView.swift @@ -1007,6 +1007,8 @@ final class TerminalSurface: Identifiable, ObservableObject { var isViewInWindow: Bool { hostedView.window != nil } let id: UUID private(set) var tabId: UUID + /// Port ordinal for CMUX_PORT range assignment + var portOrdinal: Int = 0 private let surfaceContext: ghostty_surface_context_e private let configTemplate: ghostty_surface_config_s? private let workingDirectory: String? @@ -1246,6 +1248,18 @@ final class TerminalSurface: Identifiable, ObservableObject { env["CMUX_TAB_ID"] = tabId.uuidString env["CMUX_SOCKET_PATH"] = SocketControlSettings.socketPath() + // Port range for this workspace + do { + let portBase = UserDefaults.standard.integer(forKey: "cmuxPortBase") + let portRange = UserDefaults.standard.integer(forKey: "cmuxPortRange") + let effectiveBase = portBase > 0 ? portBase : 9100 + let effectiveRange = portRange > 0 ? portRange : 10 + let startPort = effectiveBase + portOrdinal * effectiveRange + env["CMUX_PORT"] = String(startPort) + env["CMUX_PORT_END"] = String(startPort + effectiveRange - 1) + env["CMUX_PORT_RANGE"] = String(effectiveRange) + } + let claudeHooksEnabled = UserDefaults.standard.object(forKey: "claudeCodeHooksEnabled") as? Bool ?? true if !claudeHooksEnabled { env["CMUX_CLAUDE_HOOKS_DISABLED"] = "1" diff --git a/Sources/Panels/TerminalPanel.swift b/Sources/Panels/TerminalPanel.swift index 1a1332287bf7..b9a9d7671d65 100644 --- a/Sources/Panels/TerminalPanel.swift +++ b/Sources/Panels/TerminalPanel.swift @@ -82,7 +82,8 @@ final class TerminalPanel: Panel, ObservableObject { workspaceId: UUID, context: ghostty_surface_context_e = GHOSTTY_SURFACE_CONTEXT_SPLIT, configTemplate: ghostty_surface_config_s? = nil, - workingDirectory: String? = nil + workingDirectory: String? = nil, + portOrdinal: Int = 0 ) { let surface = TerminalSurface( tabId: workspaceId, @@ -90,6 +91,7 @@ final class TerminalPanel: Panel, ObservableObject { configTemplate: configTemplate, workingDirectory: workingDirectory ) + surface.portOrdinal = portOrdinal self.init(workspaceId: workspaceId, surface: surface) } diff --git a/Sources/TabManager.swift b/Sources/TabManager.swift index f3c435225527..3e200105c429 100644 --- a/Sources/TabManager.swift +++ b/Sources/TabManager.swift @@ -226,6 +226,9 @@ fileprivate func cmuxVsyncIOSurfaceTimelineCallback( class TabManager: ObservableObject { @Published var tabs: [Workspace] = [] @Published private(set) var isWorkspaceCycleHot: Bool = false + + /// Monotonically increasing counter for CMUX_PORT ordinal assignment + private var nextPortOrdinal: Int = 0 @Published var selectedTabId: UUID? { didSet { guard selectedTabId != oldValue else { return } @@ -395,6 +398,8 @@ class TabManager: ObservableObject { func addWorkspace(workingDirectory overrideWorkingDirectory: String? = nil) -> Workspace { let workingDirectory = normalizedWorkingDirectory(overrideWorkingDirectory) ?? preferredWorkingDirectoryForNewTab() let newWorkspace = Workspace(title: "Terminal \(tabs.count + 1)", workingDirectory: workingDirectory) + newWorkspace.portOrdinal = nextPortOrdinal + nextPortOrdinal += 1 let insertIndex = newTabInsertIndex() if insertIndex >= 0 && insertIndex <= tabs.count { tabs.insert(newWorkspace, at: insertIndex) diff --git a/Sources/Workspace.swift b/Sources/Workspace.swift index 9709f541cf17..e44e2bec37f1 100644 --- a/Sources/Workspace.swift +++ b/Sources/Workspace.swift @@ -46,6 +46,9 @@ final class Workspace: Identifiable, ObservableObject { @Published var isPinned: Bool = false @Published var currentDirectory: String + /// Ordinal for CMUX_PORT range assignment (monotonically increasing per app session) + var portOrdinal: Int = 0 + /// The bonsplit controller managing the split panes for this workspace let bonsplitController: BonsplitController @@ -138,7 +141,8 @@ final class Workspace: Identifiable, ObservableObject { let terminalPanel = TerminalPanel( workspaceId: id, context: GHOSTTY_SURFACE_CONTEXT_TAB, - workingDirectory: hasWorkingDirectory ? trimmedWorkingDirectory : nil + workingDirectory: hasWorkingDirectory ? trimmedWorkingDirectory : nil, + portOrdinal: portOrdinal ) panels[terminalPanel.id] = terminalPanel @@ -382,7 +386,8 @@ final class Workspace: Identifiable, ObservableObject { let newPanel = TerminalPanel( workspaceId: id, context: GHOSTTY_SURFACE_CONTEXT_SPLIT, - configTemplate: inheritedConfig + configTemplate: inheritedConfig, + portOrdinal: portOrdinal ) panels[newPanel.id] = newPanel @@ -447,7 +452,8 @@ final class Workspace: Identifiable, ObservableObject { let newPanel = TerminalPanel( workspaceId: id, context: GHOSTTY_SURFACE_CONTEXT_SPLIT, - configTemplate: inheritedConfig + configTemplate: inheritedConfig, + portOrdinal: portOrdinal ) panels[newPanel.id] = newPanel @@ -974,7 +980,8 @@ final class Workspace: Identifiable, ObservableObject { let newPanel = TerminalPanel( workspaceId: id, context: GHOSTTY_SURFACE_CONTEXT_TAB, - configTemplate: nil + configTemplate: nil, + portOrdinal: portOrdinal ) panels[newPanel.id] = newPanel @@ -1439,7 +1446,8 @@ extension Workspace: BonsplitDelegate { let replacementPanel = TerminalPanel( workspaceId: id, context: GHOSTTY_SURFACE_CONTEXT_SPLIT, - configTemplate: inheritedConfig + configTemplate: inheritedConfig, + portOrdinal: portOrdinal ) panels[replacementPanel.id] = replacementPanel surfaceIdToPanelId[replacementTab.id] = replacementPanel.id @@ -1497,7 +1505,8 @@ extension Workspace: BonsplitDelegate { let newPanel = TerminalPanel( workspaceId: id, context: GHOSTTY_SURFACE_CONTEXT_SPLIT, - configTemplate: inheritedConfig + configTemplate: inheritedConfig, + portOrdinal: portOrdinal ) panels[newPanel.id] = newPanel diff --git a/Sources/cmuxApp.swift b/Sources/cmuxApp.swift index 973409e9237b..bff05e5bdd1c 100644 --- a/Sources/cmuxApp.swift +++ b/Sources/cmuxApp.swift @@ -2236,6 +2236,8 @@ struct SettingsView: View { @AppStorage("appearanceMode") private var appearanceMode = AppearanceMode.dark.rawValue @AppStorage(SocketControlSettings.appStorageKey) private var socketControlMode = SocketControlSettings.defaultMode.rawValue @AppStorage("claudeCodeHooksEnabled") private var claudeCodeHooksEnabled = true + @AppStorage("cmuxPortBase") private var cmuxPortBase = 9100 + @AppStorage("cmuxPortRange") private var cmuxPortRange = 10 @AppStorage(BrowserSearchSettings.searchEngineKey) private var browserSearchEngine = BrowserSearchSettings.defaultSearchEngine.rawValue @AppStorage(BrowserSearchSettings.searchSuggestionsEnabledKey) private var browserSearchSuggestionsEnabled = BrowserSearchSettings.defaultSearchSuggestionsEnabled @AppStorage(NotificationBadgeSettings.dockBadgeEnabledKey) private var notificationDockBadgeEnabled = NotificationBadgeSettings.defaultDockBadgeEnabled @@ -2377,6 +2379,26 @@ struct SettingsView: View { SettingsCardNote("When enabled, cmux wraps the claude command to inject session tracking and notification hooks. Disable if you prefer to manage Claude Code hooks yourself.") } + SettingsCard { + SettingsCardRow("Port Base", subtitle: "Starting port for CMUX_PORT env var.", controlWidth: pickerColumnWidth) { + TextField("", value: $cmuxPortBase, format: .number) + .textFieldStyle(.roundedBorder) + .multilineTextAlignment(.trailing) + } + + SettingsCardDivider() + + SettingsCardRow("Port Range Size", subtitle: "Number of ports per workspace.", controlWidth: pickerColumnWidth) { + TextField("", value: $cmuxPortRange, format: .number) + .textFieldStyle(.roundedBorder) + .multilineTextAlignment(.trailing) + } + + SettingsCardDivider() + + SettingsCardNote("Each workspace gets CMUX_PORT and CMUX_PORT_END env vars with a dedicated port range. New terminals inherit these values.") + } + SettingsSectionHeader(title: "Browser") SettingsCard { SettingsCardRow( From e3bf6a0ea6a9bb5b16f87ef96c7118f3f29dd606 Mon Sep 17 00:00:00 2001 From: Lawrence Chen <54008264+lawrencecchen@users.noreply.github.com> Date: Fri, 20 Feb 2026 03:10:24 -0800 Subject: [PATCH 2/5] Make port ordinal counter static to avoid overlap across windows Each window creates its own TabManager, so a per-instance counter would reset and reuse port ranges. Making it static ensures unique ranges across all windows. --- Sources/TabManager.swift | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Sources/TabManager.swift b/Sources/TabManager.swift index 3e200105c429..264b41942471 100644 --- a/Sources/TabManager.swift +++ b/Sources/TabManager.swift @@ -227,8 +227,9 @@ class TabManager: ObservableObject { @Published var tabs: [Workspace] = [] @Published private(set) var isWorkspaceCycleHot: Bool = false - /// Monotonically increasing counter for CMUX_PORT ordinal assignment - private var nextPortOrdinal: Int = 0 + /// Global monotonically increasing counter for CMUX_PORT ordinal assignment. + /// Static so port ranges don't overlap across multiple windows (each window has its own TabManager). + private static var nextPortOrdinal: Int = 0 @Published var selectedTabId: UUID? { didSet { guard selectedTabId != oldValue else { return } @@ -398,8 +399,8 @@ class TabManager: ObservableObject { func addWorkspace(workingDirectory overrideWorkingDirectory: String? = nil) -> Workspace { let workingDirectory = normalizedWorkingDirectory(overrideWorkingDirectory) ?? preferredWorkingDirectoryForNewTab() let newWorkspace = Workspace(title: "Terminal \(tabs.count + 1)", workingDirectory: workingDirectory) - newWorkspace.portOrdinal = nextPortOrdinal - nextPortOrdinal += 1 + newWorkspace.portOrdinal = Self.nextPortOrdinal + Self.nextPortOrdinal += 1 let insertIndex = newTabInsertIndex() if insertIndex >= 0 && insertIndex <= tabs.count { tabs.insert(newWorkspace, at: insertIndex) From 91cad20f615a416eed2bf2eb237c916f8ec43cdc Mon Sep 17 00:00:00 2001 From: Lawrence Chen <54008264+lawrencecchen@users.noreply.github.com> Date: Fri, 20 Feb 2026 03:19:49 -0800 Subject: [PATCH 3/5] Fix portOrdinal race: pass through Workspace init instead of setting after The first TerminalPanel is created inside Workspace.init, so setting portOrdinal after init returns meant the initial terminal always got ordinal 0. Pass portOrdinal as an init parameter and set it before the TerminalPanel is created. --- Sources/TabManager.swift | 4 ++-- Sources/Workspace.swift | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Sources/TabManager.swift b/Sources/TabManager.swift index 264b41942471..b54323b00144 100644 --- a/Sources/TabManager.swift +++ b/Sources/TabManager.swift @@ -398,9 +398,9 @@ class TabManager: ObservableObject { @discardableResult func addWorkspace(workingDirectory overrideWorkingDirectory: String? = nil) -> Workspace { let workingDirectory = normalizedWorkingDirectory(overrideWorkingDirectory) ?? preferredWorkingDirectoryForNewTab() - let newWorkspace = Workspace(title: "Terminal \(tabs.count + 1)", workingDirectory: workingDirectory) - newWorkspace.portOrdinal = Self.nextPortOrdinal + let ordinal = Self.nextPortOrdinal Self.nextPortOrdinal += 1 + let newWorkspace = Workspace(title: "Terminal \(tabs.count + 1)", workingDirectory: workingDirectory, portOrdinal: ordinal) let insertIndex = newTabInsertIndex() if insertIndex >= 0 && insertIndex <= tabs.count { tabs.insert(newWorkspace, at: insertIndex) diff --git a/Sources/Workspace.swift b/Sources/Workspace.swift index e44e2bec37f1..f4edcd7bc9dd 100644 --- a/Sources/Workspace.swift +++ b/Sources/Workspace.swift @@ -104,8 +104,9 @@ final class Workspace: Identifiable, ObservableObject { // MARK: - Initialization - init(title: String = "Terminal", workingDirectory: String? = nil) { + init(title: String = "Terminal", workingDirectory: String? = nil, portOrdinal: Int = 0) { self.id = UUID() + self.portOrdinal = portOrdinal self.processTitle = title self.title = title self.customTitle = nil From f8e645525befef23aca31a9da6d95cffe4d5c2bf Mon Sep 17 00:00:00 2001 From: Lawrence Chen <54008264+lawrencecchen@users.noreply.github.com> Date: Fri, 20 Feb 2026 04:10:12 -0800 Subject: [PATCH 4/5] Fix P2/P3: snapshot port settings at surface creation, use window screen for sidebar cap P2: Port base/range are now snapshotted on TerminalSurface when the panel is created, so changing settings mid-session won't cause inconsistent CMUX_PORT values across terminals in the same workspace. P3: Sidebar max width now uses NSApp.keyWindow?.screen instead of NSScreen.main, so multi-monitor setups get the correct 2/3 cap for the display the window is actually on. --- Sources/ContentView.swift | 2 +- Sources/GhosttyTerminalView.swift | 9 +++++---- Sources/Panels/TerminalPanel.swift | 2 ++ 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Sources/ContentView.swift b/Sources/ContentView.swift index a18fad616a4e..ff1f88357c5a 100644 --- a/Sources/ContentView.swift +++ b/Sources/ContentView.swift @@ -462,7 +462,7 @@ struct ContentView: View { isResizerHovering = true } } - let maxSidebarWidth = (NSScreen.main?.frame.width ?? 1920) * 2 / 3 + let maxSidebarWidth = (NSApp.keyWindow?.screen?.frame.width ?? NSScreen.main?.frame.width ?? 1920) * 2 / 3 let nextWidth = max(186, min(maxSidebarWidth, value.location.x - sidebarMinX + sidebarHandleWidth / 2)) withTransaction(Transaction(animation: nil)) { sidebarWidth = nextWidth diff --git a/Sources/GhosttyTerminalView.swift b/Sources/GhosttyTerminalView.swift index 610a90c62fd8..747b03545c54 100644 --- a/Sources/GhosttyTerminalView.swift +++ b/Sources/GhosttyTerminalView.swift @@ -1009,6 +1009,9 @@ final class TerminalSurface: Identifiable, ObservableObject { private(set) var tabId: UUID /// Port ordinal for CMUX_PORT range assignment var portOrdinal: Int = 0 + /// Snapshotted at surface creation time so mid-session settings changes don't cause inconsistency + var portBase: Int = 0 + var portRangeSize: Int = 0 private let surfaceContext: ghostty_surface_context_e private let configTemplate: ghostty_surface_config_s? private let workingDirectory: String? @@ -1248,12 +1251,10 @@ final class TerminalSurface: Identifiable, ObservableObject { env["CMUX_TAB_ID"] = tabId.uuidString env["CMUX_SOCKET_PATH"] = SocketControlSettings.socketPath() - // Port range for this workspace + // Port range for this workspace (values snapshotted at surface init) do { - let portBase = UserDefaults.standard.integer(forKey: "cmuxPortBase") - let portRange = UserDefaults.standard.integer(forKey: "cmuxPortRange") let effectiveBase = portBase > 0 ? portBase : 9100 - let effectiveRange = portRange > 0 ? portRange : 10 + let effectiveRange = portRangeSize > 0 ? portRangeSize : 10 let startPort = effectiveBase + portOrdinal * effectiveRange env["CMUX_PORT"] = String(startPort) env["CMUX_PORT_END"] = String(startPort + effectiveRange - 1) diff --git a/Sources/Panels/TerminalPanel.swift b/Sources/Panels/TerminalPanel.swift index b9a9d7671d65..7cc83729711f 100644 --- a/Sources/Panels/TerminalPanel.swift +++ b/Sources/Panels/TerminalPanel.swift @@ -92,6 +92,8 @@ final class TerminalPanel: Panel, ObservableObject { workingDirectory: workingDirectory ) surface.portOrdinal = portOrdinal + surface.portBase = UserDefaults.standard.integer(forKey: "cmuxPortBase") + surface.portRangeSize = UserDefaults.standard.integer(forKey: "cmuxPortRange") self.init(workspaceId: workspaceId, surface: surface) } From 3542434329f1a483b3f1dd24293c17a0f64e4252 Mon Sep 17 00:00:00 2001 From: Lawrence Chen <54008264+lawrencecchen@users.noreply.github.com> Date: Fri, 20 Feb 2026 04:27:40 -0800 Subject: [PATCH 5/5] Fix P1: snapshot port base/range once per app session, not per panel Port base and range size are now static properties on TerminalSurface, initialized once from UserDefaults at first access. This prevents overlapping port ranges across workspaces when settings are changed mid-session (e.g., workspace 1 with range=10 at 9110-9119, then range changed to 5, workspace 2 would overlap at 9110). --- Sources/GhosttyTerminalView.swift | 22 +++++++++++++--------- Sources/Panels/TerminalPanel.swift | 2 -- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Sources/GhosttyTerminalView.swift b/Sources/GhosttyTerminalView.swift index 747b03545c54..0b7c9775ea72 100644 --- a/Sources/GhosttyTerminalView.swift +++ b/Sources/GhosttyTerminalView.swift @@ -1009,9 +1009,15 @@ final class TerminalSurface: Identifiable, ObservableObject { private(set) var tabId: UUID /// Port ordinal for CMUX_PORT range assignment var portOrdinal: Int = 0 - /// Snapshotted at surface creation time so mid-session settings changes don't cause inconsistency - var portBase: Int = 0 - var portRangeSize: Int = 0 + /// Snapshotted once per app session so all workspaces use consistent values + private static let sessionPortBase: Int = { + let val = UserDefaults.standard.integer(forKey: "cmuxPortBase") + return val > 0 ? val : 9100 + }() + private static let sessionPortRangeSize: Int = { + let val = UserDefaults.standard.integer(forKey: "cmuxPortRange") + return val > 0 ? val : 10 + }() private let surfaceContext: ghostty_surface_context_e private let configTemplate: ghostty_surface_config_s? private let workingDirectory: String? @@ -1251,14 +1257,12 @@ final class TerminalSurface: Identifiable, ObservableObject { env["CMUX_TAB_ID"] = tabId.uuidString env["CMUX_SOCKET_PATH"] = SocketControlSettings.socketPath() - // Port range for this workspace (values snapshotted at surface init) + // Port range for this workspace (base/range snapshotted once per app session) do { - let effectiveBase = portBase > 0 ? portBase : 9100 - let effectiveRange = portRangeSize > 0 ? portRangeSize : 10 - let startPort = effectiveBase + portOrdinal * effectiveRange + let startPort = Self.sessionPortBase + portOrdinal * Self.sessionPortRangeSize env["CMUX_PORT"] = String(startPort) - env["CMUX_PORT_END"] = String(startPort + effectiveRange - 1) - env["CMUX_PORT_RANGE"] = String(effectiveRange) + env["CMUX_PORT_END"] = String(startPort + Self.sessionPortRangeSize - 1) + env["CMUX_PORT_RANGE"] = String(Self.sessionPortRangeSize) } let claudeHooksEnabled = UserDefaults.standard.object(forKey: "claudeCodeHooksEnabled") as? Bool ?? true diff --git a/Sources/Panels/TerminalPanel.swift b/Sources/Panels/TerminalPanel.swift index 7cc83729711f..b9a9d7671d65 100644 --- a/Sources/Panels/TerminalPanel.swift +++ b/Sources/Panels/TerminalPanel.swift @@ -92,8 +92,6 @@ final class TerminalPanel: Panel, ObservableObject { workingDirectory: workingDirectory ) surface.portOrdinal = portOrdinal - surface.portBase = UserDefaults.standard.integer(forKey: "cmuxPortBase") - surface.portRangeSize = UserDefaults.standard.integer(forKey: "cmuxPortRange") self.init(workspaceId: workspaceId, surface: surface) }