From 4e9b7fbe529c4470259e8506bc9e6ad1c920218a Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 14 Jul 2025 00:57:50 -0700 Subject: [PATCH] Add input history navigation with up/down arrow keys --- package-lock.json | 4 +- src/ui/UserInput.tsx | 34 +++++++++++- src/util/inputHistory.ts | 114 +++++++++++++++++++++++++++++++++++++++ test-history.js | 46 ++++++++++++++++ 4 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 src/util/inputHistory.ts create mode 100644 test-history.js diff --git a/package-lock.json b/package-lock.json index 3ea880e57..93e9eafa3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@continuedev/cli", - "version": "1.0.1", + "version": "1.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@continuedev/cli", - "version": "1.0.1", + "version": "1.1.0", "license": "Apache-2.0", "dependencies": { "@continuedev/openai-adapters": "^1.0.18", diff --git a/src/ui/UserInput.tsx b/src/ui/UserInput.tsx index d05a4c54c..405d2c3c0 100644 --- a/src/ui/UserInput.tsx +++ b/src/ui/UserInput.tsx @@ -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 = ({ 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); @@ -167,10 +169,37 @@ const UserInput: React.FC = ({ } } + // 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); @@ -202,6 +231,9 @@ const UserInput: React.FC = ({ setInputText(newText); setCursorPosition(newCursor); updateSlashCommandState(newText, newCursor); + + // Reset history navigation when user starts typing + inputHistory.resetNavigation(); } }); diff --git a/src/util/inputHistory.ts b/src/util/inputHistory.ts new file mode 100644 index 000000000..1ec172adc --- /dev/null +++ b/src/util/inputHistory.ts @@ -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(); + } +} \ No newline at end of file diff --git a/test-history.js b/test-history.js new file mode 100644 index 000000000..705dab39d --- /dev/null +++ b/test-history.js @@ -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!'); \ No newline at end of file