mux-sesh 1.8.0 → 1.9.1
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/README.md +2 -0
- package/package.json +6 -2
- package/src/app/data.ts +2 -3
- package/src/app/effects.ts +72 -1
- package/src/app/modal-keyboard.ts +15 -6
- package/src/app/view.ts +3 -2
- package/src/config/store.ts +11 -4
- package/src/files/target.ts +96 -0
- package/src/handlers/action-helpers.ts +14 -4
- package/src/handlers/actions.ts +31 -4
- package/src/handlers/keyboard/new-session.ts +3 -2
- package/src/handlers/keyboard/normal.ts +9 -3
- package/src/handlers/keyboard/opencode.ts +3 -2
- package/src/handlers/keyboard/search.ts +4 -3
- package/src/items/order.ts +3 -6
- package/src/opencode/index.ts +1 -0
- package/src/opencode/parser.ts +16 -1
- package/src/opencode/session-name.ts +11 -0
- package/src/opencode/stats.ts +2 -1
- package/src/preview/project.ts +90 -13
- package/src/projects/session-links.ts +5 -2
- package/src/search/fff.ts +224 -0
- package/src/search/index.ts +7 -0
- package/src/styles/theme.tsx +1 -1
- package/src/tmux/actions.ts +71 -29
- package/src/tmux/details.ts +30 -49
- package/src/tmux/project-discovery.ts +10 -7
- package/src/tmux/project-loader.ts +19 -1
- package/src/tmux/project-zoxide.ts +1 -1
- package/src/types/index.ts +4 -20
- package/src/ui/ItemList.tsx +5 -1
- package/src/ui/SessionDetailsPanel.tsx +7 -6
- package/src/ui/item-icon.ts +15 -2
- package/src/ui/list-window.ts +10 -1
- package/src/util/github.ts +65 -20
- package/src/util/path-display.ts +1 -1
- package/src/util/shell.ts +3 -0
- package/src/util/shutdown.ts +3 -3
package/src/preview/project.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import { readdir } from 'fs/promises'
|
|
2
|
-
import { basename, join } from 'path'
|
|
1
|
+
import { readdir, stat } from 'fs/promises'
|
|
2
|
+
import { basename, dirname, join } from 'path'
|
|
3
3
|
import { spawn } from 'bun'
|
|
4
4
|
import { getSessionDetails } from '../tmux'
|
|
5
5
|
import type { Config, SessionDetails } from '../types'
|
|
6
6
|
import { stripAnsi } from '../util/ansi'
|
|
7
7
|
import { getGitRoot, resolveProjectSession } from '../config/session-rules'
|
|
8
|
+
import { resolveFileSession } from '../files/target'
|
|
9
|
+
import { toHomeRelativePath } from '../util/path-display'
|
|
10
|
+
import { quoteShellArg } from '../util/shell'
|
|
8
11
|
|
|
9
12
|
const MAX_PREVIEW_LINES = 24
|
|
10
13
|
const MAX_PREVIEW_LINE_LENGTH = 120
|
|
@@ -15,13 +18,13 @@ const projectPreviewCache = new Map<string, { preview: ProjectPreview; expiresAt
|
|
|
15
18
|
export interface ProjectPreview {
|
|
16
19
|
path: string
|
|
17
20
|
sessionName: string
|
|
18
|
-
source: 'project' | 'wildcard' | 'default'
|
|
21
|
+
source: 'project' | 'wildcard' | 'default' | 'file'
|
|
19
22
|
startupCommand?: string
|
|
20
23
|
previewCommand?: string
|
|
21
24
|
gitRoot?: string
|
|
22
25
|
gitBranch?: string
|
|
23
26
|
linkedSession?: SessionDetails
|
|
24
|
-
previewKind: 'command' | 'directory'
|
|
27
|
+
previewKind: 'command' | 'directory' | 'file'
|
|
25
28
|
previewLabel: string
|
|
26
29
|
previewLines: string[]
|
|
27
30
|
previewNotice?: string
|
|
@@ -37,10 +40,6 @@ interface PackageSummary {
|
|
|
37
40
|
scripts: string[]
|
|
38
41
|
}
|
|
39
42
|
|
|
40
|
-
function displayPath(path: string): string {
|
|
41
|
-
return path.replace(process.env.HOME || '', '~')
|
|
42
|
-
}
|
|
43
|
-
|
|
44
43
|
export function interpolatePreviewCommand(
|
|
45
44
|
command: string | undefined,
|
|
46
45
|
projectPath: string
|
|
@@ -49,7 +48,7 @@ export function interpolatePreviewCommand(
|
|
|
49
48
|
return undefined
|
|
50
49
|
}
|
|
51
50
|
|
|
52
|
-
return command.replaceAll('{}', projectPath)
|
|
51
|
+
return command.replaceAll('{}', quoteShellArg(projectPath))
|
|
53
52
|
}
|
|
54
53
|
|
|
55
54
|
export function formatPreviewOutput(
|
|
@@ -228,12 +227,12 @@ export async function getProjectPreview(
|
|
|
228
227
|
|
|
229
228
|
if (formattedCommandOutput && formattedCommandOutput.lines.length > 0) {
|
|
230
229
|
const preview: ProjectPreview = {
|
|
231
|
-
path:
|
|
230
|
+
path: toHomeRelativePath(projectPath),
|
|
232
231
|
sessionName: resolvedSession.sessionName,
|
|
233
232
|
source: resolvedSession.source,
|
|
234
233
|
startupCommand: resolvedSession.startupCommand,
|
|
235
234
|
previewCommand: resolvedPreviewCommand,
|
|
236
|
-
gitRoot: gitRootPath ?
|
|
235
|
+
gitRoot: gitRootPath ? toHomeRelativePath(gitRootPath) : undefined,
|
|
237
236
|
gitBranch,
|
|
238
237
|
linkedSession: linkedSession ?? undefined,
|
|
239
238
|
previewKind: 'command',
|
|
@@ -256,12 +255,12 @@ export async function getProjectPreview(
|
|
|
256
255
|
const formattedDirectoryOutput = formatPreviewOutput(fallbackLines.join('\n'))
|
|
257
256
|
|
|
258
257
|
const preview: ProjectPreview = {
|
|
259
|
-
path:
|
|
258
|
+
path: toHomeRelativePath(projectPath),
|
|
260
259
|
sessionName: resolvedSession.sessionName,
|
|
261
260
|
source: resolvedSession.source,
|
|
262
261
|
startupCommand: resolvedSession.startupCommand,
|
|
263
262
|
previewCommand: resolvedPreviewCommand,
|
|
264
|
-
gitRoot: gitRootPath ?
|
|
263
|
+
gitRoot: gitRootPath ? toHomeRelativePath(gitRootPath) : undefined,
|
|
265
264
|
gitBranch,
|
|
266
265
|
linkedSession: linkedSession ?? undefined,
|
|
267
266
|
previewKind: 'directory',
|
|
@@ -281,3 +280,81 @@ export async function getProjectPreview(
|
|
|
281
280
|
|
|
282
281
|
return preview
|
|
283
282
|
}
|
|
283
|
+
|
|
284
|
+
const FILE_PREVIEW_MAX_BYTES = 16 * 1024
|
|
285
|
+
|
|
286
|
+
async function getFilePreviewLines(
|
|
287
|
+
filePath: string
|
|
288
|
+
): Promise<{ lines: string[]; notice?: string }> {
|
|
289
|
+
try {
|
|
290
|
+
const fileInfo = await stat(filePath)
|
|
291
|
+
const bytes = new Uint8Array(
|
|
292
|
+
await Bun.file(filePath).slice(0, FILE_PREVIEW_MAX_BYTES).arrayBuffer()
|
|
293
|
+
)
|
|
294
|
+
|
|
295
|
+
if (bytes.includes(0)) {
|
|
296
|
+
return {
|
|
297
|
+
lines: [`${basename(filePath)} (${fileInfo.size} bytes)`],
|
|
298
|
+
notice: 'Binary file; preview skipped.',
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const formatted = formatPreviewOutput(new TextDecoder().decode(bytes))
|
|
303
|
+
return {
|
|
304
|
+
lines: formatted.lines.length > 0 ? formatted.lines : ['(empty file)'],
|
|
305
|
+
notice:
|
|
306
|
+
formatted.truncated || fileInfo.size > FILE_PREVIEW_MAX_BYTES
|
|
307
|
+
? 'File preview truncated to fit the panel.'
|
|
308
|
+
: undefined,
|
|
309
|
+
}
|
|
310
|
+
} catch {
|
|
311
|
+
return {
|
|
312
|
+
lines: [basename(filePath)],
|
|
313
|
+
notice: 'Unable to read file contents.',
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export async function getFilePreview(
|
|
319
|
+
filePath: string,
|
|
320
|
+
config: Config,
|
|
321
|
+
linkedSessionName?: string
|
|
322
|
+
): Promise<ProjectPreview> {
|
|
323
|
+
const cacheKey = `file::${filePath}::${linkedSessionName ?? ''}`
|
|
324
|
+
const cached = projectPreviewCache.get(cacheKey)
|
|
325
|
+
if (cached && cached.expiresAt > Date.now()) {
|
|
326
|
+
return cached.preview
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const parentDirectory = dirname(filePath)
|
|
330
|
+
const resolvedSession = resolveFileSession(filePath, config)
|
|
331
|
+
const [gitRootPath, gitBranch, linkedSession, fileLines] = await Promise.all([
|
|
332
|
+
getGitRoot(parentDirectory),
|
|
333
|
+
getGitBranch(parentDirectory),
|
|
334
|
+
linkedSessionName
|
|
335
|
+
? getSessionDetails(linkedSessionName).catch(() => null)
|
|
336
|
+
: Promise.resolve(null),
|
|
337
|
+
getFilePreviewLines(filePath),
|
|
338
|
+
])
|
|
339
|
+
|
|
340
|
+
const preview: ProjectPreview = {
|
|
341
|
+
path: toHomeRelativePath(filePath),
|
|
342
|
+
sessionName: resolvedSession.sessionName,
|
|
343
|
+
source: 'file',
|
|
344
|
+
startupCommand: resolvedSession.startupCommand,
|
|
345
|
+
gitRoot: gitRootPath ? toHomeRelativePath(gitRootPath) : undefined,
|
|
346
|
+
gitBranch,
|
|
347
|
+
linkedSession: linkedSession ?? undefined,
|
|
348
|
+
previewKind: 'file',
|
|
349
|
+
previewLabel: 'File Preview',
|
|
350
|
+
previewLines: fileLines.lines,
|
|
351
|
+
previewNotice: fileLines.notice,
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
projectPreviewCache.set(cacheKey, {
|
|
355
|
+
preview,
|
|
356
|
+
expiresAt: Date.now() + PROJECT_PREVIEW_CACHE_TTL_MS,
|
|
357
|
+
})
|
|
358
|
+
|
|
359
|
+
return preview
|
|
360
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Config, Item } from '../types'
|
|
2
2
|
import { resolveProjectSession } from '../config/session-rules'
|
|
3
|
+
import { buildFileSessionName, isFileItem } from '../files/target'
|
|
3
4
|
|
|
4
5
|
export async function annotateProjectItemsWithSessionLinks(
|
|
5
6
|
projectItems: Item[],
|
|
@@ -10,8 +11,10 @@ export async function annotateProjectItemsWithSessionLinks(
|
|
|
10
11
|
|
|
11
12
|
return Promise.all(
|
|
12
13
|
projectItems.map(async projectItem => {
|
|
13
|
-
const
|
|
14
|
-
|
|
14
|
+
const sessionName = isFileItem(projectItem)
|
|
15
|
+
? buildFileSessionName(projectItem.path)
|
|
16
|
+
: (await resolveProjectSession(projectItem.path, config)).sessionName
|
|
17
|
+
const linkedSession = liveSessionsByTitle.get(sessionName)
|
|
15
18
|
|
|
16
19
|
if (!linkedSession) {
|
|
17
20
|
return {
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { basename, join } from 'path'
|
|
2
|
+
import { homedir } from 'os'
|
|
3
|
+
import type { FileFinder, MixedSearchResult } from '@ff-labs/fff-bun'
|
|
4
|
+
import { scoreSearchItems } from './index'
|
|
5
|
+
import type { Config, Item } from '../types'
|
|
6
|
+
|
|
7
|
+
declare global {
|
|
8
|
+
// Build-time constant used by @ff-labs/fff-bun for compiled binaries.
|
|
9
|
+
const FFF_LIBC: 'gnu' | 'musl'
|
|
10
|
+
}
|
|
11
|
+
import { getExistingProjectRoots, uniqueNormalizedPaths } from '../tmux/project-discovery'
|
|
12
|
+
import { getPathContextLabel } from '../util/path-display'
|
|
13
|
+
|
|
14
|
+
const DEFAULT_RESULT_LIMIT = 60
|
|
15
|
+
const SCAN_WAIT_TIMEOUT_MS = 1500
|
|
16
|
+
|
|
17
|
+
interface ScoredItem {
|
|
18
|
+
item: Item
|
|
19
|
+
score: number
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type FffModule = typeof import('@ff-labs/fff-bun')
|
|
23
|
+
|
|
24
|
+
const state: {
|
|
25
|
+
module?: FffModule | null
|
|
26
|
+
finders: Map<string, Promise<FileFinder | null>>
|
|
27
|
+
} = {
|
|
28
|
+
finders: new Map(),
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function isFileSearchDisabled(): boolean {
|
|
32
|
+
const flag = process.env.MUX_SESH_DISABLE_FFF?.toLowerCase()
|
|
33
|
+
return flag === '1' || flag === 'true'
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function loadFffModule(): Promise<FffModule | null> {
|
|
37
|
+
if (state.module !== undefined) {
|
|
38
|
+
return state.module
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (isFileSearchDisabled()) {
|
|
42
|
+
state.module = null
|
|
43
|
+
return null
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
const loaded = await import('@ff-labs/fff-bun')
|
|
48
|
+
state.module = loaded.FileFinder.isAvailable() ? loaded : null
|
|
49
|
+
} catch {
|
|
50
|
+
state.module = null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return state.module
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function getFinder(root: string): Promise<FileFinder | null> {
|
|
57
|
+
const cached = state.finders.get(root)
|
|
58
|
+
if (cached) {
|
|
59
|
+
return cached
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const created = (async () => {
|
|
63
|
+
const loaded = await loadFffModule()
|
|
64
|
+
if (!loaded) {
|
|
65
|
+
return null
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const made = loaded.FileFinder.create({
|
|
69
|
+
basePath: root,
|
|
70
|
+
disableMmapCache: true,
|
|
71
|
+
disableContentIndexing: true,
|
|
72
|
+
enableHomeDirScanning: root === homedir(),
|
|
73
|
+
})
|
|
74
|
+
if (!made.ok) {
|
|
75
|
+
return null
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
await made.value.waitForScan(SCAN_WAIT_TIMEOUT_MS)
|
|
79
|
+
return made.value
|
|
80
|
+
})()
|
|
81
|
+
|
|
82
|
+
state.finders.set(root, created)
|
|
83
|
+
return created
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function mapMixedResultToScoredItems(root: string, result: MixedSearchResult): ScoredItem[] {
|
|
87
|
+
return result.items.flatMap((mixed, index): ScoredItem[] => {
|
|
88
|
+
const score = result.scores[index]?.total ?? 0
|
|
89
|
+
|
|
90
|
+
if (mixed.type === 'file') {
|
|
91
|
+
const path = join(root, mixed.item.relativePath)
|
|
92
|
+
return [
|
|
93
|
+
{
|
|
94
|
+
score,
|
|
95
|
+
item: {
|
|
96
|
+
title: mixed.item.fileName,
|
|
97
|
+
desc: getPathContextLabel(path),
|
|
98
|
+
path,
|
|
99
|
+
isSession: false,
|
|
100
|
+
itemKind: 'file' as const,
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
]
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const relative = mixed.item.relativePath.replace(/[\\/]+$/, '')
|
|
107
|
+
if (!relative) {
|
|
108
|
+
return []
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const path = join(root, relative)
|
|
112
|
+
return [
|
|
113
|
+
{
|
|
114
|
+
score,
|
|
115
|
+
item: {
|
|
116
|
+
title: basename(path),
|
|
117
|
+
desc: getPathContextLabel(path),
|
|
118
|
+
path,
|
|
119
|
+
isSession: false,
|
|
120
|
+
itemKind: 'project' as const,
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
]
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function mergeScoredItems(results: ScoredItem[][], limit: number): Item[] {
|
|
128
|
+
const seenPaths = new Set<string>()
|
|
129
|
+
|
|
130
|
+
return results
|
|
131
|
+
.flat()
|
|
132
|
+
.sort(
|
|
133
|
+
(left, right) => right.score - left.score || left.item.path.length - right.item.path.length
|
|
134
|
+
)
|
|
135
|
+
.flatMap(scored => {
|
|
136
|
+
if (seenPaths.has(scored.item.path)) {
|
|
137
|
+
return []
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
seenPaths.add(scored.item.path)
|
|
141
|
+
return [scored.item]
|
|
142
|
+
})
|
|
143
|
+
.slice(0, limit)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function combineFileSearchResults(
|
|
147
|
+
fileSearchItems: Item[],
|
|
148
|
+
localItems: Item[],
|
|
149
|
+
query: string
|
|
150
|
+
): Item[] {
|
|
151
|
+
if (fileSearchItems.length === 0) {
|
|
152
|
+
return localItems
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const localPaths = new Set(localItems.map(item => item.path))
|
|
156
|
+
const newFileSearchItems = fileSearchItems.filter(item => !localPaths.has(item.path))
|
|
157
|
+
const candidates = [...localItems, ...newFileSearchItems]
|
|
158
|
+
|
|
159
|
+
const scored = scoreSearchItems(candidates, query).sort((left, right) => right.score - left.score)
|
|
160
|
+
const scoredPaths = new Set(scored.map(result => result.item.path))
|
|
161
|
+
|
|
162
|
+
// fff matched these (typo-resistant) even though the local matcher rejected
|
|
163
|
+
// them; keep them visible at the bottom in fff ranking order.
|
|
164
|
+
const typoOnlyItems = newFileSearchItems.filter(item => !scoredPaths.has(item.path))
|
|
165
|
+
|
|
166
|
+
return [...scored.map(result => result.item), ...typoOnlyItems]
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async function searchRoot(root: string, query: string, limit: number): Promise<ScoredItem[]> {
|
|
170
|
+
const finder = await getFinder(root)
|
|
171
|
+
if (!finder) {
|
|
172
|
+
return []
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const result = finder.mixedSearch(query, { pageSize: limit })
|
|
176
|
+
if (!result.ok) {
|
|
177
|
+
return []
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return mapMixedResultToScoredItems(root, result.value)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export async function isFileSearchAvailable(): Promise<boolean> {
|
|
184
|
+
return (await loadFffModule()) !== null
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export async function warmFileSearch(config: Config): Promise<void> {
|
|
188
|
+
if (!(await loadFffModule())) {
|
|
189
|
+
return
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const roots = await getExistingProjectRoots(uniqueNormalizedPaths(config.projectPaths))
|
|
193
|
+
await Promise.all(roots.map(root => getFinder(root)))
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export async function searchFilesAndDirectories(
|
|
197
|
+
query: string,
|
|
198
|
+
config: Config,
|
|
199
|
+
limit = DEFAULT_RESULT_LIMIT
|
|
200
|
+
): Promise<Item[]> {
|
|
201
|
+
const trimmed = query.trim()
|
|
202
|
+
if (!trimmed || !(await loadFffModule())) {
|
|
203
|
+
return []
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const roots = await getExistingProjectRoots(uniqueNormalizedPaths(config.projectPaths))
|
|
207
|
+
if (roots.length === 0) {
|
|
208
|
+
return []
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const results = await Promise.all(roots.map(root => searchRoot(root, trimmed, limit)))
|
|
212
|
+
return mergeScoredItems(results, limit)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export async function destroyFileSearch(): Promise<void> {
|
|
216
|
+
const pending = [...state.finders.values()]
|
|
217
|
+
state.finders.clear()
|
|
218
|
+
state.module = undefined
|
|
219
|
+
|
|
220
|
+
for (const finderPromise of pending) {
|
|
221
|
+
const finder = await finderPromise.catch(() => null)
|
|
222
|
+
finder?.destroy()
|
|
223
|
+
}
|
|
224
|
+
}
|
package/src/search/index.ts
CHANGED
|
@@ -176,6 +176,13 @@ export function clearMatchIndices(items: Item[]): Item[] {
|
|
|
176
176
|
return items.map(item => withSearchMatch(item))
|
|
177
177
|
}
|
|
178
178
|
|
|
179
|
+
export function scoreSearchItems(items: Item[], query: string): SearchResult[] {
|
|
180
|
+
return items.flatMap(item => {
|
|
181
|
+
const result = buildSearchResult(item, query)
|
|
182
|
+
return result ? [result] : []
|
|
183
|
+
})
|
|
184
|
+
}
|
|
185
|
+
|
|
179
186
|
export function filterAndSortItems(items: Item[], query: string): Item[] {
|
|
180
187
|
if (!query.trim()) {
|
|
181
188
|
return clearMatchIndices(items)
|
package/src/styles/theme.tsx
CHANGED
|
@@ -269,7 +269,7 @@ export function getModalStyle(theme: ThemeColors, columns: number, preferredWidt
|
|
|
269
269
|
}
|
|
270
270
|
}
|
|
271
271
|
|
|
272
|
-
const ThemeContext = createContext<ThemeColors>(
|
|
272
|
+
const ThemeContext = createContext<ThemeColors>(resolveTheme(DEFAULT_THEME_ID, {}, 'dark').colors)
|
|
273
273
|
|
|
274
274
|
export function ThemeProvider(props: { theme: ThemeColors; children: ReactNode }) {
|
|
275
275
|
return <ThemeContext.Provider value={props.theme}>{props.children}</ThemeContext.Provider>
|
package/src/tmux/actions.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { spawn } from 'bun'
|
|
2
2
|
import { sanitizeTmuxSessionName } from './helpers'
|
|
3
3
|
|
|
4
|
+
type SpawnOptions = Parameters<typeof spawn>[1]
|
|
5
|
+
|
|
4
6
|
export interface CreateTmuxSessionOptions {
|
|
5
7
|
startupCommand?: string
|
|
6
8
|
}
|
|
@@ -18,8 +20,43 @@ async function tmuxSessionExists(sessionName: string): Promise<boolean> {
|
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
async function switchClient(sessionName: string): Promise<void> {
|
|
21
|
-
|
|
23
|
+
await runTmuxCommand(
|
|
24
|
+
['tmux', 'switch-client', '-t', sessionName],
|
|
25
|
+
`Failed to switch to tmux session '${sessionName}'`
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function runTmuxCommand(
|
|
30
|
+
command: string[],
|
|
31
|
+
errorMessage: string,
|
|
32
|
+
options?: SpawnOptions
|
|
33
|
+
): Promise<void> {
|
|
34
|
+
const proc = spawn(command, options)
|
|
35
|
+
const stderrPromise =
|
|
36
|
+
proc.stderr instanceof ReadableStream ? new Response(proc.stderr).text() : Promise.resolve('')
|
|
22
37
|
await proc.exited
|
|
38
|
+
const stderr = (await stderrPromise).trim()
|
|
39
|
+
|
|
40
|
+
if (proc.exitCode !== 0) {
|
|
41
|
+
throw new Error(stderr ? `${errorMessage}: ${stderr}` : errorMessage)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function openTmuxSession(sessionName: string): Promise<void> {
|
|
46
|
+
if (process.env.TMUX) {
|
|
47
|
+
await switchClient(sessionName)
|
|
48
|
+
return
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
await runTmuxCommand(
|
|
52
|
+
['tmux', 'attach-session', '-t', sessionName],
|
|
53
|
+
`Failed to attach tmux session '${sessionName}'`,
|
|
54
|
+
{
|
|
55
|
+
stdin: 'inherit',
|
|
56
|
+
stdout: 'inherit',
|
|
57
|
+
stderr: 'inherit',
|
|
58
|
+
}
|
|
59
|
+
)
|
|
23
60
|
}
|
|
24
61
|
|
|
25
62
|
export async function createTmuxSession(
|
|
@@ -36,33 +73,29 @@ export async function createTmuxSession(
|
|
|
36
73
|
command.push(options.startupCommand)
|
|
37
74
|
}
|
|
38
75
|
|
|
39
|
-
|
|
76
|
+
await runTmuxCommand(command, `Failed to create tmux session '${sessionName}'`, {
|
|
40
77
|
stdin: 'inherit',
|
|
41
78
|
stdout: 'inherit',
|
|
42
79
|
stderr: 'inherit',
|
|
43
80
|
})
|
|
44
|
-
await proc.exited
|
|
45
81
|
return
|
|
46
82
|
}
|
|
47
83
|
|
|
48
84
|
if (!(await tmuxSessionExists(sessionName))) {
|
|
49
|
-
|
|
50
|
-
|
|
85
|
+
await runTmuxCommand(
|
|
86
|
+
['tmux', 'new-session', '-d', '-s', sessionName, '-c', path],
|
|
87
|
+
`Failed to create tmux session '${sessionName}'`
|
|
88
|
+
)
|
|
51
89
|
|
|
52
90
|
if (options.startupCommand) {
|
|
53
|
-
|
|
54
|
-
'tmux',
|
|
55
|
-
'
|
|
56
|
-
|
|
57
|
-
sessionName,
|
|
58
|
-
options.startupCommand,
|
|
59
|
-
'Enter',
|
|
60
|
-
])
|
|
61
|
-
await startupProc.exited
|
|
91
|
+
await runTmuxCommand(
|
|
92
|
+
['tmux', 'send-keys', '-t', sessionName, options.startupCommand, 'Enter'],
|
|
93
|
+
`Failed to run startup command for tmux session '${sessionName}'`
|
|
94
|
+
)
|
|
62
95
|
}
|
|
63
96
|
}
|
|
64
97
|
|
|
65
|
-
await
|
|
98
|
+
await openTmuxSession(sessionName)
|
|
66
99
|
}
|
|
67
100
|
|
|
68
101
|
export async function createNamedTmuxSession(name: string): Promise<void> {
|
|
@@ -70,34 +103,43 @@ export async function createNamedTmuxSession(name: string): Promise<void> {
|
|
|
70
103
|
const insideTmux = !!process.env.TMUX
|
|
71
104
|
|
|
72
105
|
if (!insideTmux && !(await isTmuxRunning())) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
106
|
+
await runTmuxCommand(
|
|
107
|
+
['tmux', 'new-session', '-s', sessionName],
|
|
108
|
+
`Failed to create tmux session '${sessionName}'`,
|
|
109
|
+
{
|
|
110
|
+
stdin: 'inherit',
|
|
111
|
+
stdout: 'inherit',
|
|
112
|
+
stderr: 'inherit',
|
|
113
|
+
}
|
|
114
|
+
)
|
|
79
115
|
return
|
|
80
116
|
}
|
|
81
117
|
|
|
82
118
|
if (!(await tmuxSessionExists(sessionName))) {
|
|
83
|
-
|
|
84
|
-
|
|
119
|
+
await runTmuxCommand(
|
|
120
|
+
['tmux', 'new-session', '-d', '-s', sessionName],
|
|
121
|
+
`Failed to create tmux session '${sessionName}'`
|
|
122
|
+
)
|
|
85
123
|
}
|
|
86
124
|
|
|
87
|
-
await
|
|
125
|
+
await openTmuxSession(sessionName)
|
|
88
126
|
}
|
|
89
127
|
|
|
90
128
|
export async function switchTmuxSession(name: string): Promise<void> {
|
|
91
|
-
await
|
|
129
|
+
await openTmuxSession(name)
|
|
92
130
|
}
|
|
93
131
|
|
|
94
132
|
export async function killTmuxSession(name: string): Promise<void> {
|
|
95
|
-
|
|
96
|
-
|
|
133
|
+
await runTmuxCommand(
|
|
134
|
+
['tmux', 'kill-session', '-t', name],
|
|
135
|
+
`Failed to kill tmux session '${name}'`
|
|
136
|
+
)
|
|
97
137
|
}
|
|
98
138
|
|
|
99
139
|
export async function renameTmuxSession(oldName: string, newName: string): Promise<void> {
|
|
100
140
|
const sanitizedName = sanitizeTmuxSessionName(newName)
|
|
101
|
-
|
|
102
|
-
|
|
141
|
+
await runTmuxCommand(
|
|
142
|
+
['tmux', 'rename-session', '-t', oldName, sanitizedName],
|
|
143
|
+
`Failed to rename tmux session '${oldName}'`
|
|
144
|
+
)
|
|
103
145
|
}
|
package/src/tmux/details.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { spawn } from 'bun'
|
|
2
2
|
import type { SessionDetails, WindowInfo } from '../types'
|
|
3
3
|
import { formatPanePreview, formatWindowCommand, readTmuxValue } from './helpers'
|
|
4
|
+
import { toHomeRelativePath } from '../util/path-display'
|
|
4
5
|
|
|
5
6
|
const SESSION_DETAILS_CACHE_TTL_MS = 4000
|
|
6
7
|
const sessionDetailsCache = new Map<string, { details: SessionDetails; expiresAt: number }>()
|
|
@@ -25,71 +26,51 @@ export async function getSessionDetails(sessionName: string): Promise<SessionDet
|
|
|
25
26
|
return cached.details
|
|
26
27
|
}
|
|
27
28
|
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
29
|
+
const statusOutput = await readTmuxValue(
|
|
30
|
+
[
|
|
31
|
+
'tmux',
|
|
32
|
+
'list-sessions',
|
|
33
|
+
'-F',
|
|
34
|
+
'#{session_name}:#{session_attached}:#{session_windows}',
|
|
35
|
+
'-f',
|
|
36
|
+
`#{==:#{session_name},${sessionName}}`,
|
|
37
|
+
],
|
|
38
|
+
`Failed to load tmux session details for '${sessionName}'`
|
|
39
|
+
)
|
|
37
40
|
const [, attached, windowCount] = statusOutput.trim().split(':')
|
|
38
41
|
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
42
|
+
const windowsOutput = await readTmuxValue(
|
|
43
|
+
[
|
|
44
|
+
'tmux',
|
|
45
|
+
'list-windows',
|
|
46
|
+
'-t',
|
|
47
|
+
sessionName,
|
|
48
|
+
'-F',
|
|
49
|
+
'#{window_index}\t#{window_name}\t#{pane_current_path}\t#{pane_current_command}\t#{window_active}',
|
|
50
|
+
],
|
|
51
|
+
`Failed to load tmux windows for '${sessionName}'`
|
|
52
|
+
)
|
|
48
53
|
const windowLines = windowsOutput.trim().split('\n').filter(Boolean)
|
|
49
54
|
|
|
50
55
|
const windows: WindowInfo[] = []
|
|
56
|
+
let activeWindowIndex = ''
|
|
51
57
|
|
|
52
58
|
for (const line of windowLines) {
|
|
53
|
-
const [index, name
|
|
59
|
+
const [index = '', name = '', currentPath = '', currentCommand = '', active = '0'] =
|
|
60
|
+
line.split('\t')
|
|
54
61
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
'-t',
|
|
59
|
-
`${sessionName}:${index}`,
|
|
60
|
-
'-p',
|
|
61
|
-
'#{pane_current_path}',
|
|
62
|
-
])
|
|
63
|
-
const currentPath = (await new Response(dirProc.stdout).text()).trim()
|
|
64
|
-
|
|
65
|
-
const cmdProc = spawn([
|
|
66
|
-
'tmux',
|
|
67
|
-
'display-message',
|
|
68
|
-
'-t',
|
|
69
|
-
`${sessionName}:${index}`,
|
|
70
|
-
'-p',
|
|
71
|
-
'#{pane_current_command}',
|
|
72
|
-
])
|
|
73
|
-
const currentCommand = (await new Response(cmdProc.stdout).text()).trim()
|
|
62
|
+
if (active === '1') {
|
|
63
|
+
activeWindowIndex = index
|
|
64
|
+
}
|
|
74
65
|
|
|
75
66
|
windows.push({
|
|
76
67
|
index,
|
|
77
68
|
name,
|
|
78
|
-
currentPath: currentPath
|
|
69
|
+
currentPath: toHomeRelativePath(currentPath),
|
|
79
70
|
currentCommand: formatWindowCommand(currentCommand),
|
|
80
71
|
})
|
|
81
72
|
}
|
|
82
73
|
|
|
83
|
-
const activeWindowProc = spawn([
|
|
84
|
-
'tmux',
|
|
85
|
-
'display-message',
|
|
86
|
-
'-t',
|
|
87
|
-
sessionName,
|
|
88
|
-
'-p',
|
|
89
|
-
'#{window_index}',
|
|
90
|
-
])
|
|
91
|
-
const activeWindowIndex = (await new Response(activeWindowProc.stdout).text()).trim()
|
|
92
|
-
|
|
93
74
|
let panePreviewLines: string[] | undefined
|
|
94
75
|
if (activeWindowIndex.length > 0) {
|
|
95
76
|
const paneProc = spawn([
|