Files
gemini-cli/packages/cli/src/ui/components/InputPrompt.tsx
T

222 lines
6.6 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
2025-04-30 08:31:32 -07:00
import React, { useCallback } from 'react';
2025-05-13 11:24:04 -07:00
import { Text, Box, Key } from 'ink';
2025-04-19 12:38:09 -04:00
import { Colors } from '../colors.js';
import { Suggestion } from './SuggestionsDisplay.js';
2025-05-13 11:24:04 -07:00
import { MultilineTextEditor } from './shared/multiline-editor.js';
2025-05-13 16:23:14 -07:00
import { useInputHistory } from '../hooks/useInputHistory.js';
2025-04-15 21:41:08 -07:00
interface InputPromptProps {
2025-04-30 08:31:32 -07:00
query: string;
2025-05-13 11:24:04 -07:00
onChange: (value: string) => void;
onChangeAndMoveCursor: (value: string) => void;
editorState: EditorState;
2025-04-17 18:06:21 -04:00
onSubmit: (value: string) => void;
2025-04-30 08:31:32 -07:00
showSuggestions: boolean;
2025-05-02 14:39:39 -07:00
suggestions: Suggestion[];
2025-04-30 08:31:32 -07:00
activeSuggestionIndex: number;
resetCompletion: () => void;
2025-05-13 16:23:14 -07:00
userMessages: readonly string[];
2025-05-13 11:24:04 -07:00
navigateSuggestionUp: () => void;
navigateSuggestionDown: () => void;
setEditorState: (updater: (prevState: EditorState) => EditorState) => void;
onClearScreen: () => void;
2025-05-13 11:24:04 -07:00
}
export interface EditorState {
key: number;
initialCursorOffset?: number;
2025-04-15 21:41:08 -07:00
}
2025-04-30 08:31:32 -07:00
export const InputPrompt: React.FC<InputPromptProps> = ({
query,
2025-05-13 11:24:04 -07:00
onChange,
onChangeAndMoveCursor,
editorState,
2025-04-30 08:31:32 -07:00
onSubmit,
showSuggestions,
suggestions,
activeSuggestionIndex,
2025-05-13 16:23:14 -07:00
userMessages,
2025-05-13 11:24:04 -07:00
navigateSuggestionUp,
navigateSuggestionDown,
2025-04-30 08:31:32 -07:00
resetCompletion,
setEditorState,
onClearScreen,
2025-04-30 08:31:32 -07:00
}) => {
2025-05-13 16:23:14 -07:00
const handleSubmit = useCallback(
(submittedValue: string) => {
onSubmit(submittedValue);
onChangeAndMoveCursor(''); // Clear query after submit
},
[onSubmit, onChangeAndMoveCursor],
);
const inputHistory = useInputHistory({
userMessages,
onSubmit: handleSubmit,
isActive: !showSuggestions, // Input history is active when suggestions are not shown
currentQuery: query,
onChangeAndMoveCursor,
});
const handleAutocomplete = useCallback(
(indexToUse: number) => {
if (indexToUse < 0 || indexToUse >= suggestions.length) {
return;
}
const selectedSuggestion = suggestions[indexToUse];
const trimmedQuery = query.trimStart();
2025-04-30 08:31:32 -07:00
if (trimmedQuery.startsWith('/')) {
// Handle / command completion
const slashIndex = query.indexOf('/');
const base = query.substring(0, slashIndex + 1);
const newValue = base + selectedSuggestion.value;
2025-05-13 11:24:04 -07:00
onChangeAndMoveCursor(newValue);
onSubmit(newValue); // Execute the command
onChangeAndMoveCursor(''); // Clear query after submit
} else {
// Handle @ command completion
const atIndex = query.lastIndexOf('@');
if (atIndex === -1) return;
2025-05-01 00:52:01 +00:00
// Find the part of the query after the '@'
const pathPart = query.substring(atIndex + 1);
// Find the last slash within that part
const lastSlashIndexInPath = pathPart.lastIndexOf('/');
2025-05-01 00:52:01 +00:00
let base = '';
if (lastSlashIndexInPath === -1) {
// No slash after '@', replace everything after '@'
base = query.substring(0, atIndex + 1);
} else {
// Slash found, keep everything up to and including the last slash
base = query.substring(0, atIndex + 1 + lastSlashIndexInPath + 1);
}
2025-05-01 00:52:01 +00:00
const newValue = base + selectedSuggestion.value;
2025-05-13 11:24:04 -07:00
onChangeAndMoveCursor(newValue);
}
2025-04-30 08:31:32 -07:00
resetCompletion(); // Hide suggestions after selection
},
[query, suggestions, resetCompletion, onChangeAndMoveCursor, onSubmit],
);
2025-04-30 08:31:32 -07:00
2025-05-13 11:24:04 -07:00
const inputPreprocessor = useCallback(
(
input: string,
key: Key,
_currentText?: string,
_cursorOffset?: number,
) => {
2025-04-30 08:31:32 -07:00
if (showSuggestions) {
if (key.upArrow) {
2025-05-13 11:24:04 -07:00
navigateSuggestionUp();
return true;
2025-04-30 08:31:32 -07:00
} else if (key.downArrow) {
2025-05-13 11:24:04 -07:00
navigateSuggestionDown();
return true;
} else if (key.tab) {
if (suggestions.length > 0) {
const targetIndex =
activeSuggestionIndex === -1 ? 0 : activeSuggestionIndex;
if (targetIndex < suggestions.length) {
handleAutocomplete(targetIndex);
2025-05-13 11:24:04 -07:00
return true;
}
}
} else if (key.return) {
if (activeSuggestionIndex >= 0) {
handleAutocomplete(activeSuggestionIndex);
} else {
if (query.trim()) {
2025-05-13 16:23:14 -07:00
handleSubmit(query);
}
}
2025-05-13 11:24:04 -07:00
return true;
2025-04-30 08:31:32 -07:00
} else if (key.escape) {
resetCompletion();
2025-05-13 11:24:04 -07:00
return true;
}
} else {
// Keybindings when suggestions are not shown
if (key.ctrl && input === 'a') {
setEditorState((s) => ({ key: s.key + 1, initialCursorOffset: 0 }));
return true;
}
if (key.ctrl && input === 'e') {
setEditorState((s) => ({
key: s.key + 1,
initialCursorOffset: query.length,
}));
return true;
}
if (key.ctrl && input === 'l') {
onClearScreen();
return true;
}
if (key.ctrl && input === 'p') {
inputHistory.navigateUp();
return true;
}
if (key.ctrl && input === 'n') {
inputHistory.navigateDown();
return true;
}
}
2025-05-13 11:24:04 -07:00
return false;
},
2025-05-13 11:24:04 -07:00
[
handleAutocomplete,
navigateSuggestionDown,
navigateSuggestionUp,
query,
suggestions,
showSuggestions,
resetCompletion,
activeSuggestionIndex,
2025-05-13 16:23:14 -07:00
handleSubmit,
inputHistory,
setEditorState,
onClearScreen,
2025-05-13 11:24:04 -07:00
],
);
2025-04-17 18:06:21 -04:00
return (
2025-04-19 12:38:09 -04:00
<Box borderStyle="round" borderColor={Colors.AccentBlue} paddingX={1}>
<Text color={Colors.AccentPurple}>&gt; </Text>
<Box flexGrow={1}>
2025-05-13 11:24:04 -07:00
<MultilineTextEditor
key={editorState.key.toString()}
initialCursorOffset={editorState.initialCursorOffset}
initialText={query}
onChange={onChange}
2025-04-30 08:31:32 -07:00
placeholder="Enter your message or use tools (e.g., @src/file.txt)..."
2025-05-13 11:24:04 -07:00
/* Account for width used by the box and &gt; */
2025-05-13 16:23:14 -07:00
navigateUp={inputHistory.navigateUp}
navigateDown={inputHistory.navigateDown}
2025-05-13 11:24:04 -07:00
inputPreprocessor={inputPreprocessor}
widthUsedByParent={3}
widthFraction={0.9}
2025-04-19 12:38:09 -04:00
onSubmit={() => {
// This onSubmit is for the TextInput component itself.
// It should only fire if suggestions are NOT showing,
2025-05-13 11:24:04 -07:00
// as inputPreprocessor handles Enter when suggestions are visible.
const trimmedQuery = query.trim();
if (!showSuggestions && trimmedQuery) {
2025-05-13 16:23:14 -07:00
handleSubmit(trimmedQuery);
}
2025-04-19 12:38:09 -04:00
}}
/>
</Box>
2025-04-17 18:06:21 -04:00
</Box>
);
2025-04-18 18:08:43 -04:00
};