mux-sesh 1.7.2 → 1.9.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.
@@ -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
+ }
@@ -120,6 +120,10 @@ function calculateItemBonus(item: Item, query: string): number {
120
120
  bonus -= depth * 2
121
121
  }
122
122
 
123
+ if (item.isPinned) {
124
+ bonus += 50000
125
+ }
126
+
123
127
  return bonus
124
128
  }
125
129
 
@@ -172,6 +176,13 @@ export function clearMatchIndices(items: Item[]): Item[] {
172
176
  return items.map(item => withSearchMatch(item))
173
177
  }
174
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
+
175
186
  export function filterAndSortItems(items: Item[], query: string): Item[] {
176
187
  if (!query.trim()) {
177
188
  return clearMatchIndices(items)
package/src/settings.ts CHANGED
@@ -18,6 +18,7 @@ export type SettingsFieldId =
18
18
  | 'defaultPreviewCommand'
19
19
  | 'projectPaths'
20
20
  | 'hiddenSessions'
21
+ | 'pinnedSessions'
21
22
  | 'icons'
22
23
  | 'projects'
23
24
  | 'wildcards'
@@ -301,6 +302,18 @@ const SETTINGS_FIELDS: readonly SettingsFieldDefinition[] = [
301
302
  raw.hidden_sessions = JSON.parse(value) as unknown
302
303
  },
303
304
  },
305
+ {
306
+ id: 'pinnedSessions',
307
+ label: 'Pinned Sessions',
308
+ hint: 'JSON array of session names',
309
+ kind: 'json',
310
+ title: 'Pinned Sessions',
311
+ getValue: config => `${config.pinnedSessions?.length ?? 0} pinned`,
312
+ getEditorValue: config => formatJSON(config.pinnedSessions ?? []),
313
+ applyEditor: (raw, value) => {
314
+ raw.pinned_sessions = JSON.parse(value) as unknown
315
+ },
316
+ },
304
317
  {
305
318
  id: 'icons',
306
319
  label: 'Icons',
@@ -12,7 +12,7 @@ export enum ViewMode {
12
12
 
13
13
  export type KeybindMode = 'vim' | 'standard'
14
14
  export type ZoxideMode = 'off' | 'rank' | 'merge'
15
- export type ItemKind = 'tmux' | 'configured' | 'project'
15
+ export type ItemKind = 'tmux' | 'configured' | 'project' | 'file'
16
16
  export type SortOrder = 'live-first' | 'configured-first' | 'zoxide-first' | 'alphabetical'
17
17
 
18
18
  export interface IconConfig {
@@ -142,6 +142,7 @@ export interface Item {
142
142
  isSession: boolean
143
143
  itemKind?: ItemKind
144
144
  icon?: string
145
+ isPinned?: boolean
145
146
  isAttached?: boolean
146
147
  linkedSessionName?: string
147
148
  linkedSessionAttached?: boolean
@@ -162,6 +163,7 @@ export interface Config {
162
163
  autoUpdate?: boolean
163
164
  dirLength?: number
164
165
  hiddenSessions?: string[]
166
+ pinnedSessions?: string[]
165
167
  zoxideMode?: ZoxideMode
166
168
  sortOrder?: SortOrder
167
169
  theme?: string
@@ -12,6 +12,7 @@ export type CommandId =
12
12
  | 'view-sessions'
13
13
  | 'rename-session'
14
14
  | 'kill-session'
15
+ | 'toggle-pin-session'
15
16
  | 'last-session'
16
17
  | 'root-session'
17
18
  | 'edit-target'
@@ -69,6 +70,16 @@ export function getCommandEntries(
69
70
  { id: 'new-session', category: 'Direct', title: 'New session', keybind: 'n' },
70
71
  { id: 'open-opencode', category: 'Direct', title: 'Open OpenCode sessions', keybind: 'o' },
71
72
  { id: 'kill-session', category: 'Direct', title: 'Kill session', keybind: 'd' },
73
+ ...(selectedItem && (selectedItem.isSession || selectedItem.itemKind === 'configured')
74
+ ? [
75
+ {
76
+ id: 'toggle-pin-session' as const,
77
+ category: 'Direct',
78
+ title: selectedItem.isPinned ? 'Unpin session' : 'Pin session',
79
+ keybind: 'ctrl+f',
80
+ },
81
+ ]
82
+ : []),
72
83
  { id: 'open-settings', category: 'Direct', title: 'Open settings', keybind: 'ctrl+p' },
73
84
  {
74
85
  id: 'view-projects',
@@ -37,7 +37,11 @@ export default function ItemList({
37
37
  const hasTitleMatch = titleMatchIndices && titleMatchIndices.length > 0
38
38
  const hasDescMatch = descMatchIndices && descMatchIndices.length > 0
39
39
  const icon = getItemIconPresentation(theme, item, icons)
40
- const sessionStatusLabel = item.linkedSessionName ? 'attach' : 'create'
40
+ const sessionStatusLabel = item.linkedSessionName
41
+ ? 'attach'
42
+ : item.itemKind === 'file'
43
+ ? 'open'
44
+ : 'create'
41
45
  const linkedSessionLabel = item.linkedSessionName ? ` -> ${item.linkedSessionName}` : ''
42
46
  const pendingKill = item.isSession && item.title === pendingKillSessionName
43
47
  const selected = absoluteIndex === cursor
@@ -1,5 +1,6 @@
1
1
  import { useEffect, useState } from 'react'
2
- import { getProjectPreview, type ProjectPreview } from '../preview/project'
2
+ import { getFilePreview, getProjectPreview, type ProjectPreview } from '../preview/project'
3
+ import { isFileItem } from '../files/target'
3
4
  import { getSessionDetails } from '../tmux'
4
5
  import { getDetailPanelStyle, useTheme } from '../styles/theme'
5
6
  import type { Config, Item, SessionDetails } from '../types'
@@ -24,6 +25,8 @@ function formatSourceLabel(source: ProjectPreview['source']): string {
24
25
  return 'Wildcard rule'
25
26
  case 'default':
26
27
  return 'Default rule'
28
+ case 'file':
29
+ return 'File target'
27
30
  }
28
31
  }
29
32
 
@@ -179,11 +182,9 @@ export default function SessionDetailsPanel({ selectedItem, config }: Props) {
179
182
  return
180
183
  }
181
184
 
182
- const details = await getProjectPreview(
183
- selectedItem.path,
184
- config,
185
- selectedItem.linkedSessionName
186
- )
185
+ const details = isFileItem(selectedItem)
186
+ ? await getFilePreview(selectedItem.path, config, selectedItem.linkedSessionName)
187
+ : await getProjectPreview(selectedItem.path, config, selectedItem.linkedSessionName)
187
188
  if (!cancelled) {
188
189
  setDetailState({ status: 'project', details })
189
190
  }
@@ -1,3 +1,4 @@
1
+ import type { SessionSection } from '../items/order'
1
2
  import type { IconConfig, Item, ThemeColors } from '../types'
2
3
 
3
4
  export interface ItemIconPresentation {
@@ -5,6 +6,8 @@ export interface ItemIconPresentation {
5
6
  color: string
6
7
  }
7
8
 
9
+ const FILE_ITEM_GLYPH = '  '
10
+
8
11
  function getIconConfig(icons?: IconConfig): IconConfig {
9
12
  return {
10
13
  tmux: icons?.tmux ?? '',
@@ -28,6 +31,17 @@ export function getItemIconPresentation(
28
31
  }
29
32
  }
30
33
 
34
+ if (item.itemKind === 'file') {
35
+ return {
36
+ glyph: item.icon ?? FILE_ITEM_GLYPH,
37
+ color: item.linkedSessionAttached
38
+ ? theme.active
39
+ : item.linkedSessionName
40
+ ? theme.inactive
41
+ : theme.fileTree,
42
+ }
43
+ }
44
+
31
45
  if (item.isSession) {
32
46
  return {
33
47
  glyph:
@@ -52,12 +66,14 @@ export function getItemIconPresentation(
52
66
 
53
67
  export function getSessionSectionPresentation(
54
68
  theme: ThemeColors,
55
- section: 'live' | 'configured' | 'opencode',
69
+ section: SessionSection | 'opencode',
56
70
  icons?: IconConfig
57
71
  ): ItemIconPresentation & { label: string } {
58
72
  const resolvedIcons = getIconConfig(icons)
59
73
 
60
74
  switch (section) {
75
+ case 'pinned':
76
+ return { glyph: '', color: theme.accent, label: 'Pinned' }
61
77
  case 'configured':
62
78
  return { glyph: resolvedIcons.configured, color: theme.action, label: 'Configured' }
63
79
  case 'opencode':
@@ -69,7 +85,7 @@ export function getSessionSectionPresentation(
69
85
 
70
86
  export function formatSectionHeader(
71
87
  theme: ThemeColors,
72
- section: 'live' | 'configured' | 'opencode',
88
+ section: SessionSection | 'opencode',
73
89
  icons?: IconConfig
74
90
  ): ItemIconPresentation & { text: string } {
75
91
  const presentation = getSessionSectionPresentation(theme, section, icons)