forked from ghostty-org/ghostty
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathWorktrunkPreferences.swift
More file actions
314 lines (276 loc) · 9.4 KB
/
WorktrunkPreferences.swift
File metadata and controls
314 lines (276 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import AppKit
import Foundation
enum WorktrunkAgent: String, CaseIterable, Identifiable {
case claude
case codex
case opencode
var id: String { rawValue }
var title: String {
switch self {
case .claude: return "Claude Code"
case .codex: return "Codex"
case .opencode: return "OpenCode"
}
}
var command: String {
switch self {
case .claude: return "claude"
case .codex: return "codex"
case .opencode: return "opencode"
}
}
var isAvailable: Bool {
Self.isExecutableAvailable(command)
}
static func availableAgents() -> [WorktrunkAgent] {
allCases.filter { $0.isAvailable }
}
static func preferredAgent(from rawValue: String, availableAgents: [WorktrunkAgent]) -> WorktrunkAgent? {
if let preferred = WorktrunkAgent(rawValue: rawValue),
availableAgents.contains(preferred) {
return preferred
}
return availableAgents.first
}
private static func isExecutableAvailable(_ name: String) -> Bool {
let binDir = AgentStatusPaths.binDir.path
for path in searchPaths(excluding: binDir) {
let candidate = URL(fileURLWithPath: path).appendingPathComponent(name).path
if FileManager.default.isExecutableFile(atPath: candidate) {
return true
}
}
return false
}
private static func searchPaths(excluding excludedPath: String) -> [String] {
var paths: [String] = []
let prefix = ["/opt/homebrew/bin", "/usr/local/bin"]
let existingPath = ProcessInfo.processInfo.environment["PATH"] ?? ""
let existingComponents = existingPath.split(separator: ":").map(String.init)
for path in prefix + existingComponents {
let trimmed = path.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { continue }
let normalized = trimmed.hasSuffix("/") ? String(trimmed.dropLast()) : trimmed
guard normalized != excludedPath else { continue }
if !paths.contains(normalized) {
paths.append(normalized)
}
}
return paths
}
}
enum WorktrunkDefaultAction: String, CaseIterable, Identifiable {
case terminal
case claude
case codex
case opencode
var id: String { rawValue }
var title: String {
switch self {
case .terminal: return "Terminal"
case .claude: return "Claude Code"
case .codex: return "Codex"
case .opencode: return "OpenCode"
}
}
var agent: WorktrunkAgent? {
switch self {
case .terminal: return nil
case .claude: return .claude
case .codex: return .codex
case .opencode: return .opencode
}
}
var isAvailable: Bool {
switch self {
case .terminal: return true
default: return agent?.isAvailable ?? false
}
}
static func availableActions() -> [WorktrunkDefaultAction] {
allCases.filter { $0.isAvailable }
}
static func preferredAction(from rawValue: String, availableActions: [WorktrunkDefaultAction]) -> WorktrunkDefaultAction {
if let preferred = WorktrunkDefaultAction(rawValue: rawValue),
availableActions.contains(preferred) {
return preferred
}
return .terminal
}
}
enum WorktrunkOpenBehavior: String, CaseIterable, Identifiable {
case newTab = "new_tab"
case splitRight = "split_right"
case splitDown = "split_down"
var id: String { rawValue }
var title: String {
switch self {
case .newTab: return "New Tab"
case .splitRight: return "Split Right"
case .splitDown: return "Split Down"
}
}
}
enum ExternalEditorCategory: String {
case editors
case git
case finder
}
enum ExternalEditor: String, CaseIterable, Identifiable {
// Editors
case cursor
case vscode
case vscodium
case zed
case sublime
case nova
case textmate
case xcode
// JetBrains
case intellij
case webstorm
case pycharm
case goland
case rubymine
case clion
case rider
case phpstorm
case fleet
// Git clients
case tower
case fork
case gitkraken
case sourcetree
case githubDesktop
// Finder
case finder
var id: String { rawValue }
var category: ExternalEditorCategory {
switch self {
case .tower, .fork, .gitkraken, .sourcetree, .githubDesktop:
return .git
case .finder:
return .finder
default:
return .editors
}
}
var title: String {
switch self {
case .cursor: return "Cursor"
case .vscode: return "VS Code"
case .vscodium: return "VSCodium"
case .zed: return "Zed"
case .sublime: return "Sublime Text"
case .nova: return "Nova"
case .xcode: return "Xcode"
case .textmate: return "TextMate"
case .intellij: return "IntelliJ IDEA"
case .webstorm: return "WebStorm"
case .pycharm: return "PyCharm"
case .goland: return "GoLand"
case .rubymine: return "RubyMine"
case .clion: return "CLion"
case .rider: return "Rider"
case .phpstorm: return "PhpStorm"
case .fleet: return "Fleet"
case .tower: return "Tower"
case .fork: return "Fork"
case .gitkraken: return "GitKraken"
case .sourcetree: return "Sourcetree"
case .githubDesktop: return "GitHub Desktop"
case .finder: return "Finder"
}
}
var bundleIdentifier: String {
switch self {
case .cursor: return "com.todesktop.230313mzl4w4u92"
case .vscode: return "com.microsoft.VSCode"
case .vscodium: return "com.vscodium"
case .zed: return "dev.zed.Zed"
case .sublime: return "com.sublimetext.4"
case .nova: return "com.panic.Nova"
case .xcode: return "com.apple.dt.Xcode"
case .textmate: return "com.macromates.TextMate"
case .intellij: return "com.jetbrains.intellij"
case .webstorm: return "com.jetbrains.WebStorm"
case .pycharm: return "com.jetbrains.pycharm"
case .goland: return "com.jetbrains.goland"
case .rubymine: return "com.jetbrains.rubymine"
case .clion: return "com.jetbrains.CLion"
case .rider: return "com.jetbrains.rider"
case .phpstorm: return "com.jetbrains.PhpStorm"
case .fleet: return "fleet.app"
case .tower: return "com.fournova.Tower3"
case .fork: return "com.DanPristupov.Fork"
case .gitkraken: return "com.axosoft.gitkraken"
case .sourcetree: return "com.torusknot.SourceTreeNotMAS"
case .githubDesktop: return "com.github.GitHubClient"
case .finder: return "com.apple.finder"
}
}
var appURL: URL? {
NSWorkspace.shared.urlForApplication(withBundleIdentifier: bundleIdentifier)
}
var isInstalled: Bool {
appURL != nil
}
var appIcon: NSImage? {
guard let url = appURL else { return nil }
return NSWorkspace.shared.icon(forFile: url.path)
}
static func installedEditors() -> [ExternalEditor] {
allCases.filter { $0.isInstalled }
}
static func installedByCategory() -> [(category: ExternalEditorCategory, editors: [ExternalEditor])] {
let installed = installedEditors()
var result: [(category: ExternalEditorCategory, editors: [ExternalEditor])] = []
for cat in [ExternalEditorCategory.editors, .git, .finder] {
let group = installed.filter { $0.category == cat }
if !group.isEmpty {
result.append((category: cat, editors: group))
}
}
return result
}
}
enum WorktrunkPreferences {
static let openBehaviorKey = "GhosttyWorktrunkOpenBehavior.v1"
static let worktreeTabsKey = "GhosttyWorktreeTabs.v1"
static let sidebarTabsKey = "GhostreeWorktrunkSidebarTabs.v1"
static let defaultAgentKey = "GhosttyWorktrunkDefaultAgent.v1"
static let githubIntegrationKey = "GhostreeGitHubIntegration.v1"
static let lastEditorKey = "GhostreeLastEditor.v1"
static var worktreeTabsEnabled: Bool {
UserDefaults.standard.bool(forKey: worktreeTabsKey)
}
static var sidebarTabsEnabled: Bool {
if !UserDefaults.standard.dictionaryRepresentation().keys.contains(sidebarTabsKey) {
return true
}
return UserDefaults.standard.bool(forKey: sidebarTabsKey)
}
static var githubIntegrationEnabled: Bool {
// Default to true if gh CLI is likely available
if !UserDefaults.standard.dictionaryRepresentation().keys.contains(githubIntegrationKey) {
return true // Default enabled
}
return UserDefaults.standard.bool(forKey: githubIntegrationKey)
}
static var lastEditor: ExternalEditor? {
get {
guard let raw = UserDefaults.standard.string(forKey: lastEditorKey) else { return nil }
return ExternalEditor(rawValue: raw)
}
set {
UserDefaults.standard.set(newValue?.rawValue, forKey: lastEditorKey)
}
}
static var preferredEditor: ExternalEditor? {
let installed = ExternalEditor.installedEditors()
if let last = lastEditor, installed.contains(last) {
return last
}
return installed.first
}
}