| 1 | import type { PermissionMode } from "../../permissions/types.js"; |
| 2 | import { |
| 3 | checkClipboardForImage, |
| 4 | getClipboardImage, |
| 5 | } from "../../util/clipboard.js"; |
| 6 | import { logger } from "../../util/logger.js"; |
| 7 | import type { TextBuffer } from "../TextBuffer.js"; |
| 8 | |
| 9 | // Helper function to handle control keys |
| 10 | interface ControlKeysOptions { |
| 11 | input: string; |
| 12 | key: any; |
| 13 | exit: () => void; |
| 14 | showSlashCommands: boolean; |
| 15 | showFileSearch: boolean; |
| 16 | cycleModes: () => Promise<PermissionMode>; |
| 17 | clearInput?: () => void; |
| 18 | textBuffer?: TextBuffer; |
| 19 | onTextBufferUpdate?: () => void; |
| 20 | } |
| 21 | |
| 22 | export function handleControlKeys(options: ControlKeysOptions): boolean { |
| 23 | const { |
| 24 | input, |
| 25 | key, |
| 26 | exit, |
| 27 | showSlashCommands, |
| 28 | showFileSearch, |
| 29 | cycleModes, |
| 30 | clearInput, |
| 31 | textBuffer, |
| 32 | onTextBufferUpdate, |
| 33 | } = options; |
| 34 | |
| 35 | // Handle Ctrl+C with two-stage exit, Ctrl+D immediately exits |
| 36 | if (key.ctrl && input === "c") { |
| 37 | // Clear input box if clearInput function is provided |
| 38 | if (clearInput) { |
| 39 | clearInput(); |
| 40 | } |
| 41 | // Let the main process SIGINT handler handle Ctrl+C logic |
| 42 | process.kill(process.pid, "SIGINT"); |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | // Handle Ctrl+V for clipboard paste (including images) |
| 47 | // Note: Cmd+V often doesn't work for image pasting as terminals don't send the key event |
| 48 | if (key.ctrl && input === "v" && textBuffer) { |
| 49 | logger.debug("Handling Ctrl+V clipboard paste"); |
| 50 | |
| 51 | // Check clipboard for images immediately on paste event |
| 52 | checkClipboardForImage() |
| 53 | .then(async (hasImage) => { |
| 54 | if (hasImage) { |
| 55 | logger.debug("Image found in clipboard during paste event"); |
| 56 | const imageBuffer = await getClipboardImage(); |
| 57 | if (imageBuffer) { |
| 58 | textBuffer.addImage(imageBuffer); |
| 59 | // Trigger UI update |
| 60 | if (onTextBufferUpdate) { |
| 61 | onTextBufferUpdate(); |
| 62 | } |
| 63 | return; |
| 64 | } |
| 65 | } |
| 66 | // If no image, let normal text paste handling continue |
| 67 | }) |
| 68 | .catch((error) => { |
| 69 | logger.debug("Error checking clipboard for image:", error); |
| 70 | }); |
| 71 | |
| 72 | // Don't consume the event - let normal text paste handling continue |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | // Handle Ctrl+D to exit |
| 77 | if (key.ctrl && input === "d") { |
| 78 | exit(); |
| 79 | import("../../util/exit.js").then(({ gracefulExit }) => gracefulExit(0)); |
| 80 | } |
| 81 | |
| 82 | // Handle Ctrl+L to refresh screen (clear terminal artifacts) |
| 83 | if (key.ctrl && input === "l") { |
| 84 | process.stdout.write("\x1b[2J\x1b[H"); |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | // Handle Shift+Tab to cycle through modes |
| 89 | if (key.tab && key.shift && !showSlashCommands && !showFileSearch) { |
| 90 | cycleModes().catch((error) => { |
| 91 | logger.error("Failed to cycle modes:", error); |
| 92 | }); |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | // Helper to update text buffer state |
| 100 | interface TextBufferStateOptions { |
| 101 | handled: boolean; |
| 102 | textBuffer: any; |
| 103 | setInputText: (text: string) => void; |
| 104 | setCursorPosition: (pos: number) => void; |
| 105 | updateSlashCommandState: (text: string, cursor: number) => void; |
| 106 | updateFileSearchState: (text: string, cursor: number) => void; |
| 107 | updateBashModeState: (text: string) => void; |
| 108 | inputHistory: any; |
| 109 | } |
| 110 | |
| 111 | export function updateTextBufferState(options: TextBufferStateOptions) { |
| 112 | const { |
| 113 | handled: _handled, |
| 114 | textBuffer, |
| 115 | setInputText, |
| 116 | setCursorPosition, |
| 117 | updateSlashCommandState, |
| 118 | updateFileSearchState, |
| 119 | updateBashModeState, |
| 120 | inputHistory, |
| 121 | } = options; |
| 122 | |
| 123 | // Skip state updates during rapid input mode to avoid conflicts with timer-based updates |
| 124 | if (!textBuffer.isInRapidInputMode()) { |
| 125 | const newText = textBuffer.text; |
| 126 | const newCursor = textBuffer.cursor; |
| 127 | setInputText(newText); |
| 128 | setCursorPosition(newCursor); |
| 129 | updateSlashCommandState(newText, newCursor); |
| 130 | updateFileSearchState(newText, newCursor); |
| 131 | updateBashModeState(newText); |
| 132 | inputHistory.resetNavigation(); |
| 133 | } |
| 134 | } |