mux-sesh 1.4.1 → 1.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.
Files changed (45) hide show
  1. package/README.md +123 -7
  2. package/package.json +3 -3
  3. package/src/app.tsx +746 -173
  4. package/src/components/KeybindHelp.tsx +178 -111
  5. package/src/config/index.ts +413 -20
  6. package/src/config/listed-sessions.ts +36 -0
  7. package/src/config/session-rules.ts +101 -0
  8. package/src/handlers/actions.ts +93 -6
  9. package/src/handlers/keyboard.ts +299 -79
  10. package/src/items/order.ts +50 -0
  11. package/src/opencode/session.ts +1 -17
  12. package/src/opencode/stats.ts +1 -1
  13. package/src/preview/project.ts +235 -0
  14. package/src/projects/session-links.ts +31 -0
  15. package/src/search/index.ts +83 -47
  16. package/src/settings.ts +425 -0
  17. package/src/styles/opencode-themes-a.ts +1 -0
  18. package/src/styles/opencode-themes-b.ts +1 -0
  19. package/src/styles/theme.tsx +441 -0
  20. package/src/tmux/index.ts +58 -14
  21. package/src/tmux/projects.ts +151 -20
  22. package/src/tmux/workflows.ts +22 -0
  23. package/src/types/index.ts +114 -0
  24. package/src/ui/CommandsModal.tsx +145 -0
  25. package/src/ui/HighlightedText.tsx +6 -3
  26. package/src/ui/ItemList.tsx +47 -41
  27. package/src/ui/Modal.tsx +41 -0
  28. package/src/ui/OpencodeSessionGroup.tsx +50 -46
  29. package/src/ui/OpencodeStatsPanel.tsx +34 -39
  30. package/src/ui/RenameModal.tsx +42 -0
  31. package/src/ui/SearchInput.tsx +3 -1
  32. package/src/ui/SessionDetailsPanel.tsx +168 -72
  33. package/src/ui/SessionList.tsx +59 -34
  34. package/src/ui/SessionStats.tsx +5 -3
  35. package/src/ui/SettingEditorModal.tsx +49 -0
  36. package/src/ui/SettingOptionsModal.tsx +77 -0
  37. package/src/ui/SettingsModal.tsx +96 -0
  38. package/src/ui/Toast.tsx +8 -9
  39. package/src/ui/item-icon.ts +78 -0
  40. package/src/ui/list-window.ts +22 -0
  41. package/src/update/installation.ts +4 -15
  42. package/src/update/version.ts +2 -1
  43. package/src/util/glob.ts +47 -0
  44. package/src/util/terminal.ts +6 -20
  45. package/src/styles/theme.ts +0 -50
package/src/app.tsx CHANGED
@@ -2,39 +2,68 @@ import type { TextareaRenderable } from '@opentui/core'
2
2
  import { useKeyboard } from '@opentui/react'
3
3
  import { useState, useEffect, useRef } from 'react'
4
4
  import { AppMode, ViewMode, type Item, type Config, type OpencodeStatsState } from './types'
5
- import { loadConfig } from './config'
5
+ import { getConfigPath, loadConfig, saveConfig } from './config'
6
+ import { getListedSessionItems, mergeSessionItems } from './config/listed-sessions'
7
+ import { orderProjectItems, orderSessionItems } from './items/order'
8
+ import { annotateProjectItemsWithSessionLinks } from './projects/session-links'
6
9
  import { listTmuxSessions } from './tmux'
10
+ import { filterHiddenSessions } from './tmux/workflows'
7
11
  import { getProjectItems } from './tmux/projects'
8
12
  import { filterAndSortItems, clearMatchIndices } from './search'
9
13
  import { isGitHubURL } from './util/github'
10
14
  import { getOpencodeSessionStats } from './opencode'
11
- import { formatSessionAge } from './util/time'
12
- import { colors, sessionListStyle, sessionListStyleFull } from './styles/theme'
15
+ import { ThemeProvider, getPanelStyle, resolveTheme } from './styles/theme'
13
16
  import { useTerminalSize, shouldShowDetailPanel } from './util/terminal'
14
17
  import Toast from './ui/Toast'
15
18
  import { checkAndUpdate, updateEvents } from './update'
16
- import KeybindHelp from './components/KeybindHelp'
17
19
  import OpencodeStatsPanel from './ui/OpencodeStatsPanel'
18
20
  import SessionDetailsPanel from './ui/SessionDetailsPanel'
19
21
  import SessionList from './ui/SessionList'
20
22
  import OpencodeSessionGroup from './ui/OpencodeSessionGroup'
21
23
  import SearchInput from './ui/SearchInput'
22
- import SessionStats from './ui/SessionStats'
23
24
  import ItemList from './ui/ItemList'
25
+ import RenameModal from './ui/RenameModal'
26
+ import CommandsModal, { filterCommandEntries, getCommandEntries, type CommandId } from './ui/CommandsModal'
27
+ import SettingsModal from './ui/SettingsModal'
28
+ import SettingOptionsModal from './ui/SettingOptionsModal'
29
+ import SettingEditorModal from './ui/SettingEditorModal'
24
30
  import {
31
+ applyEditorSetting,
32
+ applyOptionSetting,
33
+ filterSettingsEntries,
34
+ filterSettingsOptions,
35
+ getSettingEditorTitle,
36
+ getSettingEditorValue,
37
+ getSettingsEntries,
38
+ getSettingOptions,
39
+ isOptionSetting,
40
+ type SettingsFieldId,
41
+ } from './settings'
42
+ import {
43
+ type KeyboardHandlerContext,
25
44
  handleNormalMode,
26
45
  handleOpencodeManageMode,
27
46
  handleSearchMode,
28
47
  handleNewSessionMode,
29
- handleRenameMode,
30
48
  } from './handlers/keyboard'
31
49
  import {
32
- handleSelect,
50
+ handleSelect as actionHandleSelect,
33
51
  handleKillSession as actionKillSession,
52
+ handleLastSession as actionHandleLastSession,
34
53
  handleRenameSubmit as actionRenameSubmit,
35
54
  handleNewSessionSubmit as actionNewSessionSubmit,
55
+ handleRootSession as actionHandleRootSession,
56
+ handleEditTarget as actionHandleEditTarget,
36
57
  } from './handlers/actions'
37
58
 
59
+ type ModalState =
60
+ | { type: 'rename'; target: string }
61
+ | { type: 'commands' }
62
+ | { type: 'settings' }
63
+ | { type: 'setting-options'; field: SettingsFieldId }
64
+ | { type: 'setting-editor'; field: SettingsFieldId }
65
+ | null
66
+
38
67
  export function App() {
39
68
  const [appMode, setAppMode] = useState(AppMode.Normal)
40
69
  const [viewMode, setViewMode] = useState(ViewMode.Sessions)
@@ -48,12 +77,67 @@ export function App() {
48
77
  const [message, setMessage] = useState('')
49
78
  const [renameTarget, setRenameTarget] = useState('')
50
79
  const [config, setConfig] = useState<Config | null>(null)
80
+ const [modalState, setModalState] = useState<ModalState>(null)
81
+ const [modalInputValue, setModalInputValue] = useState('')
82
+ const [commandsCursor, setCommandsCursor] = useState(0)
83
+ const [commandsSearchQuery, setCommandsSearchQuery] = useState('')
84
+ const [settingsCursor, setSettingsCursor] = useState(0)
85
+ const [settingsSearchQuery, setSettingsSearchQuery] = useState('')
86
+ const [settingOptionsCursor, setSettingOptionsCursor] = useState(0)
87
+ const [settingOptionsSearchQuery, setSettingOptionsSearchQuery] = useState('')
88
+ const [settingEditorValue, setSettingEditorValue] = useState('')
89
+ const [settingEditorError, setSettingEditorError] = useState('')
90
+ const [pendingKillSessionName, setPendingKillSessionName] = useState<string | null>(null)
51
91
  const [prefixActive, setPrefixActive] = useState(false)
52
92
  const prefixTimeoutRef = useRef<NodeJS.Timeout | null>(null)
53
93
  const textareaRef = useRef<TextareaRenderable | null>(null)
54
- const { columns } = useTerminalSize()
94
+ const modalTextareaRef = useRef<TextareaRenderable | null>(null)
95
+ const commandsSearchTextareaRef = useRef<TextareaRenderable | null>(null)
96
+ const settingsSearchTextareaRef = useRef<TextareaRenderable | null>(null)
97
+ const settingOptionsSearchTextareaRef = useRef<TextareaRenderable | null>(null)
98
+ const settingEditorTextareaRef = useRef<TextareaRenderable | null>(null)
99
+ const lastSessionSelectionRef = useRef<string | null>(null)
100
+ const lastProjectSelectionRef = useRef<string | null>(null)
101
+ const { columns, rows } = useTerminalSize()
55
102
  const [toastMessage, setToastMessage] = useState('')
56
103
  const [toastVisible, setToastVisible] = useState(false)
104
+ const configPath = getConfigPath()
105
+ const resolvedTheme = resolveTheme(config?.theme, config?.themes, config?.colorScheme)
106
+ const theme = resolvedTheme.colors
107
+ const settingsEntries = config ? getSettingsEntries(config) : []
108
+ const filteredSettingsEntries = filterSettingsEntries(settingsEntries, settingsSearchQuery)
109
+ const currentOptionField = modalState?.type === 'setting-options' ? modalState.field : undefined
110
+ const settingOptions = config && currentOptionField ? getSettingOptions(config, currentOptionField) : []
111
+ const filteredSettingOptions = filterSettingsOptions(settingOptions, settingOptionsSearchQuery)
112
+
113
+ function getSessionSelectionIndex(nextItems: Item[]): number {
114
+ const regularItems = nextItems.filter(item => !(item.isSession && item.title.startsWith('opencode-')))
115
+ if (regularItems.length === 0) {
116
+ return 0
117
+ }
118
+
119
+ const selection = lastSessionSelectionRef.current
120
+ if (!selection) {
121
+ return 0
122
+ }
123
+
124
+ const index = regularItems.findIndex(item => item.title === selection)
125
+ return index >= 0 ? index : 0
126
+ }
127
+
128
+ function getProjectSelectionIndex(nextItems: Item[]): number {
129
+ if (nextItems.length === 0) {
130
+ return 0
131
+ }
132
+
133
+ const selection = lastProjectSelectionRef.current
134
+ if (!selection) {
135
+ return 0
136
+ }
137
+
138
+ const index = nextItems.findIndex(item => item.path === selection)
139
+ return index >= 0 ? index : 0
140
+ }
57
141
 
58
142
  function updateOpencodeState(sessionName: string, nextState: OpencodeStatsState) {
59
143
  const applyState = (existingItems: Item[]) =>
@@ -76,21 +160,33 @@ export function App() {
76
160
  setAppMode(AppMode.Search)
77
161
  }
78
162
 
79
- const sessions = await listTmuxSessions()
80
- const projects = await getProjectItems(cfg)
163
+ const [sessions, listedSessions] = await Promise.all([listTmuxSessions(), getListedSessionItems(cfg)])
164
+ const visibleSessions = filterHiddenSessions(sessions, cfg.hiddenSessions)
165
+ const combinedSessions = orderSessionItems(mergeSessionItems(visibleSessions, listedSessions), cfg.sortOrder)
166
+
167
+ if (combinedSessions.length > 0) {
168
+ setViewMode(ViewMode.Sessions)
169
+ setSessionItems(combinedSessions)
170
+ setAllItems(combinedSessions)
171
+ setItems(combinedSessions)
172
+ setCursor(getSessionSelectionIndex(combinedSessions))
173
+ }
174
+
175
+ const rawProjects = await getProjectItems(cfg)
176
+ const projects = orderProjectItems(
177
+ await annotateProjectItemsWithSessionLinks(rawProjects, visibleSessions, cfg),
178
+ cfg.sortOrder
179
+ )
81
180
 
82
181
  setProjectItems(projects)
83
182
 
84
- if (sessions.length > 0) {
85
- setViewMode(ViewMode.Sessions)
86
- setSessionItems(sessions)
87
- setAllItems(sessions)
88
- setItems(sessions)
89
- } else {
183
+ if (combinedSessions.length === 0) {
90
184
  setViewMode(ViewMode.Projects)
91
185
  setAllItems(projects)
92
186
  setItems(projects)
187
+ setCursor(getProjectSelectionIndex(projects))
93
188
  }
189
+
94
190
  checkAndUpdate(cfg).catch(error => {
95
191
  console.error('Auto-update check failed:', error)
96
192
  })
@@ -98,25 +194,40 @@ export function App() {
98
194
  init()
99
195
  }, [])
100
196
 
101
- async function refreshItems(forceViewMode?: ViewMode) {
102
- if (!config) return
197
+ async function refreshItems(forceViewMode?: ViewMode, nextConfig = config) {
198
+ if (!nextConfig) return
103
199
 
104
200
  const targetMode = forceViewMode ?? viewMode
105
201
 
106
202
  if (targetMode === ViewMode.Sessions) {
107
203
  const sessions = await listTmuxSessions()
108
- clearMatchIndices(sessions)
109
- setSessionItems(sessions)
110
- setAllItems(sessions)
111
- setItems(sessions)
204
+ const visibleSessions = filterHiddenSessions(sessions, nextConfig.hiddenSessions)
205
+ const listedSessions = await getListedSessionItems(nextConfig)
206
+ const cleanSessions = clearMatchIndices(
207
+ orderSessionItems(mergeSessionItems(visibleSessions, listedSessions), nextConfig.sortOrder)
208
+ )
209
+ const linkedProjects = orderProjectItems(
210
+ await annotateProjectItemsWithSessionLinks(projectItems, visibleSessions, nextConfig),
211
+ nextConfig.sortOrder
212
+ )
213
+ setSessionItems(cleanSessions)
214
+ setProjectItems(linkedProjects)
215
+ setAllItems(cleanSessions)
216
+ setItems(cleanSessions)
217
+ setCursor(getSessionSelectionIndex(cleanSessions))
112
218
  } else {
113
- const projects = await getProjectItems(config)
114
- clearMatchIndices(projects)
115
- setProjectItems(projects)
116
- setAllItems(projects)
117
- setItems(projects)
219
+ const sessions = await listTmuxSessions()
220
+ const visibleSessions = filterHiddenSessions(sessions, nextConfig.hiddenSessions)
221
+ const projects = orderProjectItems(
222
+ await annotateProjectItemsWithSessionLinks(await getProjectItems(nextConfig), visibleSessions, nextConfig),
223
+ nextConfig.sortOrder
224
+ )
225
+ const cleanProjects = clearMatchIndices(projects)
226
+ setProjectItems(cleanProjects)
227
+ setAllItems(cleanProjects)
228
+ setItems(cleanProjects)
229
+ setCursor(getProjectSelectionIndex(cleanProjects))
118
230
  }
119
- setCursor(0)
120
231
  }
121
232
 
122
233
  async function loadOpencodeStatsForSession(sessionName: string) {
@@ -147,7 +258,7 @@ export function App() {
147
258
  const regularSessions =
148
259
  viewMode === ViewMode.Sessions &&
149
260
  (appMode === AppMode.Normal || appMode === AppMode.OpencodeManage)
150
- ? items.filter(item => item.isSession && !item.title.startsWith('opencode-'))
261
+ ? items.filter(item => !(item.isSession && item.title.startsWith('opencode-')))
151
262
  : items
152
263
 
153
264
  const opencodeSessions =
@@ -157,8 +268,14 @@ export function App() {
157
268
  : []
158
269
  const selectedOpencodeSessionName =
159
270
  appMode === AppMode.OpencodeManage ? opencodeSessions[opencodeCursor]?.title : undefined
271
+ const selectedPrimaryItem =
272
+ viewMode === ViewMode.Sessions && appMode === AppMode.Normal ? regularSessions[cursor] : items[cursor]
273
+ const commandEntries = getCommandEntries(appMode, config?.keybindMode, config?.prefixKey, selectedPrimaryItem)
274
+ const filteredCommandEntries = filterCommandEntries(commandEntries, commandsSearchQuery)
160
275
 
161
276
  async function handleKillSessionWrapper(sessionName: string) {
277
+ setPendingKillSessionName(null)
278
+
162
279
  await actionKillSession(sessionName, {
163
280
  onSuccess: msg => {
164
281
  setMessage(msg)
@@ -172,8 +289,109 @@ export function App() {
172
289
  })
173
290
  }
174
291
 
292
+ async function handleSelectWrapper(item: Item) {
293
+ await actionHandleSelect(item, config)
294
+ }
295
+
296
+ async function handleLastSessionWrapper() {
297
+ try {
298
+ await actionHandleLastSession(sessionItems)
299
+ } catch (error) {
300
+ const errorMessage = error instanceof Error ? error.message : 'Failed to switch to the previous session'
301
+ setMessage(errorMessage)
302
+ setTimeout(() => setMessage(''), 3000)
303
+ }
304
+ }
305
+
306
+ async function handleRootSessionWrapper(item?: Item) {
307
+ try {
308
+ await actionHandleRootSession(item, config)
309
+ } catch (error) {
310
+ const errorMessage = error instanceof Error ? error.message : 'Failed to open the root session'
311
+ setMessage(errorMessage)
312
+ setTimeout(() => setMessage(''), 3000)
313
+ }
314
+ }
315
+
316
+ async function handleEditTargetWrapper(item?: Item) {
317
+ try {
318
+ await actionHandleEditTarget(item, config)
319
+ } catch (error) {
320
+ const errorMessage = error instanceof Error ? error.message : 'Failed to edit target'
321
+ setMessage(errorMessage)
322
+ setTimeout(() => setMessage(''), 3000)
323
+ }
324
+ }
325
+
326
+ function clearPendingKill() {
327
+ setPendingKillSessionName(null)
328
+ }
329
+
330
+ function requestKillSession(sessionName: string) {
331
+ if (pendingKillSessionName === sessionName) {
332
+ void handleKillSessionWrapper(sessionName)
333
+ return
334
+ }
335
+
336
+ setPendingKillSessionName(sessionName)
337
+ }
338
+
339
+ function openRenameModal(sessionName: string) {
340
+ clearPendingKill()
341
+ setRenameTarget(sessionName)
342
+ setModalInputValue(sessionName)
343
+ setModalState({ type: 'rename', target: sessionName })
344
+ }
345
+
346
+ function openCommandsModal() {
347
+ clearPendingKill()
348
+ setCommandsSearchQuery('')
349
+ setCommandsCursor(0)
350
+ setModalState({ type: 'commands' })
351
+ }
352
+
353
+ function openSettingsModal() {
354
+ if (!config) {
355
+ return
356
+ }
357
+
358
+ clearPendingKill()
359
+ setSettingEditorError('')
360
+ setSettingsSearchQuery('')
361
+ setSettingsCursor(0)
362
+ setModalState({ type: 'settings' })
363
+ }
364
+
365
+ function openSettingOptions(field: SettingsFieldId) {
366
+ setSettingOptionsSearchQuery('')
367
+ setSettingOptionsCursor(0)
368
+ setModalState({ type: 'setting-options', field })
369
+ }
370
+
371
+ function openSettingEditor(field: SettingsFieldId) {
372
+ if (!config) {
373
+ return
374
+ }
375
+
376
+ setSettingEditorError('')
377
+ setSettingEditorValue(getSettingEditorValue(config, field))
378
+ setModalState({ type: 'setting-editor', field })
379
+ }
380
+
381
+ function closeModal() {
382
+ setModalState(null)
383
+ setModalInputValue('')
384
+ setCommandsSearchQuery('')
385
+ setCommandsCursor(0)
386
+ setSettingsSearchQuery('')
387
+ setSettingOptionsSearchQuery('')
388
+ setSettingEditorValue('')
389
+ setSettingEditorError('')
390
+ setRenameTarget('')
391
+ }
392
+
175
393
  async function handleRenameSubmit() {
176
- const newName = searchQuery.trim()
394
+ const newName = (modalTextareaRef.current?.plainText ?? modalInputValue).trim()
177
395
  if (newName && newName !== renameTarget) {
178
396
  await actionRenameSubmit(renameTarget, newName, {
179
397
  onSuccess: msg => {
@@ -187,9 +405,8 @@ export function App() {
187
405
  refreshItems,
188
406
  })
189
407
  }
190
- setAppMode(AppMode.Normal)
191
- setSearchQuery('')
192
- setRenameTarget('')
408
+
409
+ closeModal()
193
410
  }
194
411
 
195
412
  async function handleNewSessionSubmit() {
@@ -198,13 +415,128 @@ export function App() {
198
415
 
199
416
  try {
200
417
  await actionNewSessionSubmit(searchTerm, config, items, cursor)
201
- } catch (error: any) {
202
- setMessage(error.message)
418
+ } catch (error) {
419
+ const errorMessage = error instanceof Error ? error.message : 'Failed to create session'
420
+ setMessage(errorMessage)
203
421
  setTimeout(() => setMessage(''), 3000)
204
422
  }
205
423
  }
206
424
 
207
- const keyboardContext = {
425
+ async function executeCommand(commandID: CommandId) {
426
+ switch (commandID) {
427
+ case 'search':
428
+ closeModal()
429
+ setAppMode(AppMode.Search)
430
+ setSearchQuery('')
431
+ return
432
+ case 'new-session':
433
+ closeModal()
434
+ setAppMode(AppMode.NewSession)
435
+ setViewMode(ViewMode.Projects)
436
+ setAllItems(projectItems)
437
+ setItems(projectItems)
438
+ setCursor(Math.max(0, projectItems.length - 1))
439
+ setSearchQuery('')
440
+ return
441
+ case 'open-settings':
442
+ openSettingsModal()
443
+ return
444
+ case 'view-projects':
445
+ closeModal()
446
+ setViewMode(ViewMode.Projects)
447
+ await refreshItems(ViewMode.Projects)
448
+ return
449
+ case 'view-sessions':
450
+ closeModal()
451
+ setViewMode(ViewMode.Sessions)
452
+ await refreshItems(ViewMode.Sessions)
453
+ return
454
+ case 'rename-session': {
455
+ const target =
456
+ appMode === AppMode.OpencodeManage ? opencodeSessions[opencodeCursor] : viewMode === ViewMode.Sessions ? regularSessions[cursor] : undefined
457
+ if (target?.isSession) {
458
+ openRenameModal(target.title)
459
+ }
460
+ return
461
+ }
462
+ case 'kill-session': {
463
+ const target =
464
+ appMode === AppMode.OpencodeManage ? opencodeSessions[opencodeCursor] : viewMode === ViewMode.Sessions ? regularSessions[cursor] : undefined
465
+ if (target?.isSession) {
466
+ closeModal()
467
+ requestKillSession(target.title)
468
+ }
469
+ return
470
+ }
471
+ case 'last-session':
472
+ closeModal()
473
+ await handleLastSessionWrapper()
474
+ return
475
+ case 'root-session':
476
+ closeModal()
477
+ await handleRootSessionWrapper(selectedPrimaryItem)
478
+ return
479
+ case 'edit-target':
480
+ closeModal()
481
+ await handleEditTargetWrapper(selectedPrimaryItem)
482
+ return
483
+ case 'open-opencode':
484
+ closeModal()
485
+ if (viewMode === ViewMode.Sessions && opencodeSessions.length > 0) {
486
+ setAppMode(AppMode.OpencodeManage)
487
+ setOpencodeCursor(0)
488
+ await loadOpencodeStatsForSession(opencodeSessions[0].title)
489
+ }
490
+ return
491
+ case 'refresh':
492
+ closeModal()
493
+ await refreshItems()
494
+ setMessage('Refreshed')
495
+ setTimeout(() => setMessage(''), 2000)
496
+ return
497
+ case 'back':
498
+ closeModal()
499
+ setAppMode(AppMode.Normal)
500
+ return
501
+ }
502
+ }
503
+
504
+ async function applyAndPersistConfig(nextConfig: Config, successMessage: string) {
505
+ await saveConfig(nextConfig)
506
+ setConfig(nextConfig)
507
+ await refreshItems(undefined, nextConfig)
508
+ setMessage(successMessage)
509
+ setTimeout(() => setMessage(''), 2000)
510
+ }
511
+
512
+ async function handleSettingsEditorSubmit(field: SettingsFieldId) {
513
+ if (!config) {
514
+ return
515
+ }
516
+
517
+ try {
518
+ const rawText = settingEditorTextareaRef.current?.plainText ?? settingEditorValue
519
+ const nextConfig = applyEditorSetting(config, field, rawText, process.env.HOME || '~')
520
+
521
+ setSettingEditorError('')
522
+ await applyAndPersistConfig(nextConfig, `${getSettingEditorTitle(field)} updated`)
523
+ openSettingsModal()
524
+ } catch (error) {
525
+ const errorMessage = error instanceof Error ? error.message : 'Failed to save settings'
526
+ setSettingEditorError(errorMessage)
527
+ }
528
+ }
529
+
530
+ async function handleSettingOptionSubmit(field: SettingsFieldId, value: string) {
531
+ if (!config) {
532
+ return
533
+ }
534
+
535
+ const nextConfig = applyOptionSetting(config, field, value)
536
+ await applyAndPersistConfig(nextConfig, `${getSettingEditorTitle(field)} updated`)
537
+ }
538
+
539
+ const keyboardContext: KeyboardHandlerContext = {
208
540
  appMode,
209
541
  viewMode,
210
542
  items,
@@ -214,6 +546,7 @@ export function App() {
214
546
  opencodeCursor,
215
547
  searchQuery,
216
548
  renameTarget,
549
+ prefixKey: config?.prefixKey,
217
550
  projectItems,
218
551
  sessionItems,
219
552
  prefixActive,
@@ -229,30 +562,114 @@ export function App() {
229
562
  setItems,
230
563
  setPrefixActive,
231
564
  refreshItems,
232
- handleSelect,
565
+ requestKillSession,
566
+ clearPendingKill,
567
+ handleSelect: handleSelectWrapper,
233
568
  handleKillSession: handleKillSessionWrapper,
569
+ handleLastSession: handleLastSessionWrapper,
570
+ handleRootSession: handleRootSessionWrapper,
571
+ handleEditTarget: handleEditTargetWrapper,
572
+ openRenameModal,
573
+ openCommandsModal,
574
+ openSettingsModal,
234
575
  loadOpencodeStatsForSession,
235
576
  setMessage,
236
577
  }
237
578
 
238
579
  useKeyboard(key => {
239
580
  const keybindMode = config?.keybindMode || 'vim'
581
+
582
+ if (modalState?.type === 'rename') {
583
+ if (key.name === 'return') {
584
+ void handleRenameSubmit()
585
+ } else if (key.name === 'escape') {
586
+ closeModal()
587
+ }
588
+ return
589
+ }
590
+
591
+ if (modalState?.type === 'commands') {
592
+ if (key.name === 'escape' || key.name === 'q') {
593
+ closeModal()
594
+ } else if (key.name === 'down' || key.name === 'j') {
595
+ setCommandsCursor(current => Math.min(current + 1, filteredCommandEntries.length - 1))
596
+ } else if (key.name === 'up' || key.name === 'k') {
597
+ setCommandsCursor(current => Math.max(current - 1, 0))
598
+ } else if (key.name === 'return') {
599
+ const command = filteredCommandEntries[commandsCursor]
600
+ if (!command) {
601
+ return
602
+ }
603
+ void executeCommand(command.id)
604
+ }
605
+ return
606
+ }
607
+
608
+ if (modalState?.type === 'settings') {
609
+ if (key.name === 'escape') {
610
+ closeModal()
611
+ } else if (key.name === 'down' || key.name === 'j') {
612
+ setSettingsCursor(current => Math.min(current + 1, filteredSettingsEntries.length - 1))
613
+ } else if (key.name === 'up' || key.name === 'k') {
614
+ setSettingsCursor(current => Math.max(current - 1, 0))
615
+ } else if (key.name === 'return') {
616
+ const entry = filteredSettingsEntries[settingsCursor]
617
+ if (!entry) {
618
+ return
619
+ }
620
+
621
+ if (isOptionSetting(entry.id)) {
622
+ openSettingOptions(entry.id)
623
+ } else {
624
+ openSettingEditor(entry.id)
625
+ }
626
+ } else if (key.name === 'e') {
627
+ const entry = filteredSettingsEntries[settingsCursor]
628
+ if (!entry) return
629
+ if (isOptionSetting(entry.id)) {
630
+ openSettingOptions(entry.id)
631
+ } else {
632
+ openSettingEditor(entry.id)
633
+ }
634
+ }
635
+ return
636
+ }
637
+
638
+ if (modalState?.type === 'setting-options') {
639
+ if (key.name === 'escape') {
640
+ setModalState({ type: 'settings' })
641
+ } else if (key.name === 'down' || key.name === 'j') {
642
+ setSettingOptionsCursor(current => Math.min(current + 1, filteredSettingOptions.length - 1))
643
+ } else if (key.name === 'up' || key.name === 'k') {
644
+ setSettingOptionsCursor(current => Math.max(current - 1, 0))
645
+ } else if (key.name === 'return') {
646
+ const option = filteredSettingOptions[settingOptionsCursor]
647
+ if (!option) return
648
+ void handleSettingOptionSubmit(modalState.field, option.value)
649
+ }
650
+ return
651
+ }
652
+
653
+ if (modalState?.type === 'setting-editor') {
654
+ if (key.name === 'escape') {
655
+ setModalState({ type: 'settings' })
656
+ setSettingEditorError('')
657
+ } else if (key.name === 'return') {
658
+ void handleSettingsEditorSubmit(modalState.field)
659
+ }
660
+ return
661
+ }
662
+
240
663
  if (appMode === AppMode.Normal) {
241
664
  handleNormalMode(key, keyboardContext, keybindMode)
242
665
  } else if (appMode === AppMode.Search) {
243
666
  handleSearchMode(key, keyboardContext, keybindMode)
244
667
  } else if (appMode === AppMode.NewSession) {
245
668
  if (key.name === 'return') {
246
- handleNewSessionSubmit()
669
+ void handleNewSessionSubmit()
247
670
  } else {
248
671
  handleNewSessionMode(key, keyboardContext, keybindMode)
249
672
  }
250
- } else if (appMode === AppMode.Rename) {
251
- if (key.name === 'return') {
252
- handleRenameSubmit()
253
- } else {
254
- handleRenameMode(key, keyboardContext, keybindMode)
255
- }
256
673
  } else if (appMode === AppMode.OpencodeManage) {
257
674
  handleOpencodeManageMode(key, keyboardContext, keybindMode)
258
675
  }
@@ -260,7 +677,7 @@ export function App() {
260
677
 
261
678
  useEffect(() => {
262
679
  const unsubscribe = updateEvents.on(event => {
263
- setToastMessage(`v${event.version}`)
680
+ setToastMessage(`Update ready ${event.version}`)
264
681
  setToastVisible(true)
265
682
  setTimeout(() => {
266
683
  setToastVisible(false)
@@ -278,12 +695,86 @@ export function App() {
278
695
  useEffect(() => {
279
696
  if (appMode === AppMode.Normal && viewMode === ViewMode.Sessions) {
280
697
  // Clear match indices when returning to normal mode
281
- clearMatchIndices(sessionItems)
282
- setAllItems(sessionItems)
283
- setItems(sessionItems)
698
+ const cleanSessions = clearMatchIndices(sessionItems)
699
+ setAllItems(cleanSessions)
700
+ setItems(cleanSessions)
701
+ setCursor(getSessionSelectionIndex(cleanSessions))
284
702
  }
285
703
  }, [appMode, viewMode, sessionItems])
286
704
 
705
+ useEffect(() => {
706
+ if (appMode === AppMode.NewSession && viewMode === ViewMode.Projects) {
707
+ setCursor(getProjectSelectionIndex(projectItems))
708
+ }
709
+ }, [appMode, viewMode, projectItems])
710
+
711
+ useEffect(() => {
712
+ if (filteredCommandEntries.length === 0) {
713
+ setCommandsCursor(0)
714
+ return
715
+ }
716
+
717
+ setCommandsCursor(current => Math.min(current, filteredCommandEntries.length - 1))
718
+ }, [filteredCommandEntries.length])
719
+
720
+ useEffect(() => {
721
+ setCommandsCursor(0)
722
+ }, [commandsSearchQuery])
723
+
724
+ useEffect(() => {
725
+ if (filteredSettingsEntries.length === 0) {
726
+ setSettingsCursor(0)
727
+ return
728
+ }
729
+
730
+ setSettingsCursor(current => Math.min(current, filteredSettingsEntries.length - 1))
731
+ }, [filteredSettingsEntries.length])
732
+
733
+ useEffect(() => {
734
+ if (filteredSettingOptions.length === 0) {
735
+ setSettingOptionsCursor(0)
736
+ return
737
+ }
738
+
739
+ setSettingOptionsCursor(current => Math.min(current, filteredSettingOptions.length - 1))
740
+ }, [filteredSettingOptions.length])
741
+
742
+ useEffect(() => {
743
+ const selectedItem = appMode === AppMode.NewSession ? items[cursor] : selectedPrimaryItem
744
+
745
+ if (!selectedItem) {
746
+ return
747
+ }
748
+
749
+ if (appMode === AppMode.NewSession || viewMode === ViewMode.Projects) {
750
+ if (!selectedItem.isSession) {
751
+ lastProjectSelectionRef.current = selectedItem.path
752
+ }
753
+ return
754
+ }
755
+
756
+ if (viewMode === ViewMode.Sessions && appMode === AppMode.Normal) {
757
+ lastSessionSelectionRef.current = selectedItem.title
758
+ }
759
+ }, [appMode, viewMode, cursor, items, selectedPrimaryItem])
760
+
761
+ useEffect(() => {
762
+ if (!pendingKillSessionName) {
763
+ return
764
+ }
765
+
766
+ const selectedSessionName =
767
+ appMode === AppMode.OpencodeManage
768
+ ? opencodeSessions[opencodeCursor]?.title
769
+ : viewMode === ViewMode.Sessions
770
+ ? regularSessions[cursor]?.title
771
+ : undefined
772
+
773
+ if (selectedSessionName !== pendingKillSessionName) {
774
+ setPendingKillSessionName(null)
775
+ }
776
+ }, [appMode, cursor, opencodeCursor, opencodeSessions, pendingKillSessionName, regularSessions, viewMode])
777
+
287
778
  useEffect(() => {
288
779
  if (appMode === AppMode.Search || appMode === AppMode.NewSession) {
289
780
  if (searchQuery.trim()) {
@@ -315,162 +806,244 @@ export function App() {
315
806
 
316
807
  const title =
317
808
  appMode === AppMode.Search
318
- ? 'Search Sessions'
809
+ ? 'Search'
319
810
  : appMode === AppMode.NewSession
320
- ? '󰏌 New Session'
321
- : appMode === AppMode.Rename
322
- ? '󰏫 Rename Session'
323
- : appMode === AppMode.OpencodeManage
324
- ? ' Opencode Sessions'
325
- : ' Tmux Session Manager'
811
+ ? 'New Session'
812
+ : appMode === AppMode.OpencodeManage
813
+ ? 'OpenCode Sessions'
814
+ : viewMode === ViewMode.Projects
815
+ ? 'Projects'
816
+ : 'Sessions'
326
817
 
327
818
  const listStyle =
328
819
  appMode === AppMode.NewSession
329
820
  ? {
330
- ...sessionListStyleFull,
821
+ ...getPanelStyle(theme, 'full'),
331
822
  flexGrow: 1,
332
823
  flexShrink: 1,
333
824
  }
334
825
  : {
335
- ...sessionListStyle,
826
+ ...getPanelStyle(theme, 'split'),
336
827
  flexGrow: 1,
337
828
  flexShrink: 1,
338
829
  minWidth: 40,
339
830
  }
340
831
 
341
- const totalSessions =
342
- viewMode === ViewMode.Sessions && appMode === AppMode.Normal ? items.length : 0
832
+ const totalSessions = sessionItems.filter(item => item.isSession).length
343
833
  const activeSessions = items.filter(item => item.isSession && item.isAttached).length
344
- const idleSessions = totalSessions - activeSessions
834
+ const maxVisibleItems = Math.max(8, rows - (appMode === AppMode.NewSession ? 10 : 12))
835
+ const prefixLabel = config?.prefixKey ? `${config.prefixKey} ...` : 'direct keys'
836
+ const footerHint =
837
+ appMode === AppMode.OpencodeManage
838
+ ? `o back d kill ctrl+p commands ${prefixLabel}`
839
+ : appMode === AppMode.NewSession
840
+ ? 'enter create esc cancel'
841
+ : appMode === AppMode.Search
842
+ ? 'enter select esc cancel'
843
+ : `enter select i search n new o opencode d kill ctrl+p commands ${prefixLabel}`
345
844
 
346
- const sessionsWithAges = items
347
- .filter(item => item.isSession && item.createdAt)
348
- .sort((a, b) => (a.createdAt || 0) - (b.createdAt || 0))
845
+ return (
846
+ <ThemeProvider theme={theme}>
847
+ <box
848
+ style={{
849
+ flexDirection: 'row',
850
+ alignItems: 'stretch',
851
+ justifyContent: 'center',
852
+ width: '100%',
853
+ height: '100%',
854
+ gap: 1,
855
+ backgroundColor: theme.background,
856
+ }}
857
+ >
858
+ <box style={listStyle}>
859
+ <box style={{ justifyContent: 'space-between', marginBottom: 1 }}>
860
+ <box style={{ flexDirection: 'column' }}>
861
+ <text style={{ fg: theme.text }}>mux-sesh</text>
862
+ <text style={{ fg: theme.textSubtle }}>{title}</text>
863
+ </box>
864
+ <box style={{ flexDirection: 'column', alignItems: 'flex-end' }}>
865
+ <text style={{ fg: theme.textMuted }}>
866
+ {viewMode === ViewMode.Sessions ? `${activeSessions}/${totalSessions} active` : `${projectItems.length} projects`}
867
+ </text>
868
+ <text style={{ fg: theme.textSubtle }}>{`${resolvedTheme.name} · ${resolvedTheme.mode}`}</text>
869
+ </box>
870
+ </box>
349
871
 
350
- const oldestSession =
351
- sessionsWithAges.length > 0 ? formatSessionAge(sessionsWithAges[0].createdAt!) : 'N/A'
352
- const newestSession =
353
- sessionsWithAges.length > 0
354
- ? formatSessionAge(sessionsWithAges[sessionsWithAges.length - 1].createdAt!)
355
- : 'N/A'
872
+ {(appMode === AppMode.Search || appMode === AppMode.NewSession) && (
873
+ <SearchInput
874
+ key={appMode}
875
+ appMode={appMode}
876
+ searchQuery={searchQuery}
877
+ textareaRef={textareaRef}
878
+ prefixActive={prefixActive}
879
+ onContentChange={() => {
880
+ if (textareaRef.current && textareaRef.current.plainText !== undefined) {
881
+ setSearchQuery(textareaRef.current.plainText)
882
+ }
883
+ }}
884
+ />
885
+ )}
356
886
 
357
- return (
358
- <box
359
- style={{
360
- flexDirection: 'row',
361
- alignItems: 'stretch',
362
- justifyContent: 'center',
363
- width: '100%',
364
- height: '100%',
365
- gap: 1,
366
- }}
367
- >
368
- {/* Left panel - Session/Project list */}
369
- <box style={listStyle}>
370
- <box style={{ alignItems: 'center', justifyContent: 'center' }}>
371
- <text style={{ fg: colors.primary, marginBottom: 1, marginTop: 1 }}>{title}</text>
887
+ <box
888
+ style={{
889
+ alignSelf: 'auto',
890
+ flexDirection: 'column',
891
+ flexGrow: 0,
892
+ flexShrink: 0,
893
+ marginTop:
894
+ viewMode === ViewMode.Sessions && (appMode === AppMode.Normal || appMode === AppMode.OpencodeManage)
895
+ ? 1
896
+ : 0,
897
+ }}
898
+ >
899
+ {items.length === 0 ? (
900
+ <text style={{ fg: theme.inactive }}>
901
+ {appMode === AppMode.NewSession && searchQuery && isGitHubURL(searchQuery)
902
+ ? 'Clone & create session'
903
+ : appMode === AppMode.NewSession && searchQuery
904
+ ? `Create session: ${searchQuery}`
905
+ : 'No items found'}
906
+ </text>
907
+ ) : viewMode === ViewMode.Sessions && (appMode === AppMode.Normal || appMode === AppMode.OpencodeManage) ? (
908
+ <>
909
+ <SessionList
910
+ items={regularSessions}
911
+ cursor={cursor}
912
+ searchQuery={searchQuery}
913
+ maxItems={maxVisibleItems}
914
+ icons={config?.icons}
915
+ pendingKillSessionName={pendingKillSessionName}
916
+ />
917
+
918
+ <OpencodeSessionGroup
919
+ sessions={opencodeSessions}
920
+ appMode={appMode}
921
+ cursor={opencodeCursor}
922
+ icons={config?.icons}
923
+ pendingKillSessionName={pendingKillSessionName}
924
+ />
925
+ </>
926
+ ) : (
927
+ <ItemList
928
+ items={items}
929
+ cursor={cursor}
930
+ appMode={appMode}
931
+ searchQuery={searchQuery}
932
+ maxItems={maxVisibleItems}
933
+ icons={config?.icons}
934
+ pendingKillSessionName={pendingKillSessionName}
935
+ />
936
+ )}
937
+ </box>
938
+
939
+ {(message || columns < 80 || footerHint) && (
940
+ <box style={{ flexDirection: 'column', marginTop: 1 }}>
941
+ {message && <text style={{ fg: theme.action }}>{message}</text>}
942
+ {columns < 80 && appMode !== AppMode.NewSession && (
943
+ <text style={{ fg: theme.textSubtle }}>Resize for the detail pane.</text>
944
+ )}
945
+ <text style={{ fg: theme.textSubtle }}>{footerHint}</text>
946
+ </box>
947
+ )}
372
948
  </box>
373
949
 
374
- {/* Search input */}
375
- {(appMode === AppMode.Search ||
376
- appMode === AppMode.NewSession ||
377
- appMode === AppMode.Rename) && (
378
- <SearchInput
379
- key={appMode}
380
- appMode={appMode}
381
- searchQuery={searchQuery}
382
- textareaRef={textareaRef}
383
- prefixActive={prefixActive}
950
+ {shouldShowDetailPanel(columns, appMode === AppMode.NewSession) && (
951
+ <>
952
+ {viewMode === ViewMode.Sessions && appMode === AppMode.OpencodeManage ? (
953
+ <OpencodeStatsPanel selectedItem={opencodeSessions[opencodeCursor]} />
954
+ ) : (
955
+ <SessionDetailsPanel selectedItem={selectedPrimaryItem} config={config} />
956
+ )}
957
+ </>
958
+ )}
959
+
960
+ {modalState?.type === 'rename' && (
961
+ <RenameModal
962
+ target={modalState.target}
963
+ initialValue={modalInputValue}
964
+ columns={columns}
965
+ textareaRef={modalTextareaRef}
384
966
  onContentChange={() => {
385
- if (textareaRef.current && textareaRef.current.plainText !== undefined) {
386
- setSearchQuery(textareaRef.current.plainText)
967
+ if (modalTextareaRef.current?.plainText !== undefined) {
968
+ setModalInputValue(modalTextareaRef.current.plainText)
387
969
  }
388
970
  }}
389
971
  />
390
972
  )}
391
973
 
392
- {/* Items list */}
393
- <box
394
- style={{
395
- alignSelf: 'auto',
396
- flexDirection: 'column',
397
- flexGrow: 0,
398
- flexShrink: 0,
399
- marginTop:
400
- viewMode === ViewMode.Sessions &&
401
- (appMode === AppMode.Normal || appMode === AppMode.OpencodeManage)
402
- ? 2
403
- : 0,
404
- }}
405
- >
406
- {items.length === 0 ? (
407
- <text style={{ fg: colors.inactive }}>
408
- {appMode === AppMode.NewSession && searchQuery && isGitHubURL(searchQuery)
409
- ? `Clone & create session`
410
- : appMode === AppMode.NewSession && searchQuery
411
- ? `Create session: ${searchQuery}`
412
- : 'No items found'}
413
- </text>
414
- ) : viewMode === ViewMode.Sessions &&
415
- (appMode === AppMode.Normal || appMode === AppMode.OpencodeManage) ? (
416
- <>
417
- {/* Regular sessions */}
418
- <SessionList items={regularSessions} cursor={cursor} searchQuery={searchQuery} />
419
-
420
- {/* Opencode sessions group */}
421
- <OpencodeSessionGroup
422
- sessions={opencodeSessions}
423
- appMode={appMode}
424
- cursor={opencodeCursor}
425
- />
974
+ {modalState?.type === 'commands' && (
975
+ <CommandsModal
976
+ columns={columns}
977
+ themeId={resolvedTheme.id}
978
+ entries={filteredCommandEntries}
979
+ cursor={commandsCursor}
980
+ searchQuery={commandsSearchQuery}
981
+ textareaRef={commandsSearchTextareaRef}
982
+ onContentChange={() => {
983
+ if (commandsSearchTextareaRef.current?.plainText !== undefined) {
984
+ setCommandsSearchQuery(commandsSearchTextareaRef.current.plainText)
985
+ setCommandsCursor(0)
986
+ }
987
+ }}
988
+ />
989
+ )}
426
990
 
427
- {/* Session statistics */}
428
- <SessionStats
429
- totalSessions={totalSessions}
430
- activeSessions={activeSessions}
431
- idleSessions={idleSessions}
432
- oldestSession={oldestSession}
433
- newestSession={newestSession}
434
- />
435
- </>
436
- ) : (
437
- <ItemList items={items} cursor={cursor} appMode={appMode} searchQuery={searchQuery} />
438
- )}
439
- </box>
991
+ {modalState?.type === 'settings' && config && (
992
+ <SettingsModal
993
+ configPath={configPath}
994
+ themeName={resolvedTheme.name}
995
+ colorMode={resolvedTheme.mode}
996
+ columns={columns}
997
+ entries={filteredSettingsEntries}
998
+ cursor={settingsCursor}
999
+ searchQuery={settingsSearchQuery}
1000
+ textareaRef={settingsSearchTextareaRef}
1001
+ onContentChange={() => {
1002
+ if (settingsSearchTextareaRef.current?.plainText !== undefined) {
1003
+ setSettingsSearchQuery(settingsSearchTextareaRef.current.plainText)
1004
+ setSettingsCursor(0)
1005
+ }
1006
+ }}
1007
+ />
1008
+ )}
440
1009
 
441
- {/* Message */}
442
- {message && <text style={{ fg: colors.action, marginTop: 1 }}>{message}</text>}
1010
+ {modalState?.type === 'setting-options' && (
1011
+ <SettingOptionsModal
1012
+ title={getSettingEditorTitle(modalState.field)}
1013
+ description='Select an option to apply it immediately'
1014
+ columns={columns}
1015
+ options={filteredSettingOptions}
1016
+ cursor={settingOptionsCursor}
1017
+ searchQuery={settingOptionsSearchQuery}
1018
+ textareaRef={settingOptionsSearchTextareaRef}
1019
+ onContentChange={() => {
1020
+ if (settingOptionsSearchTextareaRef.current?.plainText !== undefined) {
1021
+ setSettingOptionsSearchQuery(settingOptionsSearchTextareaRef.current.plainText)
1022
+ setSettingOptionsCursor(0)
1023
+ }
1024
+ }}
1025
+ />
1026
+ )}
443
1027
 
444
- {/* Terminal size warning */}
445
- {columns < 80 && appMode !== AppMode.NewSession && (
446
- <text style={{ fg: colors.inactive, marginTop: 1 }}>
447
- Terminal too small. Resize for full view.
448
- </text>
1028
+ {modalState?.type === 'setting-editor' && (
1029
+ <SettingEditorModal
1030
+ title={getSettingEditorTitle(modalState.field)}
1031
+ description='Edit value and press Enter to apply immediately'
1032
+ value={settingEditorValue}
1033
+ error={settingEditorError}
1034
+ columns={columns}
1035
+ rows={rows}
1036
+ textareaRef={settingEditorTextareaRef}
1037
+ onContentChange={() => {
1038
+ if (settingEditorTextareaRef.current?.plainText !== undefined) {
1039
+ setSettingEditorValue(settingEditorTextareaRef.current.plainText)
1040
+ }
1041
+ }}
1042
+ />
449
1043
  )}
450
1044
 
451
- {/* Keybind help */}
452
- <box style={{ marginTop: 1, flexDirection: 'column' }}>
453
- <KeybindHelp appMode={appMode} keybindMode={config?.keybindMode} />
454
- </box>
1045
+ <Toast message={toastMessage} visible={toastVisible} />
455
1046
  </box>
456
-
457
- {/* Right panel - Detail panel (only in non-new-session modes) */}
458
- {shouldShowDetailPanel(columns, appMode === AppMode.NewSession) && (
459
- <>
460
- {viewMode === ViewMode.Sessions && appMode === AppMode.OpencodeManage ? (
461
- <OpencodeStatsPanel selectedItem={opencodeSessions[opencodeCursor]} />
462
- ) : (
463
- <SessionDetailsPanel
464
- selectedItem={
465
- viewMode === ViewMode.Sessions && appMode === AppMode.Normal
466
- ? regularSessions[cursor]
467
- : items[cursor]
468
- }
469
- />
470
- )}
471
- </>
472
- )}
473
- <Toast message={toastMessage} visible={toastVisible} />
474
- </box>
1047
+ </ThemeProvider>
475
1048
  )
476
1049
  }