refactor: UserInput.tsx

This commit is contained in:
Nate 2025-07-13 16:29:28 -07:00
parent a08e2176a7
commit 0c179681bc
2 changed files with 152 additions and 122 deletions

View file

@ -1,10 +1,10 @@
import { ContinueClient } from "@continuedev/sdk";
import { Box, Text, useApp, useInput } from "ink";
import { Box, Text, useApp } from "ink";
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";
import UserInput from "./UserInput.js";
interface TUIChatProps {
assistant: ContinueClient["assistant"];
@ -34,9 +34,6 @@ const TUIChat: React.FC<TUIChatProps> = ({
}
);
const [textBuffer] = useState(() => new TextBuffer());
const [inputText, setInputText] = useState("");
const [cursorPosition, setCursorPosition] = useState(0);
const [isWaitingForResponse, setIsWaitingForResponse] = useState(false);
const [messages, setMessages] = useState<DisplayMessage[]>([]);
const [inputMode, setInputMode] = useState(true);
@ -134,46 +131,6 @@ const TUIChat: React.FC<TUIChatProps> = ({
}
};
useInput((input, key) => {
if (key.ctrl && (input === "c" || input === "d")) {
exit();
return;
}
if (!inputMode) {
return;
}
// Handle Enter key (regular enter for submit)
if (key.return && !key.shift) {
if (textBuffer.text.trim() && !isWaitingForResponse) {
handleUserMessage(textBuffer.text.trim());
textBuffer.clear();
setInputText("");
setCursorPosition(0);
}
return;
}
// Handle Shift+Enter (carriage return character) for new line
if (input === "\r" || input === "\\\r") {
const handled = textBuffer.handleInput("\n", key);
if (handled) {
setInputText(textBuffer.text);
setCursorPosition(textBuffer.cursor);
}
return;
}
// Let TextBuffer handle the input
const handled = textBuffer.handleInput(input, key);
// Update React state to trigger re-render
if (handled) {
setInputText(textBuffer.text);
setCursorPosition(textBuffer.cursor);
}
});
const renderMessage = (message: DisplayMessage, index: number) => {
const isUser = message.role === "user";
@ -207,83 +164,12 @@ const TUIChat: React.FC<TUIChatProps> = ({
{messages.map(renderMessage)}
</Box>
{/* Input area with rounded corners */}
<Box borderStyle="round" borderTop={true} paddingX={1}>
<Text color="green" bold>
●{" "}
</Text>
{(() => {
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) {
// Handle multi-line text with cursor
const lines = inputText.split("\n");
let charCount = 0;
let cursorLine = 0;
let cursorCol = 0;
// Find which line and column the cursor is on
for (let i = 0; i < lines.length; i++) {
if (cursorPosition <= charCount + lines[i].length) {
cursorLine = i;
cursorCol = cursorPosition - charCount;
break;
}
charCount += lines[i].length + 1; // +1 for the newline character
}
// Handle cursor at the very end
if (cursorPosition >= inputText.length) {
cursorLine = lines.length - 1;
cursorCol = lines[cursorLine].length;
}
return (
<Box flexDirection="column">
{lines.map((line, lineIndex) => {
if (lineIndex === cursorLine) {
// Line with cursor
const beforeCursor = line.slice(0, cursorCol);
const atCursor = line.slice(cursorCol, cursorCol + 1);
const afterCursor = line.slice(cursorCol + 1);
return (
<Text key={lineIndex}>
{beforeCursor}
<Text inverse>{atCursor || " "}</Text>
{afterCursor}
</Text>
);
} else {
// Regular line - ensure empty lines still render
return <Text key={lineIndex}>{line || " "}</Text>;
}
})}
</Box>
);
} else {
// Not in input mode, just display the text
return (
<Box flexDirection="column">
{inputText.split("\n").map((line, index) => (
<Text key={index}>{line || " "}</Text>
))}
</Box>
);
}
})()}
</Box>
{/* Input area */}
<UserInput
onSubmit={handleUserMessage}
isWaitingForResponse={isWaitingForResponse}
inputMode={inputMode}
/>
{/* Status */}
{isWaitingForResponse && (

144
src/ui/UserInput.tsx Normal file
View file

@ -0,0 +1,144 @@
import { Box, Text, useApp, useInput } from "ink";
import React, { useState } from "react";
import { TextBuffer } from "./TextBuffer.js";
interface UserInputProps {
onSubmit: (message: string) => void;
isWaitingForResponse: boolean;
inputMode: boolean;
}
const UserInput: React.FC<UserInputProps> = ({
onSubmit,
isWaitingForResponse,
inputMode,
}) => {
const [textBuffer] = useState(() => new TextBuffer());
const [inputText, setInputText] = useState("");
const [cursorPosition, setCursorPosition] = useState(0);
const { exit } = useApp();
useInput((input, key) => {
if (key.ctrl && (input === "c" || input === "d")) {
exit();
return;
}
if (!inputMode) {
return;
}
// Handle Enter key (regular enter for submit)
if (key.return && !key.shift) {
if (textBuffer.text.trim() && !isWaitingForResponse) {
onSubmit(textBuffer.text.trim());
textBuffer.clear();
setInputText("");
setCursorPosition(0);
}
return;
}
// Handle Shift+Enter (carriage return character) for new line
if (input === "\r" || input === "\\\r") {
const handled = textBuffer.handleInput("\n", key);
if (handled) {
setInputText(textBuffer.text);
setCursorPosition(textBuffer.cursor);
}
return;
}
// Let TextBuffer handle the input
const handled = textBuffer.handleInput(input, key);
// Update React state to trigger re-render
if (handled) {
setInputText(textBuffer.text);
setCursorPosition(textBuffer.cursor);
}
});
const renderInputText = () => {
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) {
// Handle multi-line text with cursor
const lines = inputText.split("\n");
let charCount = 0;
let cursorLine = 0;
let cursorCol = 0;
// Find which line and column the cursor is on
for (let i = 0; i < lines.length; i++) {
if (cursorPosition <= charCount + lines[i].length) {
cursorLine = i;
cursorCol = cursorPosition - charCount;
break;
}
charCount += lines[i].length + 1; // +1 for the newline character
}
// Handle cursor at the very end
if (cursorPosition >= inputText.length) {
cursorLine = lines.length - 1;
cursorCol = lines[cursorLine].length;
}
return (
<Box flexDirection="column">
{lines.map((line, lineIndex) => {
if (lineIndex === cursorLine) {
// Line with cursor
const beforeCursor = line.slice(0, cursorCol);
const atCursor = line.slice(cursorCol, cursorCol + 1);
const afterCursor = line.slice(cursorCol + 1);
return (
<Text key={lineIndex}>
{beforeCursor}
<Text inverse>{atCursor || " "}</Text>
{afterCursor}
</Text>
);
} else {
// Regular line - ensure empty lines still render
return <Text key={lineIndex}>{line || " "}</Text>;
}
})}
</Box>
);
} else {
// Not in input mode, just display the text
return (
<Box flexDirection="column">
{inputText.split("\n").map((line, index) => (
<Text key={index}>{line || " "}</Text>
))}
</Box>
);
}
};
return (
<Box borderStyle="round" borderTop={true} paddingX={1}>
<Text color="green" bold>
●{" "}
</Text>
{renderInputText()}
</Box>
);
};
export default UserInput;