Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ struct ContentView: View {
isResizerHovering = true
}
}
let nextWidth = max(186, min(360, value.location.x - sidebarMinX + sidebarHandleWidth / 2))
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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 15 additions & 0 deletions Sources/GhosttyTerminalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,11 @@ 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
/// 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?
Expand Down Expand Up @@ -1246,6 +1251,16 @@ 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)
do {
let effectiveBase = portBase > 0 ? portBase : 9100
let effectiveRange = portRangeSize > 0 ? portRangeSize : 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"
Expand Down
6 changes: 5 additions & 1 deletion Sources/Panels/TerminalPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,18 @@ 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,
context: context,
configTemplate: configTemplate,
workingDirectory: workingDirectory
)
surface.portOrdinal = portOrdinal
surface.portBase = UserDefaults.standard.integer(forKey: "cmuxPortBase")
surface.portRangeSize = UserDefaults.standard.integer(forKey: "cmuxPortRange")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep port range immutable for ordinal mapping

Reading cmuxPortBase/cmuxPortRange from UserDefaults for each new panel makes the startPort = base + portOrdinal * range calculation in TerminalSurface.createSurface depend on mutable settings, so changing range/base mid-session can assign overlapping ranges to different workspaces (for example, workspace ordinal 1 created with range 10 uses 9110-9119, then after switching range to 5, ordinal 2 starts at 9110). This breaks the dedicated non-overlapping workspace port contract and can cause cross-workspace port conflicts.

Useful? React with 👍 / 👎.

self.init(workspaceId: workspaceId, surface: surface)
}

Expand Down
8 changes: 7 additions & 1 deletion Sources/TabManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ fileprivate func cmuxVsyncIOSurfaceTimelineCallback(
class TabManager: ObservableObject {
@Published var tabs: [Workspace] = []
@Published private(set) var isWorkspaceCycleHot: Bool = false

/// 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 }
Expand Down Expand Up @@ -394,7 +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)
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)
Expand Down
24 changes: 17 additions & 7 deletions Sources/Workspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -101,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
Expand Down Expand Up @@ -138,7 +142,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

Expand Down Expand Up @@ -382,7 +387,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

Expand Down Expand Up @@ -447,7 +453,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

Expand Down Expand Up @@ -974,7 +981,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

Expand Down Expand Up @@ -1439,7 +1447,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
Expand Down Expand Up @@ -1497,7 +1506,8 @@ extension Workspace: BonsplitDelegate {
let newPanel = TerminalPanel(
workspaceId: id,
context: GHOSTTY_SURFACE_CONTEXT_SPLIT,
configTemplate: inheritedConfig
configTemplate: inheritedConfig,
portOrdinal: portOrdinal
)
panels[newPanel.id] = newPanel

Expand Down
22 changes: 22 additions & 0 deletions Sources/cmuxApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down