| 1 | import type { PermissionMode } from "../../permissions/types.js"; |
| 2 | import { logger } from "../../util/logger.js"; |
| 3 | |
| 4 | // Helper function to handle control keys |
| 5 | interface ControlKeysOptions { |
| 6 | input: string; |
| 7 | key: any; |
| 8 | exit: () => void; |
| 9 | showSlashCommands: boolean; |
| 10 | showFileSearch: boolean; |
| 11 | cycleModes: () => Promise<PermissionMode>; |
| 12 | } |
| 13 | |
| 14 | export function handleControlKeys(options: ControlKeysOptions): boolean { |
| 15 | const { input, key, exit, showSlashCommands, showFileSearch, cycleModes } = |
| 16 | options; |
| 17 | |
| 18 | // Handle Ctrl+C and Ctrl+D |
| 19 | if (key.ctrl && (input === "c" || input === "d")) { |
| 20 | exit(); |
| 21 | return true; |
| 22 | } |
| 23 | |
| 24 | // Handle Ctrl+L to refresh screen (clear terminal artifacts) |
| 25 | if (key.ctrl && input === "l") { |
| 26 | process.stdout.write("\x1b[2J\x1b[H"); |
| 27 | return true; |
| 28 | } |
| 29 | |
| 30 | // Handle Shift+Tab to cycle through modes |
| 31 | if (key.tab && key.shift && !showSlashCommands && !showFileSearch) { |
| 32 | cycleModes().catch((error) => { |
| 33 | logger.error("Failed to cycle modes:", error); |
| 34 | }); |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | // Helper to update text buffer state |
| 42 | interface TextBufferStateOptions { |
| 43 | handled: boolean; |
| 44 | textBuffer: any; |
| 45 | setInputText: (text: string) => void; |
| 46 | setCursorPosition: (pos: number) => void; |
| 47 | updateSlashCommandState: (text: string, cursor: number) => void; |
| 48 | updateFileSearchState: (text: string, cursor: number) => void; |
| 49 | inputHistory: any; |
| 50 | } |
| 51 | |
| 52 | export function updateTextBufferState(options: TextBufferStateOptions) { |
| 53 | const { |
| 54 | handled, |
| 55 | textBuffer, |
| 56 | setInputText, |
| 57 | setCursorPosition, |
| 58 | updateSlashCommandState, |
| 59 | updateFileSearchState, |
| 60 | inputHistory, |
| 61 | } = options; |
| 62 | |
| 63 | // Skip state updates during rapid input mode to avoid conflicts with timer-based updates |
| 64 | if (handled && !textBuffer.isInRapidInputMode()) { |
| 65 | const newText = textBuffer.text; |
| 66 | const newCursor = textBuffer.cursor; |
| 67 | setInputText(newText); |
| 68 | setCursorPosition(newCursor); |
| 69 | updateSlashCommandState(newText, newCursor); |
| 70 | updateFileSearchState(newText, newCursor); |
| 71 | inputHistory.resetNavigation(); |
| 72 | } |
| 73 | } |