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
@@ -0,0 +1,325 @@
1
+ import { executeCommand as runCommand } from './commands'
2
+ import { persistConfigUpdate, runWithErrorMessage } from './operations'
3
+ import { AppMode, type Config, type Item, type OpencodeSessionStats, type ViewMode } from '../types'
4
+ import type { SettingsFieldId } from '../settings'
5
+ import { applyEditorSetting, applyOptionSetting, getSettingEditorTitle } from '../settings'
6
+ import {
7
+ handleEditTarget as actionHandleEditTarget,
8
+ handleKillSession as actionHandleKillSession,
9
+ handleLastSession as actionHandleLastSession,
10
+ handleNewSessionSubmit as actionHandleNewSessionSubmit,
11
+ handleRenameSubmit as actionHandleRenameSubmit,
12
+ handleRootSession as actionHandleRootSession,
13
+ handleSelect as actionHandleSelect,
14
+ } from '../handlers/actions'
15
+
16
+ type ShowMessage = (message: string, timeout?: number) => void
17
+
18
+ interface SharedHandlerOptions {
19
+ config: Config | null
20
+ items: Item[]
21
+ sessionItems: Item[]
22
+ cursor: number
23
+ showMessage: ShowMessage
24
+ refreshItems: (forceViewMode?: ViewMode, nextConfig?: Config | null) => Promise<void>
25
+ }
26
+
27
+ interface KillHandlerOptions extends SharedHandlerOptions {
28
+ setPendingKillSessionName: (sessionName: string | null) => void
29
+ }
30
+
31
+ interface SettingsHandlerOptions {
32
+ config: Config | null
33
+ saveConfig: (config: Config) => Promise<void>
34
+ setConfig: (config: Config) => void
35
+ setSettingEditorError: (message: string) => void
36
+ openSettingsModal: () => void
37
+ settingEditorValue: string
38
+ settingEditorPlainText?: string
39
+ refreshItems: (forceViewMode?: ViewMode, nextConfig?: Config | null) => Promise<void>
40
+ showMessage: ShowMessage
41
+ }
42
+
43
+ export async function handleKillSessionWithFeedback(
44
+ sessionName: string,
45
+ options: KillHandlerOptions
46
+ ): Promise<void> {
47
+ options.setPendingKillSessionName(null)
48
+
49
+ await actionHandleKillSession(sessionName, {
50
+ onSuccess: msg => options.showMessage(msg),
51
+ onError: msg => options.showMessage(msg, 3000),
52
+ refreshItems: () => options.refreshItems(),
53
+ })
54
+ }
55
+
56
+ export async function handleSelectItem(item: Item, config: Config | null): Promise<void> {
57
+ await actionHandleSelect(item, config)
58
+ }
59
+
60
+ export async function handleLastSessionWithFeedback(options: SharedHandlerOptions): Promise<void> {
61
+ await runWithErrorMessage(
62
+ () => actionHandleLastSession(options.sessionItems),
63
+ 'Failed to switch to the previous session',
64
+ options.showMessage
65
+ )
66
+ }
67
+
68
+ export async function handleRootSessionWithFeedback(
69
+ item: Item | undefined,
70
+ options: SharedHandlerOptions
71
+ ): Promise<void> {
72
+ await runWithErrorMessage(
73
+ () => actionHandleRootSession(item, options.config),
74
+ 'Failed to open the root session',
75
+ options.showMessage
76
+ )
77
+ }
78
+
79
+ export async function handleEditTargetWithFeedback(
80
+ item: Item | undefined,
81
+ options: SharedHandlerOptions
82
+ ): Promise<void> {
83
+ await runWithErrorMessage(
84
+ () => actionHandleEditTarget(item, options.config),
85
+ 'Failed to edit target',
86
+ options.showMessage
87
+ )
88
+ }
89
+
90
+ export async function handleRenameSubmitWithFeedback(
91
+ renameTarget: string,
92
+ newName: string,
93
+ closeModal: () => void,
94
+ options: Pick<SharedHandlerOptions, 'showMessage' | 'refreshItems'>
95
+ ): Promise<void> {
96
+ if (newName && newName !== renameTarget) {
97
+ await actionHandleRenameSubmit(renameTarget, newName, {
98
+ onSuccess: msg => options.showMessage(msg),
99
+ onError: msg => options.showMessage(msg, 3000),
100
+ refreshItems: () => options.refreshItems(),
101
+ })
102
+ }
103
+
104
+ closeModal()
105
+ }
106
+
107
+ export async function handleNewSessionSubmitWithSearch(
108
+ searchTerm: string,
109
+ options: SharedHandlerOptions
110
+ ): Promise<void> {
111
+ if (!searchTerm) {
112
+ return
113
+ }
114
+
115
+ await runWithErrorMessage(
116
+ () => actionHandleNewSessionSubmit(searchTerm, options.config, options.items, options.cursor),
117
+ 'Failed to create session',
118
+ options.showMessage
119
+ )
120
+ }
121
+
122
+ export async function handleSettingsEditorSubmitWithFeedback(
123
+ field: SettingsFieldId,
124
+ options: SettingsHandlerOptions
125
+ ): Promise<void> {
126
+ if (!options.config) {
127
+ return
128
+ }
129
+
130
+ try {
131
+ const rawText = options.settingEditorPlainText ?? options.settingEditorValue
132
+ const nextConfig = applyEditorSetting(options.config, field, rawText, process.env.HOME || '~')
133
+
134
+ options.setSettingEditorError('')
135
+ await persistConfigUpdate(
136
+ nextConfig,
137
+ `${getSettingEditorTitle(field)} updated`,
138
+ options.saveConfig,
139
+ options.setConfig,
140
+ options.refreshItems,
141
+ options.showMessage
142
+ )
143
+ options.openSettingsModal()
144
+ } catch (error) {
145
+ const errorMessage = error instanceof Error ? error.message : 'Failed to save settings'
146
+ options.setSettingEditorError(errorMessage)
147
+ }
148
+ }
149
+
150
+ export async function handleSettingOptionSubmitWithFeedback(
151
+ field: SettingsFieldId,
152
+ value: string,
153
+ options: SettingsHandlerOptions
154
+ ): Promise<void> {
155
+ if (!options.config) {
156
+ return
157
+ }
158
+
159
+ const nextConfig = applyOptionSetting(options.config, field, value)
160
+ await persistConfigUpdate(
161
+ nextConfig,
162
+ `${getSettingEditorTitle(field)} updated`,
163
+ options.saveConfig,
164
+ options.setConfig,
165
+ options.refreshItems,
166
+ options.showMessage
167
+ )
168
+ }
169
+
170
+ export async function executeAppCommand(
171
+ commandID: Parameters<typeof runCommand>[0],
172
+ options: Parameters<typeof runCommand>[1]
173
+ ): Promise<void> {
174
+ await runCommand(commandID, options)
175
+ }
176
+
177
+ interface CreateAppHandlersOptions extends SharedHandlerOptions {
178
+ appMode: AppMode
179
+ viewMode: ViewMode
180
+ opencodeCursor: number
181
+ regularSessions: Item[]
182
+ opencodeSessions: Item[]
183
+ selectedPrimaryItem?: Item
184
+ sessionCandidateItems: Item[]
185
+ projectSourceItems: Item[]
186
+ closeModal: () => void
187
+ openRenameModal: (sessionName: string) => void
188
+ openSettingsModal: () => void
189
+ requestKillSession: (sessionName: string) => void
190
+ setAppMode: (mode: AppMode) => void
191
+ setViewMode: (mode: ViewMode) => void
192
+ setAllItems: (items: Item[]) => void
193
+ setItems: (items: Item[]) => void
194
+ setCursor: (cursor: number) => void
195
+ setSearchQuery: (value: string) => void
196
+ setOpencodeCursor: (cursor: number) => void
197
+ setPendingKillSessionName: (sessionName: string | null) => void
198
+ saveConfig: (config: Config) => Promise<void>
199
+ setConfig: (config: Config) => void
200
+ setSettingEditorError: (message: string) => void
201
+ settingEditorValue: string
202
+ settingEditorPlainText?: string
203
+ renameTarget: string
204
+ renamedValue: string
205
+ searchTerm: string
206
+ loadOpencodeStatsForSession: (sessionName: string) => Promise<OpencodeSessionStats | null>
207
+ }
208
+
209
+ export function createAppHandlers(options: CreateAppHandlersOptions) {
210
+ const sharedOptions: SharedHandlerOptions = {
211
+ config: options.config,
212
+ items: options.items,
213
+ sessionItems: options.sessionItems,
214
+ cursor: options.cursor,
215
+ showMessage: options.showMessage,
216
+ refreshItems: options.refreshItems,
217
+ }
218
+
219
+ async function handleKillSessionWrapper(sessionName: string) {
220
+ await handleKillSessionWithFeedback(sessionName, {
221
+ ...sharedOptions,
222
+ setPendingKillSessionName: options.setPendingKillSessionName,
223
+ })
224
+ }
225
+
226
+ async function handleSelectWrapper(item: Item) {
227
+ await handleSelectItem(item, options.config)
228
+ }
229
+
230
+ async function handleLastSessionWrapper() {
231
+ await handleLastSessionWithFeedback(sharedOptions)
232
+ }
233
+
234
+ async function handleRootSessionWrapper(item?: Item) {
235
+ await handleRootSessionWithFeedback(item, sharedOptions)
236
+ }
237
+
238
+ async function handleEditTargetWrapper(item?: Item) {
239
+ await handleEditTargetWithFeedback(item, sharedOptions)
240
+ }
241
+
242
+ async function handleRenameSubmit() {
243
+ await handleRenameSubmitWithFeedback(
244
+ options.renameTarget,
245
+ options.renamedValue,
246
+ options.closeModal,
247
+ sharedOptions
248
+ )
249
+ }
250
+
251
+ async function handleNewSessionSubmit() {
252
+ await handleNewSessionSubmitWithSearch(options.searchTerm, sharedOptions)
253
+ }
254
+
255
+ async function handleSettingsEditorSubmit(field: SettingsFieldId) {
256
+ await handleSettingsEditorSubmitWithFeedback(field, {
257
+ config: options.config,
258
+ saveConfig: options.saveConfig,
259
+ setConfig: options.setConfig,
260
+ setSettingEditorError: options.setSettingEditorError,
261
+ openSettingsModal: options.openSettingsModal,
262
+ settingEditorValue: options.settingEditorValue,
263
+ settingEditorPlainText: options.settingEditorPlainText,
264
+ refreshItems: options.refreshItems,
265
+ showMessage: options.showMessage,
266
+ })
267
+ }
268
+
269
+ async function handleSettingOptionSubmit(field: SettingsFieldId, value: string) {
270
+ await handleSettingOptionSubmitWithFeedback(field, value, {
271
+ config: options.config,
272
+ saveConfig: options.saveConfig,
273
+ setConfig: options.setConfig,
274
+ setSettingEditorError: options.setSettingEditorError,
275
+ openSettingsModal: options.openSettingsModal,
276
+ settingEditorValue: options.settingEditorValue,
277
+ refreshItems: options.refreshItems,
278
+ showMessage: options.showMessage,
279
+ })
280
+ }
281
+
282
+ async function executeCommand(commandID: Parameters<typeof executeAppCommand>[0]) {
283
+ await executeAppCommand(commandID, {
284
+ appMode: options.appMode,
285
+ viewMode: options.viewMode,
286
+ cursor: options.cursor,
287
+ opencodeCursor: options.opencodeCursor,
288
+ regularSessions: options.regularSessions,
289
+ opencodeSessions: options.opencodeSessions,
290
+ selectedPrimaryItem: options.selectedPrimaryItem,
291
+ sessionCandidateItems: options.sessionCandidateItems,
292
+ projectSourceItems: options.projectSourceItems,
293
+ closeModal: options.closeModal,
294
+ openRenameModal: options.openRenameModal,
295
+ openSettingsModal: options.openSettingsModal,
296
+ requestKillSession: options.requestKillSession,
297
+ setAppMode: options.setAppMode,
298
+ setViewMode: options.setViewMode,
299
+ setAllItems: options.setAllItems,
300
+ setItems: options.setItems,
301
+ setCursor: options.setCursor,
302
+ setSearchQuery: options.setSearchQuery,
303
+ setOpencodeCursor: options.setOpencodeCursor,
304
+ refreshItems: forceViewMode => options.refreshItems(forceViewMode),
305
+ handleLastSession: handleLastSessionWrapper,
306
+ handleRootSession: handleRootSessionWrapper,
307
+ handleEditTarget: handleEditTargetWrapper,
308
+ loadOpencodeStatsForSession: options.loadOpencodeStatsForSession,
309
+ showMessage: options.showMessage,
310
+ })
311
+ }
312
+
313
+ return {
314
+ handleKillSessionWrapper,
315
+ handleSelectWrapper,
316
+ handleLastSessionWrapper,
317
+ handleRootSessionWrapper,
318
+ handleEditTargetWrapper,
319
+ handleRenameSubmit,
320
+ handleNewSessionSubmit,
321
+ handleSettingsEditorSubmit,
322
+ handleSettingOptionSubmit,
323
+ executeCommand,
324
+ }
325
+ }
@@ -0,0 +1,161 @@
1
+ import { useKeyboard } from '@opentui/react'
2
+ import type { Dispatch, MutableRefObject, SetStateAction } from 'react'
3
+ import { handleModalKeyboard } from './modal-keyboard'
4
+ import type { ModalState } from './modals'
5
+ import {
6
+ handleNewSessionMode,
7
+ handleNormalMode,
8
+ handleOpencodeManageMode,
9
+ handleSearchMode,
10
+ type KeyboardHandlerContext,
11
+ } from '../handlers/keyboard'
12
+ import type { SettingsEntry, SettingsFieldId, SettingsOption } from '../settings'
13
+ import type { Config, Item, OpencodeSessionStats } from '../types'
14
+ import type { CommandEntry } from '../ui/CommandsModal'
15
+ import { AppMode, ViewMode } from '../types'
16
+
17
+ interface UseAppKeyboardOptions {
18
+ appMode: AppMode
19
+ viewMode: ViewMode
20
+ config: Config | null
21
+ items: Item[]
22
+ regularSessions: Item[]
23
+ opencodeSessions: Item[]
24
+ sessionItems: Item[]
25
+ sessionCandidateItems: Item[]
26
+ projectSourceItems: Item[]
27
+ cursor: number
28
+ opencodeCursor: number
29
+ searchQuery: string
30
+ prefixActive: boolean
31
+ prefixTimeoutRef: MutableRefObject<NodeJS.Timeout | null>
32
+ modalState: ModalState
33
+ filteredCommandEntries: CommandEntry[]
34
+ commandsCursor: number
35
+ setCommandsCursor: Dispatch<SetStateAction<number>>
36
+ filteredSettingsEntries: SettingsEntry[]
37
+ settingsCursor: number
38
+ setSettingsCursor: Dispatch<SetStateAction<number>>
39
+ filteredSettingOptions: SettingsOption[]
40
+ settingOptionsCursor: number
41
+ setSettingOptionsCursor: Dispatch<SetStateAction<number>>
42
+ setModalState: Dispatch<SetStateAction<ModalState>>
43
+ setSettingEditorError: Dispatch<SetStateAction<string>>
44
+ executeCommand: (id: CommandEntry['id']) => Promise<void>
45
+ handleSettingOptionSubmit: (field: SettingsFieldId, value: string) => Promise<void>
46
+ handleSettingsEditorSubmit: (field: SettingsFieldId) => Promise<void>
47
+ handleRenameSubmit: () => Promise<void>
48
+ handleNewSessionSubmit: () => Promise<void>
49
+ isOptionSetting: (field: SettingsFieldId) => boolean
50
+ openSettingOptions: (field: SettingsFieldId) => void
51
+ openSettingEditor: (field: SettingsFieldId) => void
52
+ closeModal: () => void
53
+ clearPendingKill: () => void
54
+ requestKillSession: (sessionName: string) => void
55
+ openRenameModal: (sessionName: string) => void
56
+ openCommandsModal: () => void
57
+ openSettingsModal: () => void
58
+ refreshItems: (forceViewMode?: ViewMode, nextConfig?: Config | null) => Promise<void>
59
+ handleSelect: (item: Item) => Promise<void>
60
+ handleKillSession: (sessionName: string) => Promise<void>
61
+ handleLastSession: () => Promise<void>
62
+ handleRootSession: (item?: Item) => Promise<void>
63
+ handleEditTarget: (item?: Item) => Promise<void>
64
+ loadOpencodeStatsForSession: (sessionName: string) => Promise<OpencodeSessionStats | null>
65
+ setAppMode: Dispatch<SetStateAction<AppMode>>
66
+ setViewMode: Dispatch<SetStateAction<ViewMode>>
67
+ setCursor: Dispatch<SetStateAction<number>>
68
+ setOpencodeCursor: Dispatch<SetStateAction<number>>
69
+ setSearchQuery: Dispatch<SetStateAction<string>>
70
+ setAllItems: Dispatch<SetStateAction<Item[]>>
71
+ setItems: Dispatch<SetStateAction<Item[]>>
72
+ setPrefixActive: Dispatch<SetStateAction<boolean>>
73
+ setMessage: Dispatch<SetStateAction<string>>
74
+ }
75
+
76
+ export function useAppKeyboard(options: UseAppKeyboardOptions) {
77
+ const keyboardContext: KeyboardHandlerContext = {
78
+ appMode: options.appMode,
79
+ viewMode: options.viewMode,
80
+ items: options.items,
81
+ regularSessions: options.regularSessions,
82
+ opencodeSessions: options.opencodeSessions,
83
+ cursor: options.cursor,
84
+ opencodeCursor: options.opencodeCursor,
85
+ searchQuery: options.searchQuery,
86
+ prefixKey: options.config?.prefixKey,
87
+ projectItems:
88
+ options.sessionCandidateItems.length > 0
89
+ ? options.sessionCandidateItems
90
+ : options.projectSourceItems,
91
+ sessionItems: options.sessionItems,
92
+ prefixActive: options.prefixActive,
93
+ prefixTimeoutRef: options.prefixTimeoutRef,
94
+ setAppMode: options.setAppMode,
95
+ setViewMode: options.setViewMode,
96
+ setCursor: options.setCursor,
97
+ setOpencodeCursor: options.setOpencodeCursor,
98
+ setSearchQuery: options.setSearchQuery,
99
+ setAllItems: options.setAllItems,
100
+ setItems: options.setItems,
101
+ setPrefixActive: options.setPrefixActive,
102
+ refreshItems: options.refreshItems,
103
+ requestKillSession: options.requestKillSession,
104
+ clearPendingKill: options.clearPendingKill,
105
+ handleSelect: options.handleSelect,
106
+ handleKillSession: options.handleKillSession,
107
+ handleLastSession: options.handleLastSession,
108
+ handleRootSession: options.handleRootSession,
109
+ handleEditTarget: options.handleEditTarget,
110
+ openRenameModal: options.openRenameModal,
111
+ openCommandsModal: options.openCommandsModal,
112
+ openSettingsModal: options.openSettingsModal,
113
+ loadOpencodeStatsForSession: options.loadOpencodeStatsForSession,
114
+ setMessage: options.setMessage,
115
+ }
116
+
117
+ useKeyboard(key => {
118
+ const keybindMode = options.config?.keybindMode || 'vim'
119
+
120
+ if (
121
+ handleModalKeyboard(key, {
122
+ modalState: options.modalState,
123
+ filteredCommandEntries: options.filteredCommandEntries,
124
+ commandsCursor: options.commandsCursor,
125
+ setCommandsCursor: options.setCommandsCursor,
126
+ filteredSettingsEntries: options.filteredSettingsEntries,
127
+ settingsCursor: options.settingsCursor,
128
+ setSettingsCursor: options.setSettingsCursor,
129
+ filteredSettingOptions: options.filteredSettingOptions,
130
+ settingOptionsCursor: options.settingOptionsCursor,
131
+ setSettingOptionsCursor: options.setSettingOptionsCursor,
132
+ closeModal: options.closeModal,
133
+ openSettingOptions: options.openSettingOptions,
134
+ openSettingEditor: options.openSettingEditor,
135
+ isOptionSetting: options.isOptionSetting,
136
+ setModalState: options.setModalState,
137
+ setSettingEditorError: options.setSettingEditorError,
138
+ executeCommand: options.executeCommand,
139
+ handleSettingOptionSubmit: options.handleSettingOptionSubmit,
140
+ handleSettingsEditorSubmit: options.handleSettingsEditorSubmit,
141
+ handleRenameSubmit: options.handleRenameSubmit,
142
+ })
143
+ ) {
144
+ return
145
+ }
146
+
147
+ if (options.appMode === AppMode.Normal) {
148
+ handleNormalMode(key, keyboardContext, keybindMode)
149
+ } else if (options.appMode === AppMode.Search) {
150
+ handleSearchMode(key, keyboardContext, keybindMode)
151
+ } else if (options.appMode === AppMode.NewSession) {
152
+ if (key.name === 'return') {
153
+ void options.handleNewSessionSubmit()
154
+ } else {
155
+ handleNewSessionMode(key, keyboardContext, keybindMode)
156
+ }
157
+ } else if (options.appMode === AppMode.OpencodeManage) {
158
+ handleOpencodeManageMode(key, keyboardContext, keybindMode)
159
+ }
160
+ })
161
+ }
@@ -0,0 +1,118 @@
1
+ import type { ModalState } from './modals'
2
+ import type { SettingsEntry, SettingsOption } from '../settings'
3
+ import type { CommandEntry } from '../ui/CommandsModal'
4
+
5
+ interface ModalKeyboardContext {
6
+ modalState: ModalState
7
+ filteredCommandEntries: CommandEntry[]
8
+ commandsCursor: number
9
+ setCommandsCursor: (cursor: (current: number) => number) => void
10
+ filteredSettingsEntries: SettingsEntry[]
11
+ settingsCursor: number
12
+ setSettingsCursor: (cursor: (current: number) => number) => void
13
+ filteredSettingOptions: SettingsOption[]
14
+ settingOptionsCursor: number
15
+ setSettingOptionsCursor: (cursor: (current: number) => number) => void
16
+ closeModal: () => void
17
+ openSettingOptions: (field: SettingsEntry['id']) => void
18
+ openSettingEditor: (field: SettingsEntry['id']) => void
19
+ isOptionSetting: (field: SettingsEntry['id']) => boolean
20
+ setModalState: (state: ModalState) => void
21
+ setSettingEditorError: (message: string) => void
22
+ executeCommand: (id: CommandEntry['id']) => Promise<void>
23
+ handleSettingOptionSubmit: (field: SettingsEntry['id'], value: string) => Promise<void>
24
+ handleSettingsEditorSubmit: (field: SettingsEntry['id']) => Promise<void>
25
+ handleRenameSubmit: () => Promise<void>
26
+ }
27
+
28
+ interface KeyboardInput {
29
+ name?: string
30
+ }
31
+
32
+ function openSelectedSetting(ctx: ModalKeyboardContext) {
33
+ const entry = ctx.filteredSettingsEntries[ctx.settingsCursor]
34
+ if (!entry) {
35
+ return
36
+ }
37
+
38
+ if (ctx.isOptionSetting(entry.id)) {
39
+ ctx.openSettingOptions(entry.id)
40
+ return
41
+ }
42
+
43
+ ctx.openSettingEditor(entry.id)
44
+ }
45
+
46
+ export function handleModalKeyboard(key: KeyboardInput, ctx: ModalKeyboardContext): boolean {
47
+ if (ctx.modalState?.type === 'rename') {
48
+ if (key.name === 'return') {
49
+ void ctx.handleRenameSubmit()
50
+ } else if (key.name === 'escape') {
51
+ ctx.closeModal()
52
+ }
53
+ return true
54
+ }
55
+
56
+ if (ctx.modalState?.type === 'commands') {
57
+ if (key.name === 'escape' || key.name === 'q') {
58
+ ctx.closeModal()
59
+ } else if (key.name === 'down' || key.name === 'j') {
60
+ ctx.setCommandsCursor(current => Math.min(current + 1, ctx.filteredCommandEntries.length - 1))
61
+ } else if (key.name === 'up' || key.name === 'k') {
62
+ ctx.setCommandsCursor(current => Math.max(current - 1, 0))
63
+ } else if (key.name === 'return') {
64
+ const command = ctx.filteredCommandEntries[ctx.commandsCursor]
65
+ if (!command) {
66
+ return true
67
+ }
68
+ void ctx.executeCommand(command.id)
69
+ }
70
+ return true
71
+ }
72
+
73
+ if (ctx.modalState?.type === 'settings') {
74
+ if (key.name === 'escape') {
75
+ ctx.closeModal()
76
+ } else if (key.name === 'down' || key.name === 'j') {
77
+ ctx.setSettingsCursor(current =>
78
+ Math.min(current + 1, ctx.filteredSettingsEntries.length - 1)
79
+ )
80
+ } else if (key.name === 'up' || key.name === 'k') {
81
+ ctx.setSettingsCursor(current => Math.max(current - 1, 0))
82
+ } else if (key.name === 'return' || key.name === 'e') {
83
+ openSelectedSetting(ctx)
84
+ }
85
+ return true
86
+ }
87
+
88
+ if (ctx.modalState?.type === 'setting-options') {
89
+ if (key.name === 'escape') {
90
+ ctx.setModalState({ type: 'settings' })
91
+ } else if (key.name === 'down' || key.name === 'j') {
92
+ ctx.setSettingOptionsCursor(current =>
93
+ Math.min(current + 1, ctx.filteredSettingOptions.length - 1)
94
+ )
95
+ } else if (key.name === 'up' || key.name === 'k') {
96
+ ctx.setSettingOptionsCursor(current => Math.max(current - 1, 0))
97
+ } else if (key.name === 'return') {
98
+ const option = ctx.filteredSettingOptions[ctx.settingOptionsCursor]
99
+ if (!option) {
100
+ return true
101
+ }
102
+ void ctx.handleSettingOptionSubmit(ctx.modalState.field, option.value)
103
+ }
104
+ return true
105
+ }
106
+
107
+ if (ctx.modalState?.type === 'setting-editor') {
108
+ if (key.name === 'escape') {
109
+ ctx.setModalState({ type: 'settings' })
110
+ ctx.setSettingEditorError('')
111
+ } else if (key.name === 'return') {
112
+ void ctx.handleSettingsEditorSubmit(ctx.modalState.field)
113
+ }
114
+ return true
115
+ }
116
+
117
+ return false
118
+ }
@@ -0,0 +1,50 @@
1
+ import type { TextareaRenderable } from '@opentui/core'
2
+ import { useRef, useState } from 'react'
3
+ import type { ModalState } from './modals'
4
+
5
+ export function useAppModalState() {
6
+ const [modalState, setModalState] = useState<ModalState>(null)
7
+ const [modalInputValue, setModalInputValue] = useState('')
8
+ const [commandsCursor, setCommandsCursor] = useState(0)
9
+ const [commandsSearchQuery, setCommandsSearchQuery] = useState('')
10
+ const [settingsCursor, setSettingsCursor] = useState(0)
11
+ const [settingsSearchQuery, setSettingsSearchQuery] = useState('')
12
+ const [settingOptionsCursor, setSettingOptionsCursor] = useState(0)
13
+ const [settingOptionsSearchQuery, setSettingOptionsSearchQuery] = useState('')
14
+ const [settingEditorValue, setSettingEditorValue] = useState('')
15
+ const [settingEditorError, setSettingEditorError] = useState('')
16
+
17
+ const modalTextareaRef = useRef<TextareaRenderable | null>(null)
18
+ const commandsSearchTextareaRef = useRef<TextareaRenderable | null>(null)
19
+ const settingsSearchTextareaRef = useRef<TextareaRenderable | null>(null)
20
+ const settingOptionsSearchTextareaRef = useRef<TextareaRenderable | null>(null)
21
+ const settingEditorTextareaRef = useRef<TextareaRenderable | null>(null)
22
+
23
+ return {
24
+ modalState,
25
+ setModalState,
26
+ modalInputValue,
27
+ setModalInputValue,
28
+ commandsCursor,
29
+ setCommandsCursor,
30
+ commandsSearchQuery,
31
+ setCommandsSearchQuery,
32
+ settingsCursor,
33
+ setSettingsCursor,
34
+ settingsSearchQuery,
35
+ setSettingsSearchQuery,
36
+ settingOptionsCursor,
37
+ setSettingOptionsCursor,
38
+ settingOptionsSearchQuery,
39
+ setSettingOptionsSearchQuery,
40
+ settingEditorValue,
41
+ setSettingEditorValue,
42
+ settingEditorError,
43
+ setSettingEditorError,
44
+ modalTextareaRef,
45
+ commandsSearchTextareaRef,
46
+ settingsSearchTextareaRef,
47
+ settingOptionsSearchTextareaRef,
48
+ settingEditorTextareaRef,
49
+ }
50
+ }