From 0c179681bcba741f5e8ba58467589fe321b1bec9 Mon Sep 17 00:00:00 2001 From: Nate Date: Sun, 13 Jul 2025 16:29:28 -0700 Subject: [PATCH] refactor: UserInput.tsx --- src/ui/TUIChat.tsx | 130 +++----------------------------------- src/ui/UserInput.tsx | 144 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+), 122 deletions(-) create mode 100644 src/ui/UserInput.tsx diff --git a/src/ui/TUIChat.tsx b/src/ui/TUIChat.tsx index d25c8b939..f505d747e 100644 --- a/src/ui/TUIChat.tsx +++ b/src/ui/TUIChat.tsx @@ -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 = ({ } ); - const [textBuffer] = useState(() => new TextBuffer()); - const [inputText, setInputText] = useState(""); - const [cursorPosition, setCursorPosition] = useState(0); const [isWaitingForResponse, setIsWaitingForResponse] = useState(false); const [messages, setMessages] = useState([]); const [inputMode, setInputMode] = useState(true); @@ -134,46 +131,6 @@ const TUIChat: React.FC = ({ } }; - 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 = ({ {messages.map(renderMessage)} - {/* Input area with rounded corners */} - - - ●{" "} - - {(() => { - if (inputText.length === 0) { - return ( - <> - {inputMode && !isWaitingForResponse && ( - ▋Type your message... - )} - {(!inputMode || isWaitingForResponse) && ( - Type your message... - )} - - ); - } - - 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 ( - - {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 ( - - {beforeCursor} - {atCursor || " "} - {afterCursor} - - ); - } else { - // Regular line - ensure empty lines still render - return {line || " "}; - } - })} - - ); - } else { - // Not in input mode, just display the text - return ( - - {inputText.split("\n").map((line, index) => ( - {line || " "} - ))} - - ); - } - })()} - + {/* Input area */} + {/* Status */} {isWaitingForResponse && ( diff --git a/src/ui/UserInput.tsx b/src/ui/UserInput.tsx new file mode 100644 index 000000000..00496634c --- /dev/null +++ b/src/ui/UserInput.tsx @@ -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 = ({ + 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 && ( + ▋Type your message... + )} + {(!inputMode || isWaitingForResponse) && ( + Type your message... + )} + + ); + } + + 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 ( + + {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 ( + + {beforeCursor} + {atCursor || " "} + {afterCursor} + + ); + } else { + // Regular line - ensure empty lines still render + return {line || " "}; + } + })} + + ); + } else { + // Not in input mode, just display the text + return ( + + {inputText.split("\n").map((line, index) => ( + {line || " "} + ))} + + ); + } + }; + + return ( + + + ●{" "} + + {renderInputText()} + + ); +}; + +export default UserInput; \ No newline at end of file