Elffuss BraidHub

kikocis/continue public

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