2025-04-18 17:44:24 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-06-03 23:01:26 -07:00
|
|
|
import React, { useCallback, useEffect, useState } from 'react';
|
2025-06-27 10:57:32 -07:00
|
|
|
import { Box, Text } from 'ink';
|
2025-04-19 12:38:09 -04:00
|
|
|
import { Colors } from '../colors.js';
|
2025-05-20 16:50:32 -07:00
|
|
|
import { SuggestionsDisplay } from './SuggestionsDisplay.js';
|
2025-05-13 16:23:14 -07:00
|
|
|
import { useInputHistory } from '../hooks/useInputHistory.js';
|
2025-06-19 20:17:23 +00:00
|
|
|
import { TextBuffer } from './shared/text-buffer.js';
|
2025-07-18 14:54:10 -07:00
|
|
|
import { cpSlice, cpLen, toCodePoints } from '../utils/textUtils.js';
|
2025-05-20 16:50:32 -07:00
|
|
|
import chalk from 'chalk';
|
|
|
|
|
import stringWidth from 'string-width';
|
2025-06-17 22:17:16 -04:00
|
|
|
import { useShellHistory } from '../hooks/useShellHistory.js';
|
2025-05-20 16:50:32 -07:00
|
|
|
import { useCompletion } from '../hooks/useCompletion.js';
|
2025-06-27 10:57:32 -07:00
|
|
|
import { useKeypress, Key } from '../hooks/useKeypress.js';
|
2025-05-20 16:50:32 -07:00
|
|
|
import { isAtCommand, isSlashCommand } from '../utils/commandUtils.js';
|
2025-07-07 16:45:44 -04:00
|
|
|
import { CommandContext, SlashCommand } from '../commands/types.js';
|
2025-06-25 05:41:11 -07:00
|
|
|
import { Config } from '@google/gemini-cli-core';
|
2025-07-12 00:06:49 -04:00
|
|
|
import {
|
|
|
|
|
clipboardHasImage,
|
|
|
|
|
saveClipboardImage,
|
|
|
|
|
cleanupOldClipboardImages,
|
|
|
|
|
} from '../utils/clipboardUtils.js';
|
|
|
|
|
import * as path from 'path';
|
2025-04-18 11:12:18 -07:00
|
|
|
|
2025-06-03 23:01:26 -07:00
|
|
|
export interface InputPromptProps {
|
2025-06-13 09:59:09 -07:00
|
|
|
buffer: TextBuffer;
|
2025-04-17 18:06:21 -04:00
|
|
|
onSubmit: (value: string) => void;
|
2025-05-13 16:23:14 -07:00
|
|
|
userMessages: readonly string[];
|
2025-05-14 17:33:37 -07:00
|
|
|
onClearScreen: () => void;
|
2025-07-07 16:45:44 -04:00
|
|
|
config: Config;
|
2025-07-20 16:57:34 -04:00
|
|
|
slashCommands: readonly SlashCommand[];
|
2025-07-07 16:45:44 -04:00
|
|
|
commandContext: CommandContext;
|
2025-05-20 16:50:32 -07:00
|
|
|
placeholder?: string;
|
|
|
|
|
focus?: boolean;
|
2025-06-13 09:59:09 -07:00
|
|
|
inputWidth: number;
|
|
|
|
|
suggestionsWidth: number;
|
2025-05-18 01:18:32 -07:00
|
|
|
shellModeActive: boolean;
|
|
|
|
|
setShellModeActive: (value: boolean) => void;
|
2025-05-13 11:24:04 -07:00
|
|
|
}
|
|
|
|
|
|
2025-04-30 08:31:32 -07:00
|
|
|
export const InputPrompt: React.FC<InputPromptProps> = ({
|
2025-06-13 09:59:09 -07:00
|
|
|
buffer,
|
2025-04-30 08:31:32 -07:00
|
|
|
onSubmit,
|
2025-05-13 16:23:14 -07:00
|
|
|
userMessages,
|
2025-05-14 17:33:37 -07:00
|
|
|
onClearScreen,
|
2025-05-20 16:50:32 -07:00
|
|
|
config,
|
|
|
|
|
slashCommands,
|
2025-07-07 16:45:44 -04:00
|
|
|
commandContext,
|
2025-06-06 13:44:11 -07:00
|
|
|
placeholder = ' Type your message or @path/to/file',
|
2025-05-20 16:50:32 -07:00
|
|
|
focus = true,
|
2025-06-13 09:59:09 -07:00
|
|
|
inputWidth,
|
|
|
|
|
suggestionsWidth,
|
2025-05-18 01:18:32 -07:00
|
|
|
shellModeActive,
|
|
|
|
|
setShellModeActive,
|
2025-04-30 08:31:32 -07:00
|
|
|
}) => {
|
2025-06-03 23:01:26 -07:00
|
|
|
const [justNavigatedHistory, setJustNavigatedHistory] = useState(false);
|
2025-07-18 14:54:10 -07:00
|
|
|
|
|
|
|
|
// Check if cursor is after @ or / without unescaped spaces
|
|
|
|
|
const isCursorAfterCommandWithoutSpace = useCallback(() => {
|
|
|
|
|
const [row, col] = buffer.cursor;
|
|
|
|
|
const currentLine = buffer.lines[row] || '';
|
|
|
|
|
|
|
|
|
|
// Convert current line to code points for Unicode-aware processing
|
|
|
|
|
const codePoints = toCodePoints(currentLine);
|
|
|
|
|
|
|
|
|
|
// Search backwards from cursor position within the current line only
|
|
|
|
|
for (let i = col - 1; i >= 0; i--) {
|
|
|
|
|
const char = codePoints[i];
|
|
|
|
|
|
|
|
|
|
if (char === ' ') {
|
|
|
|
|
// Check if this space is escaped by counting backslashes before it
|
|
|
|
|
let backslashCount = 0;
|
|
|
|
|
for (let j = i - 1; j >= 0 && codePoints[j] === '\\'; j--) {
|
|
|
|
|
backslashCount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If there's an odd number of backslashes, the space is escaped
|
|
|
|
|
const isEscaped = backslashCount % 2 === 1;
|
|
|
|
|
|
|
|
|
|
if (!isEscaped) {
|
|
|
|
|
// Found unescaped space before @ or /, return false
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// If escaped, continue searching backwards
|
|
|
|
|
} else if (char === '@' || char === '/') {
|
|
|
|
|
// Found @ or / without unescaped space in between
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}, [buffer.cursor, buffer.lines]);
|
|
|
|
|
|
|
|
|
|
const shouldShowCompletion = useCallback(
|
|
|
|
|
() =>
|
|
|
|
|
(isAtCommand(buffer.text) || isSlashCommand(buffer.text)) &&
|
|
|
|
|
isCursorAfterCommandWithoutSpace(),
|
|
|
|
|
[buffer.text, isCursorAfterCommandWithoutSpace],
|
|
|
|
|
);
|
|
|
|
|
|
2025-05-20 16:50:32 -07:00
|
|
|
const completion = useCompletion(
|
|
|
|
|
buffer.text,
|
|
|
|
|
config.getTargetDir(),
|
2025-07-18 14:54:10 -07:00
|
|
|
shouldShowCompletion(),
|
2025-05-20 16:50:32 -07:00
|
|
|
slashCommands,
|
2025-07-07 16:45:44 -04:00
|
|
|
commandContext,
|
Ignore folders files (#651)
# Add .gitignore-Aware File Filtering to gemini-cli
This pull request introduces .gitignore-based file filtering to the gemini-cli, ensuring that git-ignored files are automatically excluded from file-related operations and suggestions throughout the CLI. The update enhances usability, reduces noise from build artifacts and dependencies, and provides new configuration options for fine-tuning file discovery.
Key Improvements
.gitignore File Filtering
All @ (at) commands, file completions, and core discovery tools now honor .gitignore patterns by default.
Git-ignored files (such as node_modules/, dist/, .env, and .git) are excluded from results unless explicitly overridden.
The behavior can be customized via a new fileFiltering section in settings.json, including options for:
Turning .gitignore respect on/off.
Adding custom ignore patterns.
Allowing or excluding build artifacts.
Configuration & Documentation Updates
settings.json schema extended with fileFiltering options.
Documentation updated to explain new filtering controls and usage patterns.
Testing
New and updated integration/unit tests for file filtering logic, configuration merging, and edge cases.
Test coverage ensures .gitignore filtering works as intended across different workflows.
Internal Refactoring
Core file discovery logic refactored for maintainability and extensibility.
Underlying tools (ls, glob, read-many-files) now support git-aware filtering out of the box.
Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
2025-06-03 21:40:46 -07:00
|
|
|
config,
|
2025-05-20 16:50:32 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const resetCompletionState = completion.resetCompletionState;
|
2025-06-17 22:17:16 -04:00
|
|
|
const shellHistory = useShellHistory(config.getProjectRoot());
|
2025-05-20 16:50:32 -07:00
|
|
|
|
|
|
|
|
const handleSubmitAndClear = useCallback(
|
2025-05-13 16:23:14 -07:00
|
|
|
(submittedValue: string) => {
|
2025-06-17 22:17:16 -04:00
|
|
|
if (shellModeActive) {
|
|
|
|
|
shellHistory.addCommandToHistory(submittedValue);
|
|
|
|
|
}
|
2025-05-30 15:16:06 -07:00
|
|
|
// Clear the buffer *before* calling onSubmit to prevent potential re-submission
|
|
|
|
|
// if onSubmit triggers a re-render while the buffer still holds the old value.
|
2025-05-20 16:50:32 -07:00
|
|
|
buffer.setText('');
|
2025-05-30 15:16:06 -07:00
|
|
|
onSubmit(submittedValue);
|
2025-05-20 16:50:32 -07:00
|
|
|
resetCompletionState();
|
2025-05-13 16:23:14 -07:00
|
|
|
},
|
2025-06-17 22:17:16 -04:00
|
|
|
[onSubmit, buffer, resetCompletionState, shellModeActive, shellHistory],
|
2025-05-20 16:50:32 -07:00
|
|
|
);
|
|
|
|
|
|
2025-06-03 23:01:26 -07:00
|
|
|
const customSetTextAndResetCompletionSignal = useCallback(
|
|
|
|
|
(newText: string) => {
|
|
|
|
|
buffer.setText(newText);
|
|
|
|
|
setJustNavigatedHistory(true);
|
|
|
|
|
},
|
|
|
|
|
[buffer, setJustNavigatedHistory],
|
|
|
|
|
);
|
|
|
|
|
|
2025-05-13 16:23:14 -07:00
|
|
|
const inputHistory = useInputHistory({
|
|
|
|
|
userMessages,
|
2025-05-20 16:50:32 -07:00
|
|
|
onSubmit: handleSubmitAndClear,
|
2025-07-18 01:30:39 +03:00
|
|
|
isActive:
|
|
|
|
|
(!completion.showSuggestions || completion.suggestions.length === 1) &&
|
|
|
|
|
!shellModeActive,
|
2025-05-20 16:50:32 -07:00
|
|
|
currentQuery: buffer.text,
|
2025-06-03 23:01:26 -07:00
|
|
|
onChange: customSetTextAndResetCompletionSignal,
|
2025-05-13 16:23:14 -07:00
|
|
|
});
|
|
|
|
|
|
2025-06-03 23:01:26 -07:00
|
|
|
// Effect to reset completion if history navigation just occurred and set the text
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (justNavigatedHistory) {
|
|
|
|
|
resetCompletionState();
|
|
|
|
|
setJustNavigatedHistory(false);
|
|
|
|
|
}
|
|
|
|
|
}, [
|
|
|
|
|
justNavigatedHistory,
|
|
|
|
|
buffer.text,
|
|
|
|
|
resetCompletionState,
|
|
|
|
|
setJustNavigatedHistory,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-05-20 16:50:32 -07:00
|
|
|
const completionSuggestions = completion.suggestions;
|
2025-05-07 12:30:32 -07:00
|
|
|
const handleAutocomplete = useCallback(
|
|
|
|
|
(indexToUse: number) => {
|
2025-05-20 16:50:32 -07:00
|
|
|
if (indexToUse < 0 || indexToUse >= completionSuggestions.length) {
|
2025-05-07 12:30:32 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2025-05-20 16:50:32 -07:00
|
|
|
const query = buffer.text;
|
2025-07-01 15:51:43 -07:00
|
|
|
const suggestion = completionSuggestions[indexToUse].value;
|
2025-04-30 08:31:32 -07:00
|
|
|
|
2025-05-20 16:50:32 -07:00
|
|
|
if (query.trimStart().startsWith('/')) {
|
2025-07-07 16:45:44 -04:00
|
|
|
const hasTrailingSpace = query.endsWith(' ');
|
|
|
|
|
const parts = query
|
|
|
|
|
.trimStart()
|
|
|
|
|
.substring(1)
|
|
|
|
|
.split(/\s+/)
|
|
|
|
|
.filter(Boolean);
|
2025-06-15 11:40:39 -07:00
|
|
|
|
2025-07-07 16:45:44 -04:00
|
|
|
let isParentPath = false;
|
|
|
|
|
// If there's no trailing space, we need to check if the current query
|
|
|
|
|
// is already a complete path to a parent command.
|
|
|
|
|
if (!hasTrailingSpace) {
|
2025-07-20 16:57:34 -04:00
|
|
|
let currentLevel: readonly SlashCommand[] | undefined = slashCommands;
|
2025-07-07 16:45:44 -04:00
|
|
|
for (let i = 0; i < parts.length; i++) {
|
|
|
|
|
const part = parts[i];
|
|
|
|
|
const found: SlashCommand | undefined = currentLevel?.find(
|
2025-07-20 16:57:34 -04:00
|
|
|
(cmd) => cmd.name === part || cmd.altNames?.includes(part),
|
2025-07-07 16:45:44 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
|
if (i === parts.length - 1 && found.subCommands) {
|
|
|
|
|
isParentPath = true;
|
|
|
|
|
}
|
2025-07-20 16:57:34 -04:00
|
|
|
currentLevel = found.subCommands as
|
|
|
|
|
| readonly SlashCommand[]
|
|
|
|
|
| undefined;
|
2025-07-07 16:45:44 -04:00
|
|
|
} else {
|
|
|
|
|
// Path is invalid, so it can't be a parent path.
|
|
|
|
|
currentLevel = undefined;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-07-07 15:43:27 -04:00
|
|
|
}
|
2025-06-15 11:40:39 -07:00
|
|
|
}
|
2025-07-07 16:45:44 -04:00
|
|
|
|
|
|
|
|
// Determine the base path of the command.
|
|
|
|
|
// - If there's a trailing space, the whole command is the base.
|
|
|
|
|
// - If it's a known parent path, the whole command is the base.
|
|
|
|
|
// - Otherwise, the base is everything EXCEPT the last partial part.
|
|
|
|
|
const basePath =
|
|
|
|
|
hasTrailingSpace || isParentPath ? parts : parts.slice(0, -1);
|
2025-07-18 00:55:29 -04:00
|
|
|
const newValue = `/${[...basePath, suggestion].join(' ')}`;
|
2025-07-07 16:45:44 -04:00
|
|
|
|
|
|
|
|
buffer.setText(newValue);
|
2025-05-07 12:30:32 -07:00
|
|
|
} else {
|
|
|
|
|
const atIndex = query.lastIndexOf('@');
|
|
|
|
|
if (atIndex === -1) return;
|
|
|
|
|
const pathPart = query.substring(atIndex + 1);
|
|
|
|
|
const lastSlashIndexInPath = pathPart.lastIndexOf('/');
|
2025-05-20 16:50:32 -07:00
|
|
|
let autoCompleteStartIndex = atIndex + 1;
|
|
|
|
|
if (lastSlashIndexInPath !== -1) {
|
|
|
|
|
autoCompleteStartIndex += lastSlashIndexInPath + 1;
|
2025-05-07 12:30:32 -07:00
|
|
|
}
|
2025-05-20 16:50:32 -07:00
|
|
|
buffer.replaceRangeByOffset(
|
|
|
|
|
autoCompleteStartIndex,
|
|
|
|
|
buffer.text.length,
|
2025-07-01 15:51:43 -07:00
|
|
|
suggestion,
|
2025-05-20 16:50:32 -07:00
|
|
|
);
|
2025-05-07 12:30:32 -07:00
|
|
|
}
|
2025-05-20 16:50:32 -07:00
|
|
|
resetCompletionState();
|
2025-05-07 12:30:32 -07:00
|
|
|
},
|
2025-07-07 16:45:44 -04:00
|
|
|
[resetCompletionState, buffer, completionSuggestions, slashCommands],
|
2025-05-07 12:30:32 -07:00
|
|
|
);
|
2025-04-30 08:31:32 -07:00
|
|
|
|
2025-07-12 00:06:49 -04:00
|
|
|
// Handle clipboard image pasting with Ctrl+V
|
|
|
|
|
const handleClipboardImage = useCallback(async () => {
|
|
|
|
|
try {
|
|
|
|
|
if (await clipboardHasImage()) {
|
|
|
|
|
const imagePath = await saveClipboardImage(config.getTargetDir());
|
|
|
|
|
if (imagePath) {
|
|
|
|
|
// Clean up old images
|
|
|
|
|
cleanupOldClipboardImages(config.getTargetDir()).catch(() => {
|
|
|
|
|
// Ignore cleanup errors
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Get relative path from current directory
|
|
|
|
|
const relativePath = path.relative(config.getTargetDir(), imagePath);
|
|
|
|
|
|
|
|
|
|
// Insert @path reference at cursor position
|
|
|
|
|
const insertText = `@${relativePath}`;
|
|
|
|
|
const currentText = buffer.text;
|
|
|
|
|
const [row, col] = buffer.cursor;
|
|
|
|
|
|
|
|
|
|
// Calculate offset from row/col
|
|
|
|
|
let offset = 0;
|
|
|
|
|
for (let i = 0; i < row; i++) {
|
|
|
|
|
offset += buffer.lines[i].length + 1; // +1 for newline
|
|
|
|
|
}
|
|
|
|
|
offset += col;
|
|
|
|
|
|
|
|
|
|
// Add spaces around the path if needed
|
|
|
|
|
let textToInsert = insertText;
|
|
|
|
|
const charBefore = offset > 0 ? currentText[offset - 1] : '';
|
|
|
|
|
const charAfter =
|
|
|
|
|
offset < currentText.length ? currentText[offset] : '';
|
|
|
|
|
|
|
|
|
|
if (charBefore && charBefore !== ' ' && charBefore !== '\n') {
|
|
|
|
|
textToInsert = ' ' + textToInsert;
|
|
|
|
|
}
|
|
|
|
|
if (!charAfter || (charAfter !== ' ' && charAfter !== '\n')) {
|
|
|
|
|
textToInsert = textToInsert + ' ';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert at cursor position
|
|
|
|
|
buffer.replaceRangeByOffset(offset, offset, textToInsert);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error handling clipboard image:', error);
|
|
|
|
|
}
|
|
|
|
|
}, [buffer, config]);
|
|
|
|
|
|
2025-06-27 10:57:32 -07:00
|
|
|
const handleInput = useCallback(
|
|
|
|
|
(key: Key) => {
|
2025-05-20 16:50:32 -07:00
|
|
|
if (!focus) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 16:45:44 -04:00
|
|
|
if (
|
|
|
|
|
key.sequence === '!' &&
|
|
|
|
|
buffer.text === '' &&
|
|
|
|
|
!completion.showSuggestions
|
|
|
|
|
) {
|
2025-05-18 01:18:32 -07:00
|
|
|
setShellModeActive(!shellModeActive);
|
2025-05-20 16:50:32 -07:00
|
|
|
buffer.setText(''); // Clear the '!' from input
|
2025-07-07 16:45:44 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (key.name === 'escape') {
|
|
|
|
|
if (shellModeActive) {
|
|
|
|
|
setShellModeActive(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (completion.showSuggestions) {
|
|
|
|
|
completion.resetCompletionState();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (key.ctrl && key.name === 'l') {
|
|
|
|
|
onClearScreen();
|
|
|
|
|
return;
|
2025-05-18 01:18:32 -07:00
|
|
|
}
|
2025-05-20 16:50:32 -07:00
|
|
|
|
2025-07-18 00:55:29 -04:00
|
|
|
// If the command is a perfect match, pressing enter should execute it.
|
|
|
|
|
if (completion.isPerfectMatch && key.name === 'return') {
|
|
|
|
|
handleSubmitAndClear(buffer.text);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-20 16:50:32 -07:00
|
|
|
if (completion.showSuggestions) {
|
2025-07-18 01:30:39 +03:00
|
|
|
if (completion.suggestions.length > 1) {
|
2025-07-22 01:43:23 +04:00
|
|
|
if (key.name === 'up' || (key.ctrl && key.name === 'p')) {
|
2025-07-18 01:30:39 +03:00
|
|
|
completion.navigateUp();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-07-22 01:43:23 +04:00
|
|
|
if (key.name === 'down' || (key.ctrl && key.name === 'n')) {
|
2025-07-18 01:30:39 +03:00
|
|
|
completion.navigateDown();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-05-20 16:50:32 -07:00
|
|
|
}
|
2025-07-07 16:45:44 -04:00
|
|
|
|
|
|
|
|
if (key.name === 'tab' || (key.name === 'return' && !key.ctrl)) {
|
2025-05-20 16:50:32 -07:00
|
|
|
if (completion.suggestions.length > 0) {
|
2025-05-07 12:30:32 -07:00
|
|
|
const targetIndex =
|
2025-05-20 16:50:32 -07:00
|
|
|
completion.activeSuggestionIndex === -1
|
2025-07-07 16:45:44 -04:00
|
|
|
? 0 // Default to the first if none is active
|
2025-05-20 16:50:32 -07:00
|
|
|
: completion.activeSuggestionIndex;
|
|
|
|
|
if (targetIndex < completion.suggestions.length) {
|
2025-05-07 12:30:32 -07:00
|
|
|
handleAutocomplete(targetIndex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-20 16:50:32 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2025-07-18 01:30:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!shellModeActive) {
|
|
|
|
|
if (key.ctrl && key.name === 'p') {
|
|
|
|
|
inputHistory.navigateUp();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (key.ctrl && key.name === 'n') {
|
|
|
|
|
inputHistory.navigateDown();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Handle arrow-up/down for history on single-line or at edges
|
|
|
|
|
if (
|
|
|
|
|
key.name === 'up' &&
|
|
|
|
|
(buffer.allVisualLines.length === 1 ||
|
|
|
|
|
(buffer.visualCursor[0] === 0 && buffer.visualScrollRow === 0))
|
|
|
|
|
) {
|
|
|
|
|
inputHistory.navigateUp();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (
|
|
|
|
|
key.name === 'down' &&
|
|
|
|
|
(buffer.allVisualLines.length === 1 ||
|
|
|
|
|
buffer.visualCursor[0] === buffer.allVisualLines.length - 1)
|
|
|
|
|
) {
|
|
|
|
|
inputHistory.navigateDown();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-05-14 17:33:37 -07:00
|
|
|
} else {
|
2025-07-18 01:30:39 +03:00
|
|
|
// Shell History Navigation
|
|
|
|
|
if (key.name === 'up') {
|
|
|
|
|
const prevCommand = shellHistory.getPreviousCommand();
|
|
|
|
|
if (prevCommand !== null) buffer.setText(prevCommand);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (key.name === 'down') {
|
|
|
|
|
const nextCommand = shellHistory.getNextCommand();
|
|
|
|
|
if (nextCommand !== null) buffer.setText(nextCommand);
|
|
|
|
|
return;
|
2025-07-07 16:45:44 -04:00
|
|
|
}
|
2025-07-18 01:30:39 +03:00
|
|
|
}
|
2025-07-07 16:45:44 -04:00
|
|
|
|
2025-07-18 01:30:39 +03:00
|
|
|
if (key.name === 'return' && !key.ctrl && !key.meta && !key.paste) {
|
|
|
|
|
if (buffer.text.trim()) {
|
|
|
|
|
const [row, col] = buffer.cursor;
|
|
|
|
|
const line = buffer.lines[row];
|
|
|
|
|
const charBefore = col > 0 ? cpSlice(line, col - 1, col) : '';
|
|
|
|
|
if (charBefore === '\\') {
|
|
|
|
|
buffer.backspace();
|
|
|
|
|
buffer.newline();
|
|
|
|
|
} else {
|
|
|
|
|
handleSubmitAndClear(buffer.text);
|
2025-07-07 16:45:44 -04:00
|
|
|
}
|
2025-05-20 16:50:32 -07:00
|
|
|
}
|
2025-07-18 01:30:39 +03:00
|
|
|
return;
|
2025-05-20 16:50:32 -07:00
|
|
|
}
|
|
|
|
|
|
2025-07-07 16:45:44 -04:00
|
|
|
// Newline insertion
|
|
|
|
|
if (key.name === 'return' && (key.ctrl || key.meta || key.paste)) {
|
|
|
|
|
buffer.newline();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ctrl+A (Home) / Ctrl+E (End)
|
2025-06-27 10:57:32 -07:00
|
|
|
if (key.ctrl && key.name === 'a') {
|
2025-05-20 16:50:32 -07:00
|
|
|
buffer.move('home');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-06-27 10:57:32 -07:00
|
|
|
if (key.ctrl && key.name === 'e') {
|
2025-05-20 16:50:32 -07:00
|
|
|
buffer.move('end');
|
2025-07-16 00:35:58 -03:00
|
|
|
buffer.moveToOffset(cpLen(buffer.text));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Ctrl+C (Clear input)
|
|
|
|
|
if (key.ctrl && key.name === 'c') {
|
|
|
|
|
if (buffer.text.length > 0) {
|
|
|
|
|
buffer.setText('');
|
|
|
|
|
resetCompletionState();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-05-20 16:50:32 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 16:45:44 -04:00
|
|
|
// Kill line commands
|
2025-06-27 10:57:32 -07:00
|
|
|
if (key.ctrl && key.name === 'k') {
|
2025-05-20 16:50:32 -07:00
|
|
|
buffer.killLineRight();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-06-27 10:57:32 -07:00
|
|
|
if (key.ctrl && key.name === 'u') {
|
2025-05-20 16:50:32 -07:00
|
|
|
buffer.killLineLeft();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 16:45:44 -04:00
|
|
|
// External editor
|
|
|
|
|
const isCtrlX = key.ctrl && (key.name === 'x' || key.sequence === '\x18');
|
|
|
|
|
if (isCtrlX) {
|
|
|
|
|
buffer.openInExternalEditor();
|
2025-05-20 16:50:32 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-12 00:06:49 -04:00
|
|
|
// Ctrl+V for clipboard image paste
|
|
|
|
|
if (key.ctrl && key.name === 'v') {
|
|
|
|
|
handleClipboardImage();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 16:45:44 -04:00
|
|
|
// Fallback to the text buffer's default input handling for all other keys
|
2025-06-27 10:57:32 -07:00
|
|
|
buffer.handleInput(key);
|
2025-04-19 19:45:42 +01:00
|
|
|
},
|
2025-06-27 10:57:32 -07:00
|
|
|
[
|
|
|
|
|
focus,
|
|
|
|
|
buffer,
|
|
|
|
|
completion,
|
|
|
|
|
shellModeActive,
|
|
|
|
|
setShellModeActive,
|
|
|
|
|
onClearScreen,
|
|
|
|
|
inputHistory,
|
|
|
|
|
handleAutocomplete,
|
|
|
|
|
handleSubmitAndClear,
|
|
|
|
|
shellHistory,
|
2025-07-12 00:06:49 -04:00
|
|
|
handleClipboardImage,
|
2025-07-16 00:35:58 -03:00
|
|
|
resetCompletionState,
|
2025-06-27 10:57:32 -07:00
|
|
|
],
|
2025-04-19 19:45:42 +01:00
|
|
|
);
|
2025-04-18 17:06:16 +01:00
|
|
|
|
2025-06-27 10:57:32 -07:00
|
|
|
useKeypress(handleInput, { isActive: focus });
|
|
|
|
|
|
2025-05-20 16:50:32 -07:00
|
|
|
const linesToRender = buffer.viewportVisualLines;
|
|
|
|
|
const [cursorVisualRowAbsolute, cursorVisualColAbsolute] =
|
|
|
|
|
buffer.visualCursor;
|
|
|
|
|
const scrollVisualRow = buffer.visualScrollRow;
|
|
|
|
|
|
2025-04-17 18:06:21 -04:00
|
|
|
return (
|
2025-05-20 16:50:32 -07:00
|
|
|
<>
|
|
|
|
|
<Box
|
|
|
|
|
borderStyle="round"
|
|
|
|
|
borderColor={shellModeActive ? Colors.AccentYellow : Colors.AccentBlue}
|
|
|
|
|
paddingX={1}
|
|
|
|
|
>
|
|
|
|
|
<Text
|
|
|
|
|
color={shellModeActive ? Colors.AccentYellow : Colors.AccentPurple}
|
|
|
|
|
>
|
|
|
|
|
{shellModeActive ? '! ' : '> '}
|
|
|
|
|
</Text>
|
|
|
|
|
<Box flexGrow={1} flexDirection="column">
|
|
|
|
|
{buffer.text.length === 0 && placeholder ? (
|
2025-06-06 13:44:11 -07:00
|
|
|
focus ? (
|
|
|
|
|
<Text>
|
|
|
|
|
{chalk.inverse(placeholder.slice(0, 1))}
|
|
|
|
|
<Text color={Colors.Gray}>{placeholder.slice(1)}</Text>
|
|
|
|
|
</Text>
|
|
|
|
|
) : (
|
|
|
|
|
<Text color={Colors.Gray}>{placeholder}</Text>
|
|
|
|
|
)
|
2025-05-20 16:50:32 -07:00
|
|
|
) : (
|
|
|
|
|
linesToRender.map((lineText, visualIdxInRenderedSet) => {
|
|
|
|
|
const cursorVisualRow = cursorVisualRowAbsolute - scrollVisualRow;
|
2025-06-13 09:59:09 -07:00
|
|
|
let display = cpSlice(lineText, 0, inputWidth);
|
2025-05-20 16:50:32 -07:00
|
|
|
const currentVisualWidth = stringWidth(display);
|
2025-06-13 09:59:09 -07:00
|
|
|
if (currentVisualWidth < inputWidth) {
|
|
|
|
|
display = display + ' '.repeat(inputWidth - currentVisualWidth);
|
2025-05-20 16:50:32 -07:00
|
|
|
}
|
|
|
|
|
|
2025-07-17 20:45:42 -04:00
|
|
|
if (focus && visualIdxInRenderedSet === cursorVisualRow) {
|
2025-05-20 16:50:32 -07:00
|
|
|
const relativeVisualColForHighlight = cursorVisualColAbsolute;
|
2025-07-12 00:06:49 -04:00
|
|
|
|
2025-05-20 16:50:32 -07:00
|
|
|
if (relativeVisualColForHighlight >= 0) {
|
|
|
|
|
if (relativeVisualColForHighlight < cpLen(display)) {
|
|
|
|
|
const charToHighlight =
|
|
|
|
|
cpSlice(
|
|
|
|
|
display,
|
|
|
|
|
relativeVisualColForHighlight,
|
|
|
|
|
relativeVisualColForHighlight + 1,
|
|
|
|
|
) || ' ';
|
|
|
|
|
const highlighted = chalk.inverse(charToHighlight);
|
|
|
|
|
display =
|
|
|
|
|
cpSlice(display, 0, relativeVisualColForHighlight) +
|
|
|
|
|
highlighted +
|
|
|
|
|
cpSlice(display, relativeVisualColForHighlight + 1);
|
|
|
|
|
} else if (
|
|
|
|
|
relativeVisualColForHighlight === cpLen(display) &&
|
2025-06-13 09:59:09 -07:00
|
|
|
cpLen(display) === inputWidth
|
2025-05-20 16:50:32 -07:00
|
|
|
) {
|
|
|
|
|
display = display + chalk.inverse(' ');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
<Text key={`line-${visualIdxInRenderedSet}`}>{display}</Text>
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
)}
|
|
|
|
|
</Box>
|
2025-04-19 12:38:09 -04:00
|
|
|
</Box>
|
2025-05-20 16:50:32 -07:00
|
|
|
{completion.showSuggestions && (
|
|
|
|
|
<Box>
|
|
|
|
|
<SuggestionsDisplay
|
|
|
|
|
suggestions={completion.suggestions}
|
|
|
|
|
activeIndex={completion.activeSuggestionIndex}
|
|
|
|
|
isLoading={completion.isLoadingSuggestions}
|
|
|
|
|
width={suggestionsWidth}
|
|
|
|
|
scrollOffset={completion.visibleStartIndex}
|
|
|
|
|
userInput={buffer.text}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
2025-04-17 18:06:21 -04:00
|
|
|
);
|
2025-04-18 18:08:43 -04:00
|
|
|
};
|