Repository Public
1import SwiftUI2import RemoteCore3import WorkspaceCore45/// "Hosts" section of the sidebar: entries discovered from ~/.ssh/config plus manual hosts, with6/// user labels/groups/favourites. Nothing connects until the user asks.7struct HostsSidebarSection: View {8 @Environment(WorkspaceController.self) private var controller9 let windowID: WindowID10 @State private var editing: SSHHost?11 @State private var filter = ""1213 private var hosts: [SSHHost] {14 controller.hosts.values15 .filter { filter.isEmpty || $0.alias.localizedCaseInsensitiveContains(filter) || ($0.label ?? "").localizedCaseInsensitiveContains(filter) || $0.tags.contains { $0.localizedCaseInsensitiveContains(filter) } }16 .sorted { ($0.favorite ? 0 : 1, $0.group ?? "", $0.alias.lowercased()) < ($1.favorite ? 0 : 1, $1.group ?? "", $1.alias.lowercased()) }17 }1819 var body: some View {20 Section {21 if controller.hosts.isEmpty {22 Text("No hosts in ~/.ssh/config").font(.system(size: 11)).foregroundStyle(.secondary)23 } else {24 if controller.hosts.count > 8 {25 TextField("Filter hosts", text: $filter).textFieldStyle(.roundedBorder).controlSize(.small)26 }27 ForEach(groupedHosts, id: \.group) { group in28 if let name = group.group { Text(name).font(.system(size: 10, weight: .semibold)).foregroundStyle(.secondary) }29 ForEach(group.hosts) { host in hostRow(host) }30 }31 }32 } header: {33 HStack {34 Text("Hosts")35 Spacer()36 Button { controller.loadHosts() } label: { Image(systemName: "arrow.clockwise").font(.system(size: 9)) }37 .buttonStyle(.borderless).help("Re-read ~/.ssh/config").accessibilityLabel("Reload hosts")38 Button { editing = SSHHost(alias: "", source: .manual) } label: { Image(systemName: "plus").font(.system(size: 9)) }39 .buttonStyle(.borderless).help("Add host").accessibilityLabel("Add host")40 }41 }42 .sheet(item: $editing) { host in HostEditorSheet(host: host) { saved in controller.saveHost(saved) } }43 }4445 private var groupedHosts: [(group: String?, hosts: [SSHHost])] {46 let grouped = Dictionary(grouping: hosts, by: { $0.group })47 return grouped.keys.sorted { ($0 ?? "~") < ($1 ?? "~") }.map { ($0, grouped[$0]!) }48 }4950 @ViewBuilder51 private func hostRow(_ host: SSHHost) -> some View {52 let live = controller.workspace.panes.values.filter { if case .ssh(let id, _) = $0.profile.kind { return controller.hosts[id]?.id == host.id }; return false }53 .compactMap { controller.registry.session($0.sessionID) }.filter { $0.status.isAlive }.count54 HStack(spacing: 6) {55 Image(systemName: host.favorite ? "star.fill" : "network").font(.system(size: 9)).foregroundStyle(host.favorite ? Color.yellow : Color.secondary)56 .accessibilityLabel(host.favorite ? "favourite host" : "host")57 VStack(alignment: .leading, spacing: 1) {58 Text(host.displayName).font(.system(size: 12)).lineLimit(1)59 Text(host.endpointDescription + (host.tags.isEmpty ? "" : " · " + host.tags.joined(separator: ", "))).font(.system(size: 10)).foregroundStyle(.secondary).lineLimit(1)60 }61 Spacer()62 if live > 0 {63 Text("\(live)").font(.system(size: 9, weight: .medium)).padding(.horizontal, 4).background(Capsule().fill(Color.green.opacity(0.25)))64 .accessibilityLabel("\(live) live connections")65 }66 }67 .contentShape(Rectangle())68 .onTapGesture(count: 2) { controller.newSSHSession(host: host, in: windowID) }69 .contextMenu {70 Button("Connect in New Tab") { controller.newSSHSession(host: host, in: windowID) }71 Button("Connect Split Right") { controller.newSSHSession(host: host, in: windowID, split: .horizontal) }72 if let tmux = host.tmuxSession, !tmux.isEmpty {73 Button("Attach tmux “\(tmux)”") { controller.newSSHSession(host: host, in: windowID, tmux: tmux) }74 }75 Divider()76 Button(host.favorite ? "Remove from Favourites" : "Add to Favourites") { var h = host; h.favorite.toggle(); controller.saveHost(h) }77 Button("Edit Label, Group, Tags…") { editing = host }78 if host.source == .manual { Button("Delete Host", role: .destructive) { controller.deleteManualHost(host) } }79 }80 .help("Double-click to connect. Host keys are verified by OpenSSH in the terminal.")81 }82}8384struct HostEditorSheet: View {85 @Environment(\.dismiss) private var dismiss86 @State var host: SSHHost87 let onSave: (SSHHost) -> Void88 @State private var tagsText = ""8990 var body: some View {91 VStack(alignment: .leading, spacing: 12) {92 Text(host.source == .manual ? "Host" : "Host “\(host.alias)” (from ~/.ssh/config)").font(.headline)93 Form {94 if host.source == .manual {95 TextField("Alias (name shown in NEXTERM)", text: $host.alias)96 TextField("Host name or IP", text: Binding(get: { host.hostName ?? "" }, set: { host.hostName = $0.isEmpty ? nil : $0 }))97 TextField("User", text: Binding(get: { host.user ?? "" }, set: { host.user = $0.isEmpty ? nil : $0 }))98 TextField("Port", value: Binding(get: { host.port ?? 22 }, set: { host.port = $0 }), format: .number)99 }100 TextField("Label", text: Binding(get: { host.label ?? "" }, set: { host.label = $0.isEmpty ? nil : $0 }))101 TextField("Group (e.g. MacLustr)", text: Binding(get: { host.group ?? "" }, set: { host.group = $0.isEmpty ? nil : $0 }))102 TextField("Tags (comma separated)", text: $tagsText)103 TextField("tmux session to attach (optional)", text: Binding(get: { host.tmuxSession ?? "" }, set: { host.tmuxSession = $0.isEmpty ? nil : $0 }))104 TextField("Remote working directory (optional)", text: Binding(get: { host.remoteDirectory ?? "" }, set: { host.remoteDirectory = $0.isEmpty ? nil : $0 }))105 Toggle("Favourite", isOn: $host.favorite)106 }107 Text("Credentials are never stored by NEXTERM: authentication uses your SSH agent and keys.").font(.system(size: 10)).foregroundStyle(.secondary)108 HStack {109 Spacer()110 Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction)111 Button("Save") {112 host.tags = tagsText.split(separator: ",").map { $0.trimmingCharacters(in: .whitespaces) }.filter { !$0.isEmpty }113 if let t = host.tmuxSession, !TmuxCommandBuilder.isValidSessionName(t) { host.tmuxSession = nil }114 onSave(host); dismiss()115 }116 .keyboardShortcut(.defaultAction)117 .disabled(host.alias.trimmingCharacters(in: .whitespaces).isEmpty)118 }119 }120 .padding(16)121 .frame(width: 440)122 .onAppear { tagsText = host.tags.joined(separator: ", ") }123 }124}125