feat: new TextBuffer class for improved keystroke handling
This commit is contained in:
parent
8c0fd5e87e
commit
e7ab301dbe
3 changed files with 297 additions and 14 deletions
|
|
@ -13,7 +13,8 @@
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"start": "node dist/index.js",
|
"start": "node dist/index.js",
|
||||||
"repomix:sdk": "npx repomix node_modules/@continuedev/sdk --ignore **/api,cli-repomix.xml -o sdk-repomix.xml",
|
"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.",
|
"author": "Continue Dev, Inc.",
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import { ChatCompletionMessageParam } from "openai/resources.mjs";
|
||||||
import React, { useEffect, useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import { handleSlashCommands } from "../slashCommands.js";
|
import { handleSlashCommands } from "../slashCommands.js";
|
||||||
import { streamChatResponse } from "../streamChatResponse.js";
|
import { streamChatResponse } from "../streamChatResponse.js";
|
||||||
|
import { TextBuffer } from "./TextBuffer.js";
|
||||||
|
|
||||||
interface TUIChatProps {
|
interface TUIChatProps {
|
||||||
assistant: ContinueClient["assistant"];
|
assistant: ContinueClient["assistant"];
|
||||||
|
|
@ -33,7 +34,8 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const [userInput, setUserInput] = useState("");
|
const [textBuffer] = useState(() => new TextBuffer());
|
||||||
|
const [inputText, setInputText] = useState("");
|
||||||
const [isWaitingForResponse, setIsWaitingForResponse] = useState(false);
|
const [isWaitingForResponse, setIsWaitingForResponse] = useState(false);
|
||||||
const [messages, setMessages] = useState<DisplayMessage[]>([]);
|
const [messages, setMessages] = useState<DisplayMessage[]>([]);
|
||||||
const [inputMode, setInputMode] = useState(true);
|
const [inputMode, setInputMode] = useState(true);
|
||||||
|
|
@ -142,21 +144,20 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key.return) {
|
if (key.return) {
|
||||||
if (userInput.trim() && !isWaitingForResponse) {
|
if (textBuffer.text.trim() && !isWaitingForResponse) {
|
||||||
handleUserMessage(userInput.trim());
|
handleUserMessage(textBuffer.text.trim());
|
||||||
setUserInput("");
|
textBuffer.clear();
|
||||||
|
setInputText("");
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key.backspace || key.delete) {
|
// Let TextBuffer handle the input
|
||||||
setUserInput((prev) => prev.slice(0, -1));
|
const handled = textBuffer.handleInput(input, key);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only add printable characters
|
// Update React state to trigger re-render
|
||||||
if (input && input.length === 1 && !key.ctrl && !key.meta) {
|
if (handled) {
|
||||||
setUserInput((prev) => prev + input);
|
setInputText(textBuffer.text);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -197,8 +198,40 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
||||||
<Text color="green" bold>
|
<Text color="green" bold>
|
||||||
You:{" "}
|
You:{" "}
|
||||||
</Text>
|
</Text>
|
||||||
<Text>{userInput}</Text>
|
{(() => {
|
||||||
{inputMode && !isWaitingForResponse && <Text color="gray">▋</Text>}
|
const cursorPosition = textBuffer.cursor;
|
||||||
|
|
||||||
|
if (inputText.length === 0) {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{inputMode && !isWaitingForResponse && (
|
||||||
|
<Text color="gray">▋Type your message...</Text>
|
||||||
|
)}
|
||||||
|
{(!inputMode || isWaitingForResponse) && (
|
||||||
|
<Text color="gray">Type your message...</Text>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (
|
||||||
|
<>
|
||||||
|
<Text>{beforeCursor}</Text>
|
||||||
|
<Text inverse>{atCursor}</Text>
|
||||||
|
<Text>{afterCursor}</Text>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return <Text>{inputText}</Text>;
|
||||||
|
}
|
||||||
|
})()}
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
{/* Status */}
|
{/* Status */}
|
||||||
|
|
|
||||||
249
src/ui/TextBuffer.ts
Normal file
249
src/ui/TextBuffer.ts
Normal file
|
|
@ -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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue