| 1 | import type { PermissionMode } from "../../permissions/types.js"; |
| 2 | import { logger } from "../../util/logger.js"; |
| 3 | import type { TextBuffer } from "../TextBuffer.js"; |
| 4 | |
| 5 | // Helper function to handle control keys |
| 6 | interface ControlKeysOptions { |
| 7 | input: string; |
| 8 | key: any; |
| 9 | exit: () => void; |
| 10 | showSlashCommands: boolean; |
| 11 | showFileSearch: boolean; |
| 12 | cycleModes: () => Promise<PermissionMode>; |
| 13 | clearInput?: () => void; |
| 14 | textBuffer?: TextBuffer; |
| 15 | onTextBufferUpdate?: () => void; |
| 16 | } |
| 17 | |
| 18 | // Helper function to check if clipboard contains an image |
| 19 | async function checkClipboardForImage(): Promise<boolean> { |
| 20 | try { |
| 21 | const os = await import("os"); |
| 22 | const { exec } = await import("child_process"); |
| 23 | const { promisify } = await import("util"); |
| 24 | const execAsync = promisify(exec); |
| 25 | |
| 26 | const platform = os.platform(); |
| 27 | |
| 28 | if (platform === "darwin") { |
| 29 | // macOS: Check clipboard using osascript |
| 30 | const { stdout } = await execAsync('osascript -e "clipboard info"'); |
| 31 | return ( |
| 32 | stdout.includes("«class PNGf»") || |
| 33 | stdout.includes("«class TIFF»") || |
| 34 | stdout.includes("picture") |
| 35 | ); |
| 36 | } else if (platform === "win32") { |
| 37 | // Windows: Use PowerShell to check clipboard |
| 38 | const { stdout } = await execAsync( |
| 39 | 'powershell -command "Get-Clipboard -Format Image | Measure-Object | Select-Object -ExpandProperty Count"', |
| 40 | ); |
| 41 | return parseInt(stdout.trim()) > 0; |
| 42 | } else if (platform === "linux") { |
| 43 | // Linux: Check if xclip can get image data |
| 44 | try { |
| 45 | await execAsync( |
| 46 | "xclip -selection clipboard -t image/png -o > /dev/null 2>&1", |
| 47 | ); |
| 48 | return true; |
| 49 | } catch { |
| 50 | return false; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return false; |
| 55 | } catch (error) { |
| 56 | logger.debug("Error checking clipboard for image:", error); |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Helper function to get image from clipboard |
| 62 | async function getClipboardImage(): Promise<Buffer | null> { |
| 63 | try { |
| 64 | const os = await import("os"); |
| 65 | const { exec } = await import("child_process"); |
| 66 | const { promisify } = await import("util"); |
| 67 | const execAsync = promisify(exec); |
| 68 | const fs = await import("fs/promises"); |
| 69 | const path = await import("path"); |
| 70 | |
| 71 | const platform = os.platform(); |
| 72 | const tempDir = os.tmpdir(); |
| 73 | const tempImagePath = path.join( |
| 74 | tempDir, |
| 75 | `continue-clipboard-${Date.now()}.png`, |
| 76 | ); |
| 77 | |
| 78 | if (platform === "darwin") { |
| 79 | // macOS: Save clipboard image using osascript |
| 80 | await execAsync( |
| 81 | `osascript -e 'set the clipboard to (the clipboard as «class PNGf»)' -e 'set png_data to (the clipboard as «class PNGf»)' -e 'set file_ref to open for access "${tempImagePath}" with write permission' -e 'write png_data to file_ref' -e 'close access file_ref'`, |
| 82 | ); |
| 83 | } else if (platform === "win32") { |
| 84 | // Windows: Use PowerShell to save clipboard image |
| 85 | await execAsync( |
| 86 | `powershell -command "$image = Get-Clipboard -Format Image; if ($image) { $image.Save('${tempImagePath}') }"`, |
| 87 | ); |
| 88 | } else if (platform === "linux") { |
| 89 | // Linux: Use xclip to save clipboard image |
| 90 | await execAsync( |
| 91 | `xclip -selection clipboard -t image/png -o > "${tempImagePath}"`, |
| 92 | ); |
| 93 | } else { |
| 94 | return null; |
| 95 | } |
| 96 | |
| 97 | // Read the temporary file |
| 98 | const imageBuffer = await fs.readFile(tempImagePath); |
| 99 | |
| 100 | // Clean up the temporary file |
| 101 | await fs.unlink(tempImagePath).catch(() => { |
| 102 | // Ignore cleanup errors |
| 103 | }); |
| 104 | |
| 105 | return imageBuffer; |
| 106 | } catch (error) { |
| 107 | logger.debug("Error reading image from clipboard:", error); |
| 108 | return null; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | export function handleControlKeys(options: ControlKeysOptions): boolean { |
| 113 | const { |
| 114 | input, |
| 115 | key, |
| 116 | exit, |
| 117 | showSlashCommands, |
| 118 | showFileSearch, |
| 119 | cycleModes, |
| 120 | clearInput, |
| 121 | textBuffer, |
| 122 | onTextBufferUpdate, |
| 123 | } = options; |
| 124 | |
| 125 | // Handle Ctrl+C to clear input |
| 126 | if (key.ctrl && input === "c" && clearInput) { |
| 127 | clearInput(); |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | // Handle Ctrl+V for clipboard paste (including images) |
| 132 | if (key.ctrl && input === "v" && textBuffer) { |
| 133 | logger.debug("Handling clipboard paste"); |
| 134 | // Check for images asynchronously |
| 135 | checkClipboardForImage() |
| 136 | .then(async (hasImage) => { |
| 137 | if (hasImage) { |
| 138 | const imageBuffer = await getClipboardImage(); |
| 139 | if (imageBuffer) { |
| 140 | textBuffer.addImage(imageBuffer); |
| 141 | // Trigger UI update |
| 142 | if (onTextBufferUpdate) { |
| 143 | onTextBufferUpdate(); |
| 144 | } |
| 145 | return; |
| 146 | } |
| 147 | } |
| 148 | // If no image or image reading failed, let normal text paste handling continue |
| 149 | // We return false here to allow the normal paste processing to happen |
| 150 | }) |
| 151 | .catch((error) => { |
| 152 | logger.debug("Error checking clipboard for image:", error); |
| 153 | // Continue with normal text paste handling |
| 154 | }); |
| 155 | |
| 156 | // Don't consume the event - let normal paste handling continue |
| 157 | // This allows text pastes to work normally when no image is present |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | // Handle Ctrl+D to exit |
| 162 | if (key.ctrl && input === "d") { |
| 163 | exit(); |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | // Handle Ctrl+L to refresh screen (clear terminal artifacts) |
| 168 | if (key.ctrl && input === "l") { |
| 169 | process.stdout.write("\x1b[2J\x1b[H"); |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | // Handle Shift+Tab to cycle through modes |
| 174 | if (key.tab && key.shift && !showSlashCommands && !showFileSearch) { |
| 175 | cycleModes().catch((error) => { |
| 176 | logger.error("Failed to cycle modes:", error); |
| 177 | }); |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | // Helper to update text buffer state |
| 185 | interface TextBufferStateOptions { |
| 186 | handled: boolean; |
| 187 | textBuffer: any; |
| 188 | setInputText: (text: string) => void; |
| 189 | setCursorPosition: (pos: number) => void; |
| 190 | updateSlashCommandState: (text: string, cursor: number) => void; |
| 191 | updateFileSearchState: (text: string, cursor: number) => void; |
| 192 | inputHistory: any; |
| 193 | } |
| 194 | |
| 195 | export function updateTextBufferState(options: TextBufferStateOptions) { |
| 196 | const { |
| 197 | handled: _handled, |
| 198 | textBuffer, |
| 199 | setInputText, |
| 200 | setCursorPosition, |
| 201 | updateSlashCommandState, |
| 202 | updateFileSearchState, |
| 203 | inputHistory, |
| 204 | } = options; |
| 205 | |
| 206 | // Skip state updates during rapid input mode to avoid conflicts with timer-based updates |
| 207 | if (!textBuffer.isInRapidInputMode()) { |
| 208 | const newText = textBuffer.text; |
| 209 | const newCursor = textBuffer.cursor; |
| 210 | setInputText(newText); |
| 211 | setCursorPosition(newCursor); |
| 212 | updateSlashCommandState(newText, newCursor); |
| 213 | updateFileSearchState(newText, newCursor); |
| 214 | inputHistory.resetNavigation(); |
| 215 | } |
| 216 | } |