tabherd 0.0.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +166 -0
- package/LICENSE +674 -0
- package/README.md +538 -2
- package/launcher/npm.js +9 -0
- package/lib/agents.js +324 -0
- package/lib/codex-client.js +306 -0
- package/lib/codex-rollout.js +251 -0
- package/lib/codex.js +424 -0
- package/lib/comfy.js +479 -0
- package/lib/completed.js +55 -0
- package/lib/copilot.js +359 -0
- package/lib/cursor.js +516 -0
- package/lib/dismissed.js +30 -0
- package/lib/filters.js +120 -0
- package/lib/groups.js +144 -0
- package/lib/history.js +253 -0
- package/lib/llamacpp.js +159 -0
- package/lib/lmstudio.js +134 -0
- package/lib/localai.js +291 -0
- package/lib/memory.js +55 -0
- package/lib/ollama.js +124 -0
- package/lib/open-dashboard.js +39 -0
- package/lib/opencode.js +371 -0
- package/lib/repos.js +81 -0
- package/lib/saved.js +33 -0
- package/lib/secrets.js +196 -0
- package/lib/session-id.js +9 -0
- package/lib/settings.js +231 -0
- package/lib/store.js +44 -0
- package/lib/terminals/index.js +62 -0
- package/lib/terminals/kitty.js +126 -0
- package/lib/terminals/linux.js +310 -0
- package/lib/terminals/macos.js +468 -0
- package/lib/terminals/tmux.js +89 -0
- package/lib/terminals/unix-driver.js +172 -0
- package/lib/terminals/unix.js +204 -0
- package/lib/terminals/unsupported.js +22 -0
- package/lib/terminals/wezterm.js +103 -0
- package/lib/terminals/windows-close.ps1 +125 -0
- package/lib/terminals/windows-focus.ps1 +235 -0
- package/lib/terminals/windows-pick-folder.ps1 +57 -0
- package/lib/terminals/windows-titles.js +124 -0
- package/lib/terminals/windows-titles.ps1 +95 -0
- package/lib/terminals/windows.js +166 -0
- package/lib/ticket-refs.js +184 -0
- package/lib/tickets.js +437 -0
- package/lib/todos.js +166 -0
- package/lib/trackers.js +541 -0
- package/lib/transcript.js +314 -0
- package/lib/updates.js +186 -0
- package/package.json +35 -7
- package/public/app.js +4077 -0
- package/public/favicon.svg +1 -0
- package/public/icons/tabherd-128.png +0 -0
- package/public/icons/tabherd-16.png +0 -0
- package/public/icons/tabherd-20.png +0 -0
- package/public/icons/tabherd-24.png +0 -0
- package/public/icons/tabherd-256.png +0 -0
- package/public/icons/tabherd-32.png +0 -0
- package/public/icons/tabherd-40.png +0 -0
- package/public/icons/tabherd-48.png +0 -0
- package/public/icons/tabherd-512.png +0 -0
- package/public/icons/tabherd-64.png +0 -0
- package/public/index.html +655 -0
- package/public/lang/es.js +742 -0
- package/public/style.css +1443 -0
- package/server.js +814 -0
- package/placeholder.js +0 -3
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// macOS. Each terminal app is a backend (see unix-driver.js); sessions inside
|
|
4
|
+
// tmux are handled by tmux whatever terminal shows it. Terminal and iTerm2 are
|
|
5
|
+
// driven with AppleScript, through osascript; the first time, macOS asks to
|
|
6
|
+
// let Tabherd control them.
|
|
7
|
+
|
|
8
|
+
const fs = require('fs');
|
|
9
|
+
const os = require('os');
|
|
10
|
+
const path = require('path');
|
|
11
|
+
const unix = require('./unix');
|
|
12
|
+
const tmux = require('./tmux');
|
|
13
|
+
const kitty = require('./kitty');
|
|
14
|
+
const wezterm = require('./wezterm');
|
|
15
|
+
const { create } = require('./unix-driver');
|
|
16
|
+
|
|
17
|
+
// Where an app may be installed.
|
|
18
|
+
function appPath(bundle) {
|
|
19
|
+
for (const dir of ['/Applications', path.join(os.homedir(), 'Applications'), '/System/Applications/Utilities', '/Applications/Utilities']) {
|
|
20
|
+
const file = path.join(dir, bundle);
|
|
21
|
+
if (fs.existsSync(file)) return file;
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Runs an AppleScript, one line per -e, with argv as its arguments, so
|
|
27
|
+
// nothing passed in is ever read as script. The first call to an app waits on
|
|
28
|
+
// macOS's prompt asking whether to allow it, hence the long timeout.
|
|
29
|
+
async function osascript(script, argv = [], { timeout = 3 * 60 * 1000 } = {}) {
|
|
30
|
+
const lines = script.trim().split('\n');
|
|
31
|
+
const out = await unix.run('osascript', [...lines.flatMap((l) => ['-e', l]), ...argv], { timeout });
|
|
32
|
+
if (/-1743/.test(out.stderr)) {
|
|
33
|
+
return { ok: false, error: "macOS hasn't allowed Tabherd to control this terminal. Turn it on in System Settings › Privacy & Security › Automation." };
|
|
34
|
+
}
|
|
35
|
+
if (!out.ok) return { ok: false, error: out.stderr.replace(/^.*execution error: /s, '').trim() || 'AppleScript failed.' };
|
|
36
|
+
return { ok: true, value: out.stdout.trim() };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const ESC = '\u001b';
|
|
40
|
+
// Text marked as a paste, so the Enter after it is an Enter: Claude Code takes
|
|
41
|
+
// keys that arrive with unmarked text as part of a paste.
|
|
42
|
+
const asPaste = (text) => `${ESC}[200~${text.replace(/\u001b/g, '')}${ESC}[201~`;
|
|
43
|
+
|
|
44
|
+
// The line a new tab or window types: `command` in `dir`. Terminal and iTerm2
|
|
45
|
+
// run it in a login shell, which stays open afterwards.
|
|
46
|
+
const typedLine = (dir, command) => `cd ${unix.shQuote(dir)} && ${command}`;
|
|
47
|
+
|
|
48
|
+
// Terminal. It can't open a tab from a script without the Accessibility
|
|
49
|
+
// permission, so new sessions open in a window. A closed tab's device can be
|
|
50
|
+
// reused, so a tab only counts while it has processes.
|
|
51
|
+
//
|
|
52
|
+
// Window IDs are fetched into a list first and looped over by index: looping
|
|
53
|
+
// over "id of every window" directly asks Terminal for "item 1 of id of every
|
|
54
|
+
// window", which it can't answer.
|
|
55
|
+
const FIND_TERMINAL_TAB = `
|
|
56
|
+
on findTab(want)
|
|
57
|
+
tell application id "com.apple.Terminal"
|
|
58
|
+
set wids to id of every window
|
|
59
|
+
repeat with k from 1 to count of wids
|
|
60
|
+
set wid to item k of wids
|
|
61
|
+
try
|
|
62
|
+
set ttys to tty of every tab of window id wid
|
|
63
|
+
repeat with i from 1 to count of ttys
|
|
64
|
+
if item i of ttys is want then
|
|
65
|
+
if (count of (processes of tab i of window id wid)) > 0 then return {wid, i}
|
|
66
|
+
end if
|
|
67
|
+
end repeat
|
|
68
|
+
end try
|
|
69
|
+
end repeat
|
|
70
|
+
end tell
|
|
71
|
+
return missing value
|
|
72
|
+
end findTab`;
|
|
73
|
+
|
|
74
|
+
const terminalApp = {
|
|
75
|
+
id: 'terminal',
|
|
76
|
+
name: 'Terminal',
|
|
77
|
+
processNames: ['Terminal'],
|
|
78
|
+
available: () => true,
|
|
79
|
+
hosts: (ctx) => ctx.chain.some((p) => p.name === 'Terminal'),
|
|
80
|
+
async open(dir, command) {
|
|
81
|
+
const result = await osascript(
|
|
82
|
+
`
|
|
83
|
+
on run argv
|
|
84
|
+
tell application id "com.apple.Terminal"
|
|
85
|
+
do script (item 1 of argv)
|
|
86
|
+
activate
|
|
87
|
+
end tell
|
|
88
|
+
end run`,
|
|
89
|
+
[typedLine(dir, command)],
|
|
90
|
+
);
|
|
91
|
+
return result.ok ? { ok: true } : result;
|
|
92
|
+
},
|
|
93
|
+
async focus(ctx) {
|
|
94
|
+
const result = await osascript(
|
|
95
|
+
`${FIND_TERMINAL_TAB}
|
|
96
|
+
on run argv
|
|
97
|
+
set loc to my findTab(item 1 of argv)
|
|
98
|
+
if loc is missing value then return "not_found"
|
|
99
|
+
set {wid, i} to loc
|
|
100
|
+
tell application id "com.apple.Terminal"
|
|
101
|
+
try
|
|
102
|
+
set miniaturized of window id wid to false
|
|
103
|
+
end try
|
|
104
|
+
set selected of tab i of window id wid to true
|
|
105
|
+
set index of window id wid to 1
|
|
106
|
+
activate
|
|
107
|
+
end tell
|
|
108
|
+
return "ok"
|
|
109
|
+
end run`,
|
|
110
|
+
[ctx.tty],
|
|
111
|
+
);
|
|
112
|
+
if (!result.ok) return result;
|
|
113
|
+
if (result.value === 'not_found') return { ok: false, error: "Terminal has no tab for this session. It may be in a window that's closing." };
|
|
114
|
+
return { ok: true, tab: true };
|
|
115
|
+
},
|
|
116
|
+
// do script types into the tab, ending with a Return.
|
|
117
|
+
async type(ctx, text) {
|
|
118
|
+
const result = await osascript(
|
|
119
|
+
`${FIND_TERMINAL_TAB}
|
|
120
|
+
on run argv
|
|
121
|
+
set loc to my findTab(item 1 of argv)
|
|
122
|
+
if loc is missing value then return "not_found"
|
|
123
|
+
set {wid, i} to loc
|
|
124
|
+
tell application id "com.apple.Terminal" to do script (item 2 of argv) in tab i of window id wid
|
|
125
|
+
return "ok"
|
|
126
|
+
end run`,
|
|
127
|
+
[ctx.tty, asPaste(text)],
|
|
128
|
+
);
|
|
129
|
+
if (!result.ok) return result.error;
|
|
130
|
+
return result.value === 'not_found' ? 'its tab was gone' : null;
|
|
131
|
+
},
|
|
132
|
+
// A program's title shows in the window's title bar, first, for the
|
|
133
|
+
// selected tab; a custom title, for any tab.
|
|
134
|
+
async titles(ctxs) {
|
|
135
|
+
const result = await osascript(
|
|
136
|
+
`
|
|
137
|
+
on run argv
|
|
138
|
+
set out to ""
|
|
139
|
+
set sep to character id 9
|
|
140
|
+
set nl to character id 10
|
|
141
|
+
tell application id "com.apple.Terminal"
|
|
142
|
+
if not running then return ""
|
|
143
|
+
set wids to id of every window
|
|
144
|
+
repeat with k from 1 to count of wids
|
|
145
|
+
set wid to item k of wids
|
|
146
|
+
try
|
|
147
|
+
set wname to name of window id wid
|
|
148
|
+
set ttys to tty of every tab of window id wid
|
|
149
|
+
repeat with i from 1 to count of ttys
|
|
150
|
+
if argv contains (item i of ttys) then
|
|
151
|
+
set t to tab i of window id wid
|
|
152
|
+
set out to out & (item i of ttys) & sep & (custom title of t) & sep & (selected of t as text) & sep & wname & nl
|
|
153
|
+
end if
|
|
154
|
+
end repeat
|
|
155
|
+
end try
|
|
156
|
+
end repeat
|
|
157
|
+
end tell
|
|
158
|
+
return out
|
|
159
|
+
end run`,
|
|
160
|
+
ctxs.map((c) => c.tty),
|
|
161
|
+
{ timeout: 10000 },
|
|
162
|
+
);
|
|
163
|
+
const map = new Map();
|
|
164
|
+
if (!result.ok) return map;
|
|
165
|
+
for (const line of result.value.split('\n')) {
|
|
166
|
+
const [tty, custom, selected, windowName] = line.split('\t');
|
|
167
|
+
const title = custom || (selected === 'true' ? String(windowName || '').split(' — ')[0] : '');
|
|
168
|
+
if (tty && title) map.set(tty, title);
|
|
169
|
+
}
|
|
170
|
+
return map;
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
// iTerm2. Each tab holds sessions (panes); the lookup fetches each window's
|
|
175
|
+
// session devices in one call, as nested lists. Closed windows stay listed,
|
|
176
|
+
// hidden, so only shown or minimized windows count.
|
|
177
|
+
const FIND_ITERM_SESSION = `
|
|
178
|
+
on findSession(want)
|
|
179
|
+
tell application id "com.googlecode.iterm2"
|
|
180
|
+
set wids to id of every window
|
|
181
|
+
repeat with k from 1 to count of wids
|
|
182
|
+
set wid to item k of wids
|
|
183
|
+
try
|
|
184
|
+
if (visible of window id wid) or (miniaturized of window id wid) then
|
|
185
|
+
set ttys to tty of every session of every tab of window id wid
|
|
186
|
+
repeat with i from 1 to count of ttys
|
|
187
|
+
set tabTtys to item i of ttys
|
|
188
|
+
repeat with j from 1 to count of tabTtys
|
|
189
|
+
if item j of tabTtys is want then return {wid, i, j}
|
|
190
|
+
end repeat
|
|
191
|
+
end repeat
|
|
192
|
+
end if
|
|
193
|
+
end try
|
|
194
|
+
end repeat
|
|
195
|
+
end tell
|
|
196
|
+
return missing value
|
|
197
|
+
end findSession`;
|
|
198
|
+
|
|
199
|
+
const iterm = {
|
|
200
|
+
id: 'iterm2',
|
|
201
|
+
name: 'iTerm2',
|
|
202
|
+
processNames: ['iTerm2', 'iTermServer*'],
|
|
203
|
+
available: () => Boolean(appPath('iTerm.app')),
|
|
204
|
+
// Sessions run under iTerm2's server process, which outlives the app.
|
|
205
|
+
hosts: (ctx) => ctx.chain.some((p) => p.name === 'iTerm2' || p.name.startsWith('iTermServer')),
|
|
206
|
+
async open(dir, command) {
|
|
207
|
+
const result = await osascript(
|
|
208
|
+
`
|
|
209
|
+
on run argv
|
|
210
|
+
tell application id "com.googlecode.iterm2"
|
|
211
|
+
set w to missing value
|
|
212
|
+
try
|
|
213
|
+
set w to current window
|
|
214
|
+
end try
|
|
215
|
+
if w is missing value then
|
|
216
|
+
set s to current session of (create window with default profile)
|
|
217
|
+
else
|
|
218
|
+
tell w to set t to (create tab with default profile)
|
|
219
|
+
set s to current session of t
|
|
220
|
+
end if
|
|
221
|
+
tell s to write text (item 1 of argv)
|
|
222
|
+
activate
|
|
223
|
+
end tell
|
|
224
|
+
end run`,
|
|
225
|
+
[typedLine(dir, command)],
|
|
226
|
+
);
|
|
227
|
+
return result.ok ? { ok: true } : result;
|
|
228
|
+
},
|
|
229
|
+
async focus(ctx) {
|
|
230
|
+
const result = await osascript(
|
|
231
|
+
`${FIND_ITERM_SESSION}
|
|
232
|
+
on run argv
|
|
233
|
+
set loc to my findSession(item 1 of argv)
|
|
234
|
+
if loc is missing value then return "not_found"
|
|
235
|
+
set {wid, i, j} to loc
|
|
236
|
+
tell application id "com.googlecode.iterm2"
|
|
237
|
+
try
|
|
238
|
+
set miniaturized of window id wid to false
|
|
239
|
+
end try
|
|
240
|
+
select tab i of window id wid
|
|
241
|
+
select session j of tab i of window id wid
|
|
242
|
+
select window id wid
|
|
243
|
+
activate
|
|
244
|
+
end tell
|
|
245
|
+
return "ok"
|
|
246
|
+
end run`,
|
|
247
|
+
[ctx.tty],
|
|
248
|
+
);
|
|
249
|
+
if (!result.ok) return result;
|
|
250
|
+
if (result.value === 'not_found') return { ok: false, error: 'iTerm2 has no session for this one.' };
|
|
251
|
+
return { ok: true, tab: true };
|
|
252
|
+
},
|
|
253
|
+
// write text sends the text and then a Return.
|
|
254
|
+
async type(ctx, text) {
|
|
255
|
+
const result = await osascript(
|
|
256
|
+
`${FIND_ITERM_SESSION}
|
|
257
|
+
on run argv
|
|
258
|
+
set loc to my findSession(item 1 of argv)
|
|
259
|
+
if loc is missing value then return "not_found"
|
|
260
|
+
set {wid, i, j} to loc
|
|
261
|
+
tell application id "com.googlecode.iterm2"
|
|
262
|
+
tell session j of tab i of window id wid to write text (item 2 of argv)
|
|
263
|
+
end tell
|
|
264
|
+
return "ok"
|
|
265
|
+
end run`,
|
|
266
|
+
[ctx.tty, asPaste(text)],
|
|
267
|
+
);
|
|
268
|
+
if (!result.ok) return result.error;
|
|
269
|
+
return result.value === 'not_found' ? 'its session was gone' : null;
|
|
270
|
+
},
|
|
271
|
+
// The title a program set, rather than the tab bar's formatted name.
|
|
272
|
+
async titles(ctxs) {
|
|
273
|
+
const result = await osascript(
|
|
274
|
+
`
|
|
275
|
+
on run argv
|
|
276
|
+
set out to ""
|
|
277
|
+
set sep to character id 9
|
|
278
|
+
set nl to character id 10
|
|
279
|
+
tell application id "com.googlecode.iterm2"
|
|
280
|
+
if not running then return ""
|
|
281
|
+
repeat with w in (every window)
|
|
282
|
+
repeat with t in (tabs of w)
|
|
283
|
+
repeat with s in (sessions of t)
|
|
284
|
+
if argv contains (tty of s) then
|
|
285
|
+
set shown to ""
|
|
286
|
+
try
|
|
287
|
+
tell s to set shown to (variable named "session.terminalWindowName")
|
|
288
|
+
end try
|
|
289
|
+
if shown is "" or shown is missing value then set shown to name of s
|
|
290
|
+
set out to out & (tty of s) & sep & shown & nl
|
|
291
|
+
end if
|
|
292
|
+
end repeat
|
|
293
|
+
end repeat
|
|
294
|
+
end repeat
|
|
295
|
+
end tell
|
|
296
|
+
return out
|
|
297
|
+
end run`,
|
|
298
|
+
ctxs.map((c) => c.tty),
|
|
299
|
+
{ timeout: 10000 },
|
|
300
|
+
);
|
|
301
|
+
const map = new Map();
|
|
302
|
+
if (!result.ok) return map;
|
|
303
|
+
for (const line of result.value.split('\n')) {
|
|
304
|
+
const [tty, title] = line.split('\t');
|
|
305
|
+
if (tty && title) map.set(tty, title);
|
|
306
|
+
}
|
|
307
|
+
return map;
|
|
308
|
+
},
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
// Ghostty, through the AppleScript support it has had since 1.3; finding a
|
|
312
|
+
// session's terminal by its device needs 1.4.
|
|
313
|
+
const FIND_GHOSTTY_TERMINAL = `
|
|
314
|
+
on findTerminal(want)
|
|
315
|
+
tell application id "com.mitchellh.ghostty"
|
|
316
|
+
repeat with w in windows
|
|
317
|
+
repeat with tb in tabs of w
|
|
318
|
+
repeat with t in terminals of tb
|
|
319
|
+
if (tty of t) is want then return t
|
|
320
|
+
end repeat
|
|
321
|
+
end repeat
|
|
322
|
+
end repeat
|
|
323
|
+
end tell
|
|
324
|
+
return missing value
|
|
325
|
+
end findTerminal`;
|
|
326
|
+
|
|
327
|
+
const OLD_GHOSTTY = 'Going to a Ghostty tab needs Ghostty 1.4 or later.';
|
|
328
|
+
|
|
329
|
+
const ghostty = {
|
|
330
|
+
id: 'ghostty',
|
|
331
|
+
name: 'Ghostty',
|
|
332
|
+
processNames: ['ghostty'],
|
|
333
|
+
available: () => Boolean(appPath('Ghostty.app')),
|
|
334
|
+
hosts: (ctx) => ctx.chain.some((p) => p.name === 'ghostty'),
|
|
335
|
+
// The line is typed into the tab's own shell, which stays open afterwards.
|
|
336
|
+
async open(dir, command) {
|
|
337
|
+
const result = await osascript(
|
|
338
|
+
`
|
|
339
|
+
on run argv
|
|
340
|
+
set nl to character id 10
|
|
341
|
+
tell application id "com.mitchellh.ghostty"
|
|
342
|
+
set cfg to new surface configuration
|
|
343
|
+
set initial working directory of cfg to item 1 of argv
|
|
344
|
+
set initial input of cfg to (item 2 of argv) & nl
|
|
345
|
+
if (count of windows) > 0 then
|
|
346
|
+
new tab in front window with configuration cfg
|
|
347
|
+
else
|
|
348
|
+
new window with configuration cfg
|
|
349
|
+
end if
|
|
350
|
+
activate
|
|
351
|
+
end tell
|
|
352
|
+
end run`,
|
|
353
|
+
[dir, typedLine(dir, command)],
|
|
354
|
+
);
|
|
355
|
+
return result.ok ? { ok: true } : result;
|
|
356
|
+
},
|
|
357
|
+
async focus(ctx) {
|
|
358
|
+
const result = await osascript(
|
|
359
|
+
`${FIND_GHOSTTY_TERMINAL}
|
|
360
|
+
on run argv
|
|
361
|
+
set t to my findTerminal(item 1 of argv)
|
|
362
|
+
if t is missing value then return "not_found"
|
|
363
|
+
tell application id "com.mitchellh.ghostty"
|
|
364
|
+
focus t
|
|
365
|
+
activate
|
|
366
|
+
end tell
|
|
367
|
+
return "ok"
|
|
368
|
+
end run`,
|
|
369
|
+
[ctx.tty],
|
|
370
|
+
);
|
|
371
|
+
if (!result.ok) return /tty/.test(result.error) ? { ok: false, error: OLD_GHOSTTY } : result;
|
|
372
|
+
if (result.value === 'not_found') return { ok: false, error: 'Ghostty has no terminal for this session.' };
|
|
373
|
+
return { ok: true, tab: true };
|
|
374
|
+
},
|
|
375
|
+
// `input text` arrives as a paste; Enter follows as its own key.
|
|
376
|
+
async type(ctx, text) {
|
|
377
|
+
const result = await osascript(
|
|
378
|
+
`${FIND_GHOSTTY_TERMINAL}
|
|
379
|
+
on run argv
|
|
380
|
+
set t to my findTerminal(item 1 of argv)
|
|
381
|
+
if t is missing value then return "not_found"
|
|
382
|
+
tell application id "com.mitchellh.ghostty"
|
|
383
|
+
input text (item 2 of argv) to t
|
|
384
|
+
delay 0.3
|
|
385
|
+
send key "enter" to t
|
|
386
|
+
end tell
|
|
387
|
+
return "ok"
|
|
388
|
+
end run`,
|
|
389
|
+
[ctx.tty, text],
|
|
390
|
+
);
|
|
391
|
+
if (!result.ok) return /tty/.test(result.error) ? OLD_GHOSTTY : result.error;
|
|
392
|
+
return result.value === 'not_found' ? 'its terminal was gone' : null;
|
|
393
|
+
},
|
|
394
|
+
async titles(ctxs) {
|
|
395
|
+
const result = await osascript(
|
|
396
|
+
`
|
|
397
|
+
on run argv
|
|
398
|
+
set out to ""
|
|
399
|
+
set sep to character id 9
|
|
400
|
+
set nl to character id 10
|
|
401
|
+
tell application id "com.mitchellh.ghostty"
|
|
402
|
+
if not running then return ""
|
|
403
|
+
repeat with w in windows
|
|
404
|
+
repeat with tb in tabs of w
|
|
405
|
+
repeat with t in terminals of tb
|
|
406
|
+
if argv contains (tty of t) then set out to out & (tty of t) & sep & (name of t) & nl
|
|
407
|
+
end repeat
|
|
408
|
+
end repeat
|
|
409
|
+
end repeat
|
|
410
|
+
end tell
|
|
411
|
+
return out
|
|
412
|
+
end run`,
|
|
413
|
+
ctxs.map((c) => c.tty),
|
|
414
|
+
{ timeout: 10000 },
|
|
415
|
+
);
|
|
416
|
+
const map = new Map();
|
|
417
|
+
if (!result.ok) return map;
|
|
418
|
+
for (const line of result.value.split('\n')) {
|
|
419
|
+
const [tty, title] = line.split('\t');
|
|
420
|
+
if (tty && title) map.set(tty, title);
|
|
421
|
+
}
|
|
422
|
+
return map;
|
|
423
|
+
},
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
// kitty and WezTerm switch tab or pane themselves; macOS brings the app forward.
|
|
427
|
+
const activate = (app) => async () => (await unix.run('open', ['-a', app])).ok;
|
|
428
|
+
|
|
429
|
+
const backends = [
|
|
430
|
+
terminalApp,
|
|
431
|
+
iterm,
|
|
432
|
+
ghostty,
|
|
433
|
+
kitty.backend({ raise: activate('kitty') }),
|
|
434
|
+
wezterm.backend({ raise: activate('WezTerm') }),
|
|
435
|
+
{
|
|
436
|
+
id: 'tmux',
|
|
437
|
+
name: 'tmux',
|
|
438
|
+
available: () => tmux.available(),
|
|
439
|
+
hosts: () => false,
|
|
440
|
+
open: tmux.open,
|
|
441
|
+
},
|
|
442
|
+
];
|
|
443
|
+
|
|
444
|
+
// The terminal Automatic picks: one that's running, else one installed,
|
|
445
|
+
// iTerm2 and the others before Terminal, which every Mac has.
|
|
446
|
+
async function defaultTerminal(list) {
|
|
447
|
+
const order = ['iterm2', 'ghostty', 'wezterm', 'kitty', 'terminal'];
|
|
448
|
+
const table = await unix.processTable();
|
|
449
|
+
const running = new Set([...table.values()].map((p) => p.name));
|
|
450
|
+
const usable = order.map((id) => list.find((b) => b.id === id && b.open && b.available())).filter(Boolean);
|
|
451
|
+
return (usable.find((b) => b.processNames?.some((n) => running.has(n))) || usable[0])?.id || null;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// The standard folder chooser, in front. Cancelling is error -128.
|
|
455
|
+
async function pickFolder(start, title) {
|
|
456
|
+
const lines = ['on run argv', 'activate'];
|
|
457
|
+
lines.push(start ? 'set f to choose folder with prompt (item 1 of argv) default location (POSIX file (item 2 of argv))' : 'set f to choose folder with prompt (item 1 of argv)');
|
|
458
|
+
lines.push('return POSIX path of f', 'end run');
|
|
459
|
+
const args = lines.flatMap((l) => ['-e', l]).concat([title, ...(start ? [start] : [])]);
|
|
460
|
+
const out = await unix.run('osascript', args, { timeout: 30 * 60 * 1000 });
|
|
461
|
+
if (/-128/.test(out.stderr)) return { ok: true, cancelled: true };
|
|
462
|
+
const picked = out.stdout.trim().replace(/\/$/, '');
|
|
463
|
+
return out.ok && picked ? { ok: true, path: picked } : { ok: false, error: out.stderr.trim() || "The folder picker didn't open." };
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
const openFolder = (dir) => unix.detached('open', [dir]);
|
|
467
|
+
|
|
468
|
+
module.exports = create({ name: 'macos', backends, openFolder, pickFolder, defaultTerminal });
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// tmux, on macOS and Linux. A session inside tmux is found by its terminal
|
|
4
|
+
// device: each pane has its own. tmux can select the pane, type into it and
|
|
5
|
+
// read its title, whatever terminal it's shown in; that terminal's window is
|
|
6
|
+
// then brought forward by the driver for it, through the tmux client's device.
|
|
7
|
+
|
|
8
|
+
const { run, which, shellCommand, pause } = require('./unix');
|
|
9
|
+
|
|
10
|
+
// Printable: tmux shows control characters in its output as escapes.
|
|
11
|
+
const SEP = ' <|> ';
|
|
12
|
+
const FIELDS = ['pane_tty', 'pane_id', 'session_name', 'window_id', 'pane_title', 'window_name', 'session_attached', 'window_activity'];
|
|
13
|
+
|
|
14
|
+
const available = () => Boolean(which('tmux'));
|
|
15
|
+
|
|
16
|
+
async function panes() {
|
|
17
|
+
if (!available()) return [];
|
|
18
|
+
const out = await run('tmux', ['list-panes', '-a', '-F', FIELDS.map((f) => `#{${f}}`).join(SEP)]);
|
|
19
|
+
if (!out.ok) return [];
|
|
20
|
+
return out.stdout
|
|
21
|
+
.split('\n')
|
|
22
|
+
.filter(Boolean)
|
|
23
|
+
.map((line) => Object.fromEntries(line.split(SEP).map((v, i) => [FIELDS[i], v])));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// The terminals attached to tmux, or to one of its sessions, latest first.
|
|
27
|
+
async function clients(session) {
|
|
28
|
+
const target = session ? ['-t', session] : [];
|
|
29
|
+
const out = await run('tmux', ['list-clients', ...target, '-F', `#{client_tty}${SEP}#{client_pid}${SEP}#{client_activity}`]);
|
|
30
|
+
if (!out.ok) return [];
|
|
31
|
+
return out.stdout
|
|
32
|
+
.split('\n')
|
|
33
|
+
.filter(Boolean)
|
|
34
|
+
.map((line) => {
|
|
35
|
+
const [tty, pid, activity] = line.split(SEP);
|
|
36
|
+
return { tty, pid: Number(pid), activity: Number(activity) };
|
|
37
|
+
})
|
|
38
|
+
.sort((a, b) => b.activity - a.activity);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const findPane = async (tty) => (tty ? (await panes()).find((p) => p.pane_tty === tty) || null : null);
|
|
42
|
+
|
|
43
|
+
// Selects the pane's window and the pane. When no terminal shows its tmux
|
|
44
|
+
// session, the terminal used last switches to it. Resolves to that terminal's
|
|
45
|
+
// client, whose window is what to bring forward, or null when tmux has none.
|
|
46
|
+
async function select(pane) {
|
|
47
|
+
await run('tmux', ['select-window', '-t', pane.window_id]);
|
|
48
|
+
await run('tmux', ['select-pane', '-t', pane.pane_id]);
|
|
49
|
+
const showing = (await clients(pane.session_name))[0];
|
|
50
|
+
if (showing) return showing;
|
|
51
|
+
const latest = (await clients())[0];
|
|
52
|
+
if (!latest) return null;
|
|
53
|
+
await run('tmux', ['switch-client', '-c', latest.tty, '-t', pane.pane_id]);
|
|
54
|
+
return latest;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Pastes the text, marked as a paste where the program asked for that, as
|
|
58
|
+
// Claude Code does; then presses Enter. Typed unmarked, Claude Code would take
|
|
59
|
+
// an Enter that follows soon after as part of the paste.
|
|
60
|
+
async function type(pane, text) {
|
|
61
|
+
const buffer = `tabherd-${process.pid}`;
|
|
62
|
+
const set = await run('tmux', ['set-buffer', '-b', buffer, '--', text]);
|
|
63
|
+
if (!set.ok) return `tmux couldn't hold the text: ${set.stderr.trim()}`;
|
|
64
|
+
const pasted = await run('tmux', ['paste-buffer', '-p', '-d', '-b', buffer, '-t', pane.pane_id]);
|
|
65
|
+
if (!pasted.ok) return `tmux couldn't type into it: ${pasted.stderr.trim()}`;
|
|
66
|
+
await pause(300);
|
|
67
|
+
const entered = await run('tmux', ['send-keys', '-t', pane.pane_id, 'Enter']);
|
|
68
|
+
if (!entered.ok) return `tmux couldn't press Enter: ${entered.stderr.trim()}`;
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// A new window in the tmux session used last.
|
|
73
|
+
async function open(dir, command) {
|
|
74
|
+
if (!available()) return { ok: false, error: "tmux isn't installed." };
|
|
75
|
+
const list = await panes();
|
|
76
|
+
if (!list.length) return { ok: false, error: "tmux isn't running. Start tmux in a terminal first, or choose another terminal in Settings." };
|
|
77
|
+
const latest = list.sort((a, b) => Number(b.window_activity) - Number(a.window_activity))[0];
|
|
78
|
+
const out = await run('tmux', ['new-window', '-t', `${latest.session_name}:`, '-c', dir, shellCommand(dir, command)]);
|
|
79
|
+
return out.ok ? { ok: true } : { ok: false, error: `tmux couldn't open a window: ${out.stderr.trim()}` };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Pane titles by terminal device. Claude Code sets its pane's title.
|
|
83
|
+
async function titles() {
|
|
84
|
+
const map = new Map();
|
|
85
|
+
for (const p of await panes()) map.set(p.pane_tty, p.pane_title);
|
|
86
|
+
return map;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
module.exports = { available, panes, findPane, select, type, open, titles };
|