mux-sesh 1.6.4 → 1.7.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 (78) hide show
  1. package/README.md +90 -59
  2. package/package.json +7 -5
  3. package/src/app/commands.ts +120 -0
  4. package/src/app/controller.ts +397 -0
  5. package/src/app/controls.ts +121 -0
  6. package/src/app/core-state.ts +71 -0
  7. package/src/app/data.ts +90 -0
  8. package/src/app/derived.ts +77 -0
  9. package/src/app/effects.ts +392 -0
  10. package/src/app/handlers.ts +325 -0
  11. package/src/app/keyboard.ts +161 -0
  12. package/src/app/modal-keyboard.ts +118 -0
  13. package/src/app/modal-state.ts +50 -0
  14. package/src/app/modals-layer.tsx +159 -0
  15. package/src/app/modals.ts +108 -0
  16. package/src/app/notifications.ts +21 -0
  17. package/src/app/opencode.ts +49 -0
  18. package/src/app/operations.ts +31 -0
  19. package/src/app/runtime.ts +76 -0
  20. package/src/app/screen.tsx +217 -0
  21. package/src/app/state.ts +166 -0
  22. package/src/app/textarea.ts +16 -0
  23. package/src/app/view.ts +89 -0
  24. package/src/app.tsx +7 -1136
  25. package/src/config/index.ts +36 -42
  26. package/src/config/paths.ts +13 -0
  27. package/src/config/session-rules.ts +6 -2
  28. package/src/config/store.ts +26 -0
  29. package/src/handlers/action-helpers.ts +50 -0
  30. package/src/handlers/actions.ts +34 -65
  31. package/src/handlers/keyboard/index.ts +5 -0
  32. package/src/handlers/keyboard/new-session.ts +57 -0
  33. package/src/handlers/keyboard/normal.ts +201 -0
  34. package/src/handlers/keyboard/opencode.ts +104 -0
  35. package/src/handlers/keyboard/search.ts +113 -0
  36. package/src/handlers/keyboard/shared.ts +116 -0
  37. package/src/opencode/sqlite.ts +21 -6
  38. package/src/preview/project.ts +41 -15
  39. package/src/search/index.ts +4 -1
  40. package/src/settings.ts +294 -297
  41. package/src/styles/opencode-themes-a.ts +1442 -1
  42. package/src/styles/opencode-themes-b.ts +1212 -1
  43. package/src/styles/opencode-tui-colors.ts +1784 -1
  44. package/src/styles/theme.tsx +2 -1
  45. package/src/tmux/actions.ts +103 -0
  46. package/src/tmux/details.ts +123 -0
  47. package/src/tmux/helpers.ts +35 -0
  48. package/src/tmux/index.ts +10 -288
  49. package/src/tmux/list.ts +31 -0
  50. package/src/tmux/project-discovery.ts +113 -0
  51. package/src/tmux/project-loader.ts +74 -0
  52. package/src/tmux/project-zoxide.ts +128 -0
  53. package/src/tmux/projects.ts +23 -262
  54. package/src/types/index.ts +0 -1
  55. package/src/ui/CommandsModal.tsx +87 -22
  56. package/src/ui/HighlightedText.tsx +1 -5
  57. package/src/ui/ItemList.tsx +14 -4
  58. package/src/ui/Modal.tsx +8 -1
  59. package/src/ui/OpencodeSessionGroup.tsx +20 -9
  60. package/src/ui/OpencodeStatsPanel.tsx +48 -15
  61. package/src/ui/RenameModal.tsx +17 -17
  62. package/src/ui/SearchInput.tsx +1 -20
  63. package/src/ui/SessionDetailsPanel.tsx +43 -15
  64. package/src/ui/SessionList.tsx +23 -6
  65. package/src/ui/SettingEditorModal.tsx +15 -9
  66. package/src/ui/SettingOptionsModal.tsx +30 -12
  67. package/src/ui/SettingsModal.tsx +20 -14
  68. package/src/ui/VersionBadge.tsx +3 -1
  69. package/src/ui/item-icon.ts +5 -1
  70. package/src/ui/list-window.ts +5 -1
  71. package/src/ui/use-textarea-focus.ts +26 -0
  72. package/src/update/index.ts +10 -2
  73. package/src/update/installation.ts +6 -2
  74. package/src/util/ansi.ts +3 -0
  75. package/src/util/glob.ts +5 -1
  76. package/src/components/KeybindHelp.tsx +0 -289
  77. package/src/handlers/keyboard.ts +0 -654
  78. package/src/ui/SessionStats.tsx +0 -32
@@ -1,5 +1,4 @@
1
1
  import { join } from 'path'
2
- import { mkdir } from 'fs/promises'
3
2
  import type {
4
3
  Config,
5
4
  IconConfig,
@@ -46,7 +45,9 @@ function asStringArray(value: unknown): string[] | undefined {
46
45
  return undefined
47
46
  }
48
47
 
49
- const strings = value.filter((entry): entry is string => typeof entry === 'string' && entry.length > 0)
48
+ const strings = value.filter(
49
+ (entry): entry is string => typeof entry === 'string' && entry.length > 0
50
+ )
50
51
  return strings.length > 0 ? strings : undefined
51
52
  }
52
53
 
@@ -59,7 +60,10 @@ function asZoxideMode(value: unknown): ZoxideMode | undefined {
59
60
  }
60
61
 
61
62
  function asSortOrder(value: unknown): SortOrder | undefined {
62
- return value === 'live-first' || value === 'configured-first' || value === 'zoxide-first' || value === 'alphabetical'
63
+ return value === 'live-first' ||
64
+ value === 'configured-first' ||
65
+ value === 'zoxide-first' ||
66
+ value === 'alphabetical'
63
67
  ? value
64
68
  : undefined
65
69
  }
@@ -277,21 +281,30 @@ export function normalizeConfig(rawConfig: unknown, homeDir = process.env.HOME |
277
281
  ? 'opencode'
278
282
  : requestedTheme
279
283
  const implicitColorScheme =
280
- requestedTheme === 'opencode-light' ? 'light' : requestedTheme === 'opencode-dark' ? 'dark' : undefined
281
-
282
- const projectPaths = (asStringArray(raw.project_paths) ?? asStringArray(raw.projectPaths) ?? defaultConfig.projectPaths).map(
283
- projectPath => expandHomePath(projectPath, homeDir)
284
- )
284
+ requestedTheme === 'opencode-light'
285
+ ? 'light'
286
+ : requestedTheme === 'opencode-dark'
287
+ ? 'dark'
288
+ : undefined
289
+
290
+ const projectPaths = (
291
+ asStringArray(raw.project_paths) ??
292
+ asStringArray(raw.projectPaths) ??
293
+ defaultConfig.projectPaths
294
+ ).map(projectPath => expandHomePath(projectPath, homeDir))
285
295
  const reposPath = expandHomePath(
286
296
  asString(raw.repos_path) ?? asString(raw.reposPath) ?? defaultConfig.reposPath,
287
297
  homeDir
288
298
  )
289
299
  const editor = asString(raw.editor) ?? defaultConfig.editor
290
300
  const editorCmd = asString(raw.editor_cmd) ?? asString(raw.editorCmd) ?? defaultConfig.editorCmd
291
- const keybindMode = asKeybindMode(raw.keybind_mode) ?? asKeybindMode(raw.keybindMode) ?? defaultConfig.keybindMode
301
+ const keybindMode =
302
+ asKeybindMode(raw.keybind_mode) ?? asKeybindMode(raw.keybindMode) ?? defaultConfig.keybindMode
292
303
  const prefixKey = asString(raw.prefix_key) ?? asString(raw.prefixKey) ?? defaultConfig.prefixKey
293
- const zoxideMode = asZoxideMode(raw.zoxide_mode) ?? asZoxideMode(raw.zoxideMode) ?? defaultConfig.zoxideMode
294
- const sortOrder = asSortOrder(raw.sort_order) ?? asSortOrder(raw.sortOrder) ?? defaultConfig.sortOrder
304
+ const zoxideMode =
305
+ asZoxideMode(raw.zoxide_mode) ?? asZoxideMode(raw.zoxideMode) ?? defaultConfig.zoxideMode
306
+ const sortOrder =
307
+ asSortOrder(raw.sort_order) ?? asSortOrder(raw.sortOrder) ?? defaultConfig.sortOrder
295
308
  const theme = normalizedTheme ?? defaultConfig.theme
296
309
  const colorScheme =
297
310
  asThemeColorScheme(raw.color_scheme) ??
@@ -299,11 +312,17 @@ export function normalizeConfig(rawConfig: unknown, homeDir = process.env.HOME |
299
312
  implicitColorScheme ??
300
313
  defaultConfig.colorScheme
301
314
  const themes = normalizeThemes(raw.themes) ?? defaultConfig.themes
302
- const autoUpdate = asBoolean(raw.auto_update) ?? asBoolean(raw.autoUpdate) ?? defaultConfig.autoUpdate
303
- const dirLength = asPositiveInteger(raw.dir_length) ?? asPositiveInteger(raw.dirLength) ?? defaultConfig.dirLength
304
- const hiddenSessions = asStringArray(raw.hidden_sessions) ?? asStringArray(raw.hiddenSessions) ?? defaultConfig.hiddenSessions
315
+ const autoUpdate =
316
+ asBoolean(raw.auto_update) ?? asBoolean(raw.autoUpdate) ?? defaultConfig.autoUpdate
317
+ const dirLength =
318
+ asPositiveInteger(raw.dir_length) ?? asPositiveInteger(raw.dirLength) ?? defaultConfig.dirLength
319
+ const hiddenSessions =
320
+ asStringArray(raw.hidden_sessions) ??
321
+ asStringArray(raw.hiddenSessions) ??
322
+ defaultConfig.hiddenSessions
305
323
  const icons = normalizeIcons(raw.icons) ?? defaultConfig.icons
306
- const configuredDefaultSession = normalizeSessionDefaults(raw.default_session) ?? normalizeSessionDefaults(raw.defaultSession)
324
+ const configuredDefaultSession =
325
+ normalizeSessionDefaults(raw.default_session) ?? normalizeSessionDefaults(raw.defaultSession)
307
326
  const defaultSession = {
308
327
  startupCommand: configuredDefaultSession?.startupCommand ?? editorCmd,
309
328
  previewCommand: configuredDefaultSession?.previewCommand,
@@ -418,30 +437,5 @@ export function serializeConfig(config: Config): Record<string, unknown> {
418
437
  }
419
438
  }
420
439
 
421
-
422
- export async function loadConfig(): Promise<Config> {
423
- const configDir = join(process.env.HOME!, '.config', 'mux-sesh')
424
- const configPath = join(configDir, 'config.json')
425
-
426
- try {
427
- const file = Bun.file(configPath)
428
- return normalizeConfig(await file.json(), process.env.HOME || '~')
429
- } catch {
430
- // Config doesn't exist, create default
431
- const defaultConfig = getDefaultConfig()
432
- await saveConfig(defaultConfig)
433
- return defaultConfig
434
- }
435
- }
436
-
437
- export async function saveConfig(config: Config): Promise<void> {
438
- const configDir = join(process.env.HOME!, '.config', 'mux-sesh')
439
- const configPath = join(configDir, 'config.json')
440
-
441
- await mkdir(configDir, { recursive: true })
442
- await Bun.write(configPath, JSON.stringify(serializeConfig(config), null, 2))
443
- }
444
-
445
- export function getConfigPath(): string {
446
- return join(process.env.HOME!, '.config', 'mux-sesh', 'config.json')
447
- }
440
+ export { getConfigDir, getConfigPath, getHomeDir } from './paths'
441
+ export { loadConfig, saveConfig } from './store'
@@ -0,0 +1,13 @@
1
+ import { join } from 'path'
2
+
3
+ export function getHomeDir(): string {
4
+ return process.env.HOME || '~'
5
+ }
6
+
7
+ export function getConfigDir(homeDir = getHomeDir()): string {
8
+ return process.env.XDG_CONFIG_HOME || join(homeDir, '.config')
9
+ }
10
+
11
+ export function getConfigPath(homeDir = getHomeDir()): string {
12
+ return join(getConfigDir(homeDir), 'mux-sesh', 'config.json')
13
+ }
@@ -70,7 +70,9 @@ export async function resolveProjectSession(
70
70
 
71
71
  if (projectProfile) {
72
72
  return {
73
- sessionName: sanitizeSessionName(projectProfile.sessionName ?? buildSessionName(projectPath, config.dirLength)),
73
+ sessionName: sanitizeSessionName(
74
+ projectProfile.sessionName ?? buildSessionName(projectPath, config.dirLength)
75
+ ),
74
76
  startupCommand: projectProfile.startupCommand ?? config.defaultSession?.startupCommand,
75
77
  previewCommand: projectProfile.previewCommand ?? config.defaultSession?.previewCommand,
76
78
  source: 'project',
@@ -80,7 +82,9 @@ export async function resolveProjectSession(
80
82
  const wildcard = findWildcard(normalizedProjectPath, config)
81
83
  if (wildcard) {
82
84
  return {
83
- sessionName: sanitizeSessionName(wildcard.sessionName ?? buildSessionName(projectPath, config.dirLength)),
85
+ sessionName: sanitizeSessionName(
86
+ wildcard.sessionName ?? buildSessionName(projectPath, config.dirLength)
87
+ ),
84
88
  startupCommand: wildcard.startupCommand ?? config.defaultSession?.startupCommand,
85
89
  previewCommand: wildcard.previewCommand ?? config.defaultSession?.previewCommand,
86
90
  source: 'wildcard',
@@ -0,0 +1,26 @@
1
+ import { mkdir } from 'fs/promises'
2
+ import { dirname } from 'path'
3
+ import type { Config } from '../types'
4
+ import { getConfigPath, getHomeDir } from './paths'
5
+ import { getDefaultConfig, normalizeConfig, serializeConfig } from './index'
6
+
7
+ export async function loadConfig(): Promise<Config> {
8
+ const homeDir = getHomeDir()
9
+ const configPath = getConfigPath(homeDir)
10
+
11
+ try {
12
+ const file = Bun.file(configPath)
13
+ return normalizeConfig(await file.json(), homeDir)
14
+ } catch {
15
+ const defaultConfig = getDefaultConfig(homeDir)
16
+ await saveConfig(defaultConfig, homeDir)
17
+ return defaultConfig
18
+ }
19
+ }
20
+
21
+ export async function saveConfig(config: Config, homeDir = getHomeDir()): Promise<void> {
22
+ const configPath = getConfigPath(homeDir)
23
+
24
+ await mkdir(dirname(configPath), { recursive: true })
25
+ await Bun.write(configPath, JSON.stringify(serializeConfig(config), null, 2))
26
+ }
@@ -0,0 +1,50 @@
1
+ import type { Config, Item } from '../types'
2
+ import { getGitRoot, resolveProjectSession } from '../config/session-rules'
3
+ import { createTmuxSession, getTmuxSessionDirectory } from '../tmux'
4
+ import { requestShutdown } from '../util/shutdown'
5
+
6
+ export function quoteShellArg(value: string): string {
7
+ return `'${value.replaceAll("'", "'\\''")}'`
8
+ }
9
+
10
+ export function buildEditorCommand(editor: string, path: string): string {
11
+ return `${editor} ${quoteShellArg(path)}`
12
+ }
13
+
14
+ export function requireConfig(config: Config | null): Config {
15
+ if (!config) {
16
+ throw new Error('Config is not loaded yet')
17
+ }
18
+
19
+ return config
20
+ }
21
+
22
+ export function requireConfiguredItem(item: Item | undefined): Item {
23
+ if (!item || item.itemKind !== 'configured') {
24
+ throw new Error('Select a configured session to edit its target')
25
+ }
26
+
27
+ return item
28
+ }
29
+
30
+ export function getNextSessionName(renameTarget: string, newName: string): string | null {
31
+ const trimmedName = newName.trim()
32
+ if (!trimmedName || trimmedName === renameTarget) {
33
+ return null
34
+ }
35
+
36
+ return trimmedName
37
+ }
38
+
39
+ export async function openProjectSession(projectPath: string, config: Config): Promise<void> {
40
+ const resolvedSession = await resolveProjectSession(projectPath, config)
41
+ await createTmuxSession(resolvedSession.sessionName, projectPath, {
42
+ startupCommand: resolvedSession.startupCommand,
43
+ })
44
+ await requestShutdown(0)
45
+ }
46
+
47
+ export async function getRootSessionPath(item: Item): Promise<string> {
48
+ const itemPath = item.isSession ? await getTmuxSessionDirectory(item.title) : item.path
49
+ return (await getGitRoot(itemPath)) ?? itemPath
50
+ }
@@ -2,37 +2,31 @@ import { spawn } from 'bun'
2
2
  import type { Item, Config } from '../types'
3
3
  import {
4
4
  switchTmuxSession,
5
- createTmuxSession,
6
5
  killTmuxSession,
7
6
  renameTmuxSession,
8
7
  createNamedTmuxSession,
9
8
  getCurrentTmuxSessionName,
10
- getTmuxSessionDirectory,
11
- } from '../tmux/index'
12
- import { getGitRoot, resolveProjectSession } from '../config/session-rules'
9
+ } from '../tmux'
13
10
  import { getLastSessionTarget } from '../tmux/workflows'
14
11
  import { isGitHubURL, cloneGitHubRepo } from '../util/github'
15
12
  import { requestShutdown } from '../util/shutdown'
16
-
17
- function quoteShellArg(value: string): string {
18
- return `'${value.replaceAll("'", "'\\''")}'`
19
- }
13
+ import {
14
+ buildEditorCommand,
15
+ getNextSessionName,
16
+ getRootSessionPath,
17
+ openProjectSession,
18
+ requireConfig,
19
+ requireConfiguredItem,
20
+ } from './action-helpers'
20
21
 
21
22
  export async function handleSelect(item: Item, config: Config | null) {
22
23
  if (item.isSession) {
23
24
  await switchTmuxSession(item.title)
24
25
  await requestShutdown(0)
25
- } else {
26
- if (!config) {
27
- throw new Error('Config is not loaded yet')
28
- }
29
-
30
- const resolvedSession = await resolveProjectSession(item.path, config)
31
- await createTmuxSession(resolvedSession.sessionName, item.path, {
32
- startupCommand: resolvedSession.startupCommand,
33
- })
34
- await requestShutdown(0)
26
+ return
35
27
  }
28
+
29
+ await openProjectSession(item.path, requireConfig(config))
36
30
  }
37
31
 
38
32
  export async function handleKillSession(
@@ -61,15 +55,17 @@ export async function handleRenameSubmit(
61
55
  refreshItems: () => Promise<void>
62
56
  }
63
57
  ) {
64
- const trimmedName = newName.trim()
65
- if (trimmedName && trimmedName !== renameTarget) {
66
- try {
67
- await renameTmuxSession(renameTarget, trimmedName)
68
- callbacks.onSuccess(`Session renamed to '${trimmedName}'`)
69
- await callbacks.refreshItems()
70
- } catch (error) {
71
- callbacks.onError(`Error renaming session: ${error}`)
72
- }
58
+ const nextSessionName = getNextSessionName(renameTarget, newName)
59
+ if (!nextSessionName) {
60
+ return
61
+ }
62
+
63
+ try {
64
+ await renameTmuxSession(renameTarget, nextSessionName)
65
+ callbacks.onSuccess(`Session renamed to '${nextSessionName}'`)
66
+ await callbacks.refreshItems()
67
+ } catch (error) {
68
+ callbacks.onError(`Error renaming session: ${error}`)
73
69
  }
74
70
  }
75
71
 
@@ -85,25 +81,13 @@ export async function handleNewSessionSubmit(
85
81
  try {
86
82
  if (!config) return
87
83
  const clonedPath = await cloneGitHubRepo(searchTerm, config)
88
- const resolvedSession = await resolveProjectSession(clonedPath, config)
89
- await createTmuxSession(resolvedSession.sessionName, clonedPath, {
90
- startupCommand: resolvedSession.startupCommand,
91
- })
92
- await requestShutdown(0)
84
+ await openProjectSession(clonedPath, config)
93
85
  } catch (error) {
94
86
  throw new Error(`Error cloning repository: ${error}`)
95
87
  }
96
88
  } else if (items.length > 0 && cursor < items.length) {
97
89
  const selectedItem = items[cursor]
98
- if (!config) {
99
- throw new Error('Config is not loaded yet')
100
- }
101
-
102
- const resolvedSession = await resolveProjectSession(selectedItem.path, config)
103
- await createTmuxSession(resolvedSession.sessionName, selectedItem.path, {
104
- startupCommand: resolvedSession.startupCommand,
105
- })
106
- await requestShutdown(0)
90
+ await openProjectSession(selectedItem.path, requireConfig(config))
107
91
  } else {
108
92
  await createNamedTmuxSession(searchTerm)
109
93
  await requestShutdown(0)
@@ -127,37 +111,22 @@ export async function handleRootSession(item: Item | undefined, config: Config |
127
111
  throw new Error('No session or project is selected')
128
112
  }
129
113
 
130
- if (!config) {
131
- throw new Error('Config is not loaded yet')
132
- }
133
-
134
- const itemPath = item.isSession ? await getTmuxSessionDirectory(item.title) : item.path
135
- const rootPath = (await getGitRoot(itemPath)) ?? itemPath
136
- const resolvedSession = await resolveProjectSession(rootPath, config)
137
-
138
- await createTmuxSession(resolvedSession.sessionName, rootPath, {
139
- startupCommand: resolvedSession.startupCommand,
140
- })
141
- await requestShutdown(0)
114
+ await openProjectSession(await getRootSessionPath(item), requireConfig(config))
142
115
  }
143
116
 
144
117
  export async function handleEditTarget(item: Item | undefined, config: Config | null) {
145
- if (!item || item.itemKind !== 'configured') {
146
- throw new Error('Select a configured session to edit its target')
147
- }
148
-
149
- if (!config) {
150
- throw new Error('Config is not loaded yet')
151
- }
152
-
153
- const command = `${config.editor} ${quoteShellArg(item.path)}`
118
+ const configuredItem = requireConfiguredItem(item)
119
+ const loadedConfig = requireConfig(config)
120
+ const command = buildEditorCommand(loadedConfig.editor, configuredItem.path)
154
121
 
155
122
  if (process.env.TMUX) {
156
- const proc = spawn(['tmux', 'new-window', '-c', item.path, command], { stderr: 'pipe' })
123
+ const proc = spawn(['tmux', 'new-window', '-c', configuredItem.path, command], {
124
+ stderr: 'pipe',
125
+ })
157
126
  await proc.exited
158
127
 
159
128
  if (proc.exitCode !== 0) {
160
- throw new Error(`Failed to open editor for '${item.title}'`)
129
+ throw new Error(`Failed to open editor for '${configuredItem.title}'`)
161
130
  }
162
131
 
163
132
  await requestShutdown(0)
@@ -165,7 +134,7 @@ export async function handleEditTarget(item: Item | undefined, config: Config |
165
134
  }
166
135
 
167
136
  spawn(['sh', '-lc', command], {
168
- cwd: item.path,
137
+ cwd: configuredItem.path,
169
138
  stdin: 'inherit',
170
139
  stdout: 'inherit',
171
140
  stderr: 'inherit',
@@ -0,0 +1,5 @@
1
+ export type { KeyboardHandlerContext, KeyboardInput } from './shared'
2
+ export { handleNormalMode } from './normal'
3
+ export { handleOpencodeManageMode } from './opencode'
4
+ export { handleSearchMode } from './search'
5
+ export { handleNewSessionMode } from './new-session'
@@ -0,0 +1,57 @@
1
+ import { AppMode, ViewMode, type KeybindMode } from '../../types'
2
+ import { getKeyName, type KeyboardHandlerContext, type KeyboardInput } from './shared'
3
+
4
+ export function handleNewSessionMode(
5
+ key: KeyboardInput,
6
+ ctx: KeyboardHandlerContext,
7
+ keybindMode: KeybindMode = 'vim'
8
+ ) {
9
+ const keyName = getKeyName(key)
10
+ const isStandard = keybindMode === 'standard'
11
+
12
+ if (isStandard && key.ctrl) {
13
+ if (keyName === 'i') {
14
+ ctx.clearPendingKill()
15
+ ctx.setAppMode(AppMode.Search)
16
+ ctx.setViewMode(ViewMode.Sessions)
17
+ ctx.setAllItems(ctx.sessionItems)
18
+ ctx.setItems(ctx.sessionItems)
19
+ ctx.setSearchQuery('')
20
+ return
21
+ }
22
+
23
+ if (keyName === 'd' && ctx.items[ctx.cursor]?.isSession) {
24
+ ctx.requestKillSession(ctx.items[ctx.cursor].title)
25
+ return
26
+ }
27
+ }
28
+
29
+ if (keyName === 'escape') {
30
+ ctx.clearPendingKill()
31
+ if (isStandard) {
32
+ ctx.setAppMode(AppMode.Search)
33
+ ctx.setViewMode(ViewMode.Sessions)
34
+ ctx.setSearchQuery('')
35
+ ctx.setCursor(0)
36
+ void ctx.refreshItems(ViewMode.Sessions)
37
+ return
38
+ }
39
+
40
+ ctx.setAppMode(AppMode.Normal)
41
+ ctx.setViewMode(ViewMode.Sessions)
42
+ ctx.setSearchQuery('')
43
+ ctx.setCursor(0)
44
+ return
45
+ }
46
+
47
+ switch (keyName) {
48
+ case 'down':
49
+ ctx.clearPendingKill()
50
+ ctx.setCursor(cursor => Math.min(cursor + 1, ctx.items.length - 1))
51
+ break
52
+ case 'up':
53
+ ctx.clearPendingKill()
54
+ ctx.setCursor(cursor => Math.max(cursor - 1, 0))
55
+ break
56
+ }
57
+ }
@@ -0,0 +1,201 @@
1
+ import { AppMode, ViewMode, type KeybindMode } from '../../types'
2
+ import { requestShutdown } from '../../util/shutdown'
3
+ import {
4
+ activatePrefix,
5
+ clearPrefix,
6
+ enterNewSessionMode,
7
+ getKeyName,
8
+ getQuickSelectItem,
9
+ getSelectedItem,
10
+ matchesPrefixKey,
11
+ openSelectedEditTarget,
12
+ openSelectedRootSession,
13
+ setTimedMessage,
14
+ type KeyboardHandlerContext,
15
+ type KeyboardInput,
16
+ } from './shared'
17
+
18
+ export function handleNormalMode(
19
+ key: KeyboardInput,
20
+ ctx: KeyboardHandlerContext,
21
+ keybindMode: KeybindMode = 'vim'
22
+ ) {
23
+ const keyName = getKeyName(key)
24
+ const isStandard = keybindMode === 'standard'
25
+
26
+ if (ctx.prefixActive) {
27
+ clearPrefix(ctx)
28
+
29
+ if (key.shift && keyName.toLowerCase() === 'r') {
30
+ ctx.clearPendingKill()
31
+ void ctx.refreshItems()
32
+ setTimedMessage(ctx, 'Refreshed')
33
+ return
34
+ }
35
+
36
+ switch (keyName) {
37
+ case 'l':
38
+ ctx.clearPendingKill()
39
+ void ctx.handleLastSession()
40
+ return
41
+ case 'g':
42
+ openSelectedRootSession(ctx)
43
+ return
44
+ case 'e':
45
+ openSelectedEditTarget(ctx)
46
+ return
47
+ case 'd':
48
+ if (ctx.viewMode === ViewMode.Sessions && ctx.regularSessions[ctx.cursor]?.isSession) {
49
+ ctx.requestKillSession(ctx.regularSessions[ctx.cursor].title)
50
+ }
51
+ return
52
+ case 'r':
53
+ if (ctx.viewMode === ViewMode.Sessions && ctx.regularSessions[ctx.cursor]?.isSession) {
54
+ ctx.clearPendingKill()
55
+ ctx.openRenameModal(ctx.regularSessions[ctx.cursor].title)
56
+ }
57
+ return
58
+ case 's':
59
+ ctx.clearPendingKill()
60
+ ctx.setViewMode(ViewMode.Sessions)
61
+ void ctx.refreshItems(ViewMode.Sessions)
62
+ return
63
+ case 'p':
64
+ ctx.clearPendingKill()
65
+ ctx.setViewMode(ViewMode.Projects)
66
+ void ctx.refreshItems(ViewMode.Projects)
67
+ return
68
+ }
69
+
70
+ return
71
+ }
72
+
73
+ if (ctx.prefixKey && matchesPrefixKey(key, ctx.prefixKey)) {
74
+ activatePrefix(ctx)
75
+ return
76
+ }
77
+
78
+ if (
79
+ (isStandard && ((key.ctrl && keyName === 'q') || keyName === 'escape')) ||
80
+ (!isStandard && (keyName === 'q' || keyName === 'escape'))
81
+ ) {
82
+ void requestShutdown(0)
83
+ return
84
+ }
85
+
86
+ if (!isStandard && keyName === 'i') {
87
+ ctx.clearPendingKill()
88
+ ctx.setAppMode(AppMode.Search)
89
+ ctx.setSearchQuery('')
90
+ return
91
+ }
92
+
93
+ if (!isStandard && keyName === 'n') {
94
+ enterNewSessionMode(ctx)
95
+ return
96
+ }
97
+
98
+ if (!isStandard && keyName === 'o') {
99
+ ctx.clearPendingKill()
100
+ if (ctx.viewMode === ViewMode.Sessions && ctx.opencodeSessions.length > 0) {
101
+ ctx.setAppMode(AppMode.OpencodeManage)
102
+ ctx.setOpencodeCursor(0)
103
+ void ctx.loadOpencodeStatsForSession(ctx.opencodeSessions[0].title)
104
+ }
105
+ return
106
+ }
107
+
108
+ if (key.ctrl && keyName === 'p') {
109
+ ctx.clearPendingKill()
110
+ ctx.openCommandsModal()
111
+ return
112
+ }
113
+
114
+ if (!ctx.prefixKey && !isStandard && keyName === 'l') {
115
+ ctx.clearPendingKill()
116
+ void ctx.handleLastSession()
117
+ return
118
+ }
119
+
120
+ if (!ctx.prefixKey && !isStandard && keyName === 'g') {
121
+ openSelectedRootSession(ctx)
122
+ return
123
+ }
124
+
125
+ if (!ctx.prefixKey && !isStandard && keyName === 'e') {
126
+ openSelectedEditTarget(ctx)
127
+ return
128
+ }
129
+
130
+ if (!isStandard && keyName === 'd') {
131
+ if (ctx.viewMode === ViewMode.Sessions && ctx.regularSessions[ctx.cursor]?.isSession) {
132
+ ctx.requestKillSession(ctx.regularSessions[ctx.cursor].title)
133
+ }
134
+ return
135
+ }
136
+
137
+ if (!ctx.prefixKey && !isStandard && keyName === 'r') {
138
+ if (ctx.viewMode === ViewMode.Sessions && ctx.regularSessions[ctx.cursor]?.isSession) {
139
+ ctx.clearPendingKill()
140
+ ctx.openRenameModal(ctx.regularSessions[ctx.cursor].title)
141
+ }
142
+ return
143
+ }
144
+
145
+ if (!ctx.prefixKey && !isStandard && keyName === 'R') {
146
+ ctx.clearPendingKill()
147
+ void ctx.refreshItems()
148
+ setTimedMessage(ctx, 'Refreshed')
149
+ return
150
+ }
151
+
152
+ if (!isStandard) {
153
+ if (keyName === 's') {
154
+ ctx.clearPendingKill()
155
+ ctx.setViewMode(ViewMode.Sessions)
156
+ void ctx.refreshItems(ViewMode.Sessions)
157
+ return
158
+ }
159
+
160
+ if (keyName === 'p') {
161
+ ctx.clearPendingKill()
162
+ ctx.setViewMode(ViewMode.Projects)
163
+ void ctx.refreshItems(ViewMode.Projects)
164
+ return
165
+ }
166
+ }
167
+
168
+ if (keyName === 'down' || (!isStandard && keyName === 'j')) {
169
+ ctx.clearPendingKill()
170
+ if (ctx.viewMode === ViewMode.Sessions) {
171
+ ctx.setCursor(cursor => Math.min(cursor + 1, ctx.regularSessions.length - 1))
172
+ return
173
+ }
174
+
175
+ ctx.setCursor(cursor => Math.min(cursor + 1, ctx.items.length - 1))
176
+ return
177
+ }
178
+
179
+ if (keyName === 'up' || (!isStandard && keyName === 'k')) {
180
+ ctx.clearPendingKill()
181
+ ctx.setCursor(cursor => Math.max(cursor - 1, 0))
182
+ return
183
+ }
184
+
185
+ if (keyName === 'return') {
186
+ ctx.clearPendingKill()
187
+ const item = getSelectedItem(ctx)
188
+ if (item) {
189
+ void ctx.handleSelect(item)
190
+ }
191
+ return
192
+ }
193
+
194
+ if (!isStandard && keyName >= '1' && keyName <= '9') {
195
+ ctx.clearPendingKill()
196
+ const item = getQuickSelectItem(ctx, Number.parseInt(keyName, 10) - 1)
197
+ if (item) {
198
+ void ctx.handleSelect(item)
199
+ }
200
+ }
201
+ }