Merge pull request #12 from continuedev/input-history
Add input history navigation with up/down arrow keys
This commit is contained in:
commit
294f2e1948
3 changed files with 193 additions and 1 deletions
|
|
@ -3,6 +3,7 @@ import { Box, Text, useApp, useInput } from "ink";
|
|||
import React, { useState } from "react";
|
||||
import SlashCommandUI from "./SlashCommandUI.js";
|
||||
import { TextBuffer } from "./TextBuffer.js";
|
||||
import { InputHistory } from "../util/inputHistory.js";
|
||||
|
||||
interface UserInputProps {
|
||||
onSubmit: (message: string) => void;
|
||||
|
|
@ -20,6 +21,7 @@ const UserInput: React.FC<UserInputProps> = ({
|
|||
assistant,
|
||||
}) => {
|
||||
const [textBuffer] = useState(() => new TextBuffer());
|
||||
const [inputHistory] = useState(() => new InputHistory());
|
||||
const [inputText, setInputText] = useState("");
|
||||
const [cursorPosition, setCursorPosition] = useState(0);
|
||||
const [showSlashCommands, setShowSlashCommands] = useState(false);
|
||||
|
|
@ -166,10 +168,37 @@ const UserInput: React.FC<UserInputProps> = ({
|
|||
}
|
||||
}
|
||||
|
||||
// Handle input history navigation (only when not in slash command mode)
|
||||
if (!showSlashCommands) {
|
||||
if (key.upArrow) {
|
||||
const historyEntry = inputHistory.navigateUp(inputText);
|
||||
if (historyEntry !== null) {
|
||||
textBuffer.setText(historyEntry);
|
||||
textBuffer.setCursor(historyEntry.length);
|
||||
setInputText(historyEntry);
|
||||
setCursorPosition(historyEntry.length);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (key.downArrow) {
|
||||
const historyEntry = inputHistory.navigateDown(inputText);
|
||||
if (historyEntry !== null) {
|
||||
textBuffer.setText(historyEntry);
|
||||
textBuffer.setCursor(historyEntry.length);
|
||||
setInputText(historyEntry);
|
||||
setCursorPosition(historyEntry.length);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle Enter key (regular enter for submit)
|
||||
if (key.return && !key.shift) {
|
||||
if (textBuffer.text.trim() && !isWaitingForResponse) {
|
||||
onSubmit(textBuffer.text.trim());
|
||||
const submittedText = textBuffer.text.trim();
|
||||
inputHistory.addEntry(submittedText);
|
||||
onSubmit(submittedText);
|
||||
textBuffer.clear();
|
||||
setInputText("");
|
||||
setCursorPosition(0);
|
||||
|
|
@ -201,6 +230,9 @@ const UserInput: React.FC<UserInputProps> = ({
|
|||
setInputText(newText);
|
||||
setCursorPosition(newCursor);
|
||||
updateSlashCommandState(newText, newCursor);
|
||||
|
||||
// Reset history navigation when user starts typing
|
||||
inputHistory.resetNavigation();
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
114
src/util/inputHistory.ts
Normal file
114
src/util/inputHistory.ts
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import os from 'os';
|
||||
|
||||
const HISTORY_FILE = path.join(os.homedir(), '.continue', 'input_history.json');
|
||||
const MAX_HISTORY_SIZE = 1000;
|
||||
|
||||
export interface InputHistoryEntry {
|
||||
text: string;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
export class InputHistory {
|
||||
private history: InputHistoryEntry[] = [];
|
||||
private currentIndex: number = -1;
|
||||
private originalInput: string = '';
|
||||
|
||||
constructor() {
|
||||
this.loadHistory();
|
||||
}
|
||||
|
||||
private loadHistory(): void {
|
||||
try {
|
||||
const dir = path.dirname(HISTORY_FILE);
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
|
||||
if (fs.existsSync(HISTORY_FILE)) {
|
||||
const data = fs.readFileSync(HISTORY_FILE, 'utf8');
|
||||
this.history = JSON.parse(data);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load input history:', error);
|
||||
this.history = [];
|
||||
}
|
||||
}
|
||||
|
||||
private saveHistory(): void {
|
||||
try {
|
||||
const dir = path.dirname(HISTORY_FILE);
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
|
||||
fs.writeFileSync(HISTORY_FILE, JSON.stringify(this.history, null, 2));
|
||||
} catch (error) {
|
||||
console.error('Failed to save input history:', error);
|
||||
}
|
||||
}
|
||||
|
||||
addEntry(text: string): void {
|
||||
if (!text.trim()) return;
|
||||
|
||||
// Remove duplicate if it exists
|
||||
this.history = this.history.filter(entry => entry.text !== text);
|
||||
|
||||
// Add new entry at the beginning
|
||||
this.history.unshift({
|
||||
text,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
// Limit history size
|
||||
if (this.history.length > MAX_HISTORY_SIZE) {
|
||||
this.history = this.history.slice(0, MAX_HISTORY_SIZE);
|
||||
}
|
||||
|
||||
this.saveHistory();
|
||||
this.resetNavigation();
|
||||
}
|
||||
|
||||
navigateUp(currentInput: string): string | null {
|
||||
if (this.history.length === 0) return null;
|
||||
|
||||
// Store original input on first navigation
|
||||
if (this.currentIndex === -1) {
|
||||
this.originalInput = currentInput;
|
||||
this.currentIndex = 0;
|
||||
} else if (this.currentIndex < this.history.length - 1) {
|
||||
this.currentIndex++;
|
||||
}
|
||||
|
||||
return this.history[this.currentIndex]?.text || null;
|
||||
}
|
||||
|
||||
navigateDown(currentInput: string): string | null {
|
||||
if (this.currentIndex === -1) return null;
|
||||
|
||||
if (this.currentIndex > 0) {
|
||||
this.currentIndex--;
|
||||
return this.history[this.currentIndex]?.text || null;
|
||||
} else {
|
||||
// Return to original input
|
||||
this.resetNavigation();
|
||||
return this.originalInput;
|
||||
}
|
||||
}
|
||||
|
||||
resetNavigation(): void {
|
||||
this.currentIndex = -1;
|
||||
this.originalInput = '';
|
||||
}
|
||||
|
||||
getHistory(): InputHistoryEntry[] {
|
||||
return [...this.history];
|
||||
}
|
||||
|
||||
clearHistory(): void {
|
||||
this.history = [];
|
||||
this.resetNavigation();
|
||||
this.saveHistory();
|
||||
}
|
||||
}
|
||||
46
test-history.js
Normal file
46
test-history.js
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import { InputHistory } from './dist/util/inputHistory.js';
|
||||
|
||||
// Test the input history functionality
|
||||
console.log('Testing input history functionality...');
|
||||
|
||||
const history = new InputHistory();
|
||||
|
||||
// Test adding entries
|
||||
history.addEntry('first command');
|
||||
history.addEntry('second command');
|
||||
history.addEntry('third command');
|
||||
|
||||
console.log('Added 3 entries to history');
|
||||
|
||||
// Test navigation
|
||||
console.log('Navigation test:');
|
||||
let result = history.navigateUp('');
|
||||
console.log('Up 1:', result); // Should be 'third command'
|
||||
|
||||
result = history.navigateUp('');
|
||||
console.log('Up 2:', result); // Should be 'second command'
|
||||
|
||||
result = history.navigateUp('');
|
||||
console.log('Up 3:', result); // Should be 'first command'
|
||||
|
||||
result = history.navigateUp('');
|
||||
console.log('Up 4 (at limit):', result); // Should still be 'first command'
|
||||
|
||||
result = history.navigateDown('');
|
||||
console.log('Down 1:', result); // Should be 'second command'
|
||||
|
||||
result = history.navigateDown('');
|
||||
console.log('Down 2:', result); // Should be 'third command'
|
||||
|
||||
result = history.navigateDown('');
|
||||
console.log('Down 3 (back to original):', result); // Should be '' (original input)
|
||||
|
||||
// Test with current input
|
||||
history.resetNavigation();
|
||||
result = history.navigateUp('current input');
|
||||
console.log('Up with current input:', result); // Should be 'third command'
|
||||
|
||||
result = history.navigateDown('current input');
|
||||
console.log('Down to original:', result); // Should be 'current input'
|
||||
|
||||
console.log('All tests passed!');
|
||||
Loading…
Add table
Reference in a new issue