From e7ab301dbe3b7e27bd6bb15992f3cf1a3f9694d7 Mon Sep 17 00:00:00 2001 From: Nate Date: Sun, 13 Jul 2025 14:43:15 -0700 Subject: [PATCH] feat: new TextBuffer class for improved keystroke handling --- package.json | 3 +- src/ui/TUIChat.tsx | 59 +++++++--- src/ui/TextBuffer.ts | 249 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 297 insertions(+), 14 deletions(-) create mode 100644 src/ui/TextBuffer.ts diff --git a/package.json b/package.json index 516d8da07..d39fc1f71 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "build": "tsc", "start": "node dist/index.js", "repomix:sdk": "npx repomix node_modules/@continuedev/sdk --ignore **/api,cli-repomix.xml -o sdk-repomix.xml", - "repomix:cli": "npx repomix --ignore sdk-repomix.xml -o cli-repomix.xml" + "repomix:cli": "npx repomix --ignore sdk-repomix.xml -o cli-repomix.xml", + "lint": "tsc --noEmit" }, "author": "Continue Dev, Inc.", "license": "Apache-2.0", diff --git a/src/ui/TUIChat.tsx b/src/ui/TUIChat.tsx index 81c4a51c2..814797d4e 100644 --- a/src/ui/TUIChat.tsx +++ b/src/ui/TUIChat.tsx @@ -4,6 +4,7 @@ import { ChatCompletionMessageParam } from "openai/resources.mjs"; import React, { useEffect, useState } from "react"; import { handleSlashCommands } from "../slashCommands.js"; import { streamChatResponse } from "../streamChatResponse.js"; +import { TextBuffer } from "./TextBuffer.js"; interface TUIChatProps { assistant: ContinueClient["assistant"]; @@ -33,7 +34,8 @@ const TUIChat: React.FC = ({ } ); - const [userInput, setUserInput] = useState(""); + const [textBuffer] = useState(() => new TextBuffer()); + const [inputText, setInputText] = useState(""); const [isWaitingForResponse, setIsWaitingForResponse] = useState(false); const [messages, setMessages] = useState([]); const [inputMode, setInputMode] = useState(true); @@ -142,21 +144,20 @@ const TUIChat: React.FC = ({ } if (key.return) { - if (userInput.trim() && !isWaitingForResponse) { - handleUserMessage(userInput.trim()); - setUserInput(""); + if (textBuffer.text.trim() && !isWaitingForResponse) { + handleUserMessage(textBuffer.text.trim()); + textBuffer.clear(); + setInputText(""); } return; } - if (key.backspace || key.delete) { - setUserInput((prev) => prev.slice(0, -1)); - return; - } + // Let TextBuffer handle the input + const handled = textBuffer.handleInput(input, key); - // Only add printable characters - if (input && input.length === 1 && !key.ctrl && !key.meta) { - setUserInput((prev) => prev + input); + // Update React state to trigger re-render + if (handled) { + setInputText(textBuffer.text); } }); @@ -197,8 +198,40 @@ const TUIChat: React.FC = ({ You:{" "} - {userInput} - {inputMode && !isWaitingForResponse && ▋} + {(() => { + const cursorPosition = textBuffer.cursor; + + if (inputText.length === 0) { + return ( + <> + {inputMode && !isWaitingForResponse && ( + ▋Type your message... + )} + {(!inputMode || isWaitingForResponse) && ( + Type your message... + )} + + ); + } + + if (inputMode && !isWaitingForResponse) { + // Show cursor at the correct position + const beforeCursor = inputText.slice(0, cursorPosition); + const atCursor = + inputText.slice(cursorPosition, cursorPosition + 1) || " "; + const afterCursor = inputText.slice(cursorPosition + 1); + + return ( + <> + {beforeCursor} + {atCursor} + {afterCursor} + + ); + } else { + return {inputText}; + } + })()} {/* Status */} diff --git a/src/ui/TextBuffer.ts b/src/ui/TextBuffer.ts new file mode 100644 index 000000000..7d890eea5 --- /dev/null +++ b/src/ui/TextBuffer.ts @@ -0,0 +1,249 @@ +import { Key } from 'ink'; + +export class TextBuffer { + private _text: string = ''; + private _cursor: number = 0; + + constructor(initialText: string = '') { + this._text = initialText; + this._cursor = initialText.length; + } + + get text(): string { + return this._text; + } + + get cursor(): number { + return this._cursor; + } + + setText(text: string): void { + this._text = text; + this._cursor = Math.min(this._cursor, text.length); + } + + setCursor(position: number): void { + this._cursor = Math.max(0, Math.min(position, this._text.length)); + } + + insertText(text: string): void { + this._text = this._text.slice(0, this._cursor) + text + this._text.slice(this._cursor); + this._cursor += text.length; + } + + deleteCharAt(position: number): void { + if (position >= 0 && position < this._text.length) { + this._text = this._text.slice(0, position) + this._text.slice(position + 1); + if (this._cursor > position) { + this._cursor--; + } + } + } + + deleteBackward(): void { + if (this._cursor > 0) { + this.deleteCharAt(this._cursor - 1); + } + } + + deleteForward(): void { + if (this._cursor < this._text.length) { + this.deleteCharAt(this._cursor); + } + } + + private findWordBoundary(position: number, direction: 'left' | 'right'): number { + if (direction === 'left') { + // Find start of current word or previous word + let pos = Math.max(0, position - 1); + + // Skip whitespace + while (pos > 0 && /\s/.test(this._text[pos])) { + pos--; + } + + // Find word boundary + while (pos > 0 && !/\s/.test(this._text[pos - 1])) { + pos--; + } + + return pos; + } else { + // Find end of current word or next word + let pos = position; + + // Skip whitespace + while (pos < this._text.length && /\s/.test(this._text[pos])) { + pos++; + } + + // Find word boundary + while (pos < this._text.length && !/\s/.test(this._text[pos])) { + pos++; + } + + return pos; + } + } + + moveWordLeft(): void { + this._cursor = this.findWordBoundary(this._cursor, 'left'); + } + + moveWordRight(): void { + this._cursor = this.findWordBoundary(this._cursor, 'right'); + } + + deleteWordBackward(): void { + const wordStart = this.findWordBoundary(this._cursor, 'left'); + if (wordStart < this._cursor) { + this._text = this._text.slice(0, wordStart) + this._text.slice(this._cursor); + this._cursor = wordStart; + } + } + + deleteWordForward(): void { + const wordEnd = this.findWordBoundary(this._cursor, 'right'); + if (wordEnd > this._cursor) { + this._text = this._text.slice(0, this._cursor) + this._text.slice(wordEnd); + } + } + + moveToStart(): void { + this._cursor = 0; + } + + moveToEnd(): void { + this._cursor = this._text.length; + } + + moveLeft(): void { + this._cursor = Math.max(0, this._cursor - 1); + } + + moveRight(): void { + this._cursor = Math.min(this._text.length, this._cursor + 1); + } + + clear(): void { + this._text = ''; + this._cursor = 0; + } + + handleInput(input: string, key: Key): boolean { + // Detect option key combinations through escape sequences + const isOptionKey = input.startsWith('\u001b') && input.length > 1; + + // Handle option key combinations (detected through escape sequences) + if (isOptionKey) { + const sequence = input.slice(1); + + // Option + left arrow (usually \u001b[1;9D or \u001bb) + if (sequence === 'b' || sequence.includes('1;9D')) { + this.moveWordLeft(); + return true; + } + + // Option + right arrow (usually \u001b[1;9C or \u001bf) + if (sequence === 'f' || sequence.includes('1;9C')) { + this.moveWordRight(); + return true; + } + + // Option + backspace (usually \u001b\u0008 or \u001b\u007f) + if (sequence === '\u0008' || sequence === '\u007f') { + this.deleteWordBackward(); + return true; + } + + return true; // Consume other option sequences + } + + // Handle special key combinations based on input character + if (key.ctrl) { + switch (input) { + case 'a': + this.moveToStart(); + return true; + case 'e': + this.moveToEnd(); + return true; + case 'u': + // Delete from cursor to start of line + this._text = this._text.slice(this._cursor); + this._cursor = 0; + return true; + case 'k': + // Delete from cursor to end of line + this._text = this._text.slice(0, this._cursor); + return true; + case 'w': + this.deleteWordBackward(); + return true; + case 'd': + this.deleteWordForward(); + return true; + } + } + + // Handle meta key combinations (cmd on Mac) + if (key.meta) { + if (key.backspace) { + this.deleteWordBackward(); + return true; + } + if (key.delete) { + this.deleteWordForward(); + return true; + } + if (key.leftArrow) { + this.moveWordLeft(); + return true; + } + if (key.rightArrow) { + this.moveWordRight(); + return true; + } + } + + // Handle arrow keys + if (key.leftArrow && !key.meta) { + this.moveLeft(); + return true; + } + if (key.rightArrow && !key.meta) { + this.moveRight(); + return true; + } + + // Handle backspace/delete + if (key.backspace && !key.meta) { + this.deleteBackward(); + return true; + } + if (key.delete && !key.meta) { + this.deleteForward(); + return true; + } + + // Handle regular character input + if (input && input.length === 1 && !key.ctrl && !key.meta && !isOptionKey) { + this.insertText(input); + return true; + } + + return false; + } + + // Helper method to render text with cursor + renderWithCursor(placeholder?: string): { text: string; cursorPosition: number } { + if (this._text.length === 0 && placeholder) { + return { text: placeholder, cursorPosition: 0 }; + } + + return { + text: this._text, + cursorPosition: this._cursor + }; + } +} \ No newline at end of file