From fa30094e61dbb2b11f7940b875fc719d06ed3eec Mon Sep 17 00:00:00 2001 From: Nate Date: Sun, 13 Jul 2025 16:15:49 -0700 Subject: [PATCH] fix: shift+enter for newline --- src/ui/TUIChat.tsx | 74 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 12 deletions(-) diff --git a/src/ui/TUIChat.tsx b/src/ui/TUIChat.tsx index ae027e17c..5940f6074 100644 --- a/src/ui/TUIChat.tsx +++ b/src/ui/TUIChat.tsx @@ -144,7 +144,8 @@ const TUIChat: React.FC = ({ return; } - if (key.return) { + // Handle Enter key (regular enter for submit) + if (key.return && !key.shift) { if (textBuffer.text.trim() && !isWaitingForResponse) { handleUserMessage(textBuffer.text.trim()); textBuffer.clear(); @@ -154,6 +155,16 @@ const TUIChat: React.FC = ({ 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); @@ -202,7 +213,6 @@ const TUIChat: React.FC = ({ You:{" "} {(() => { - if (inputText.length === 0) { return ( <> @@ -217,20 +227,60 @@ const TUIChat: React.FC = ({ } 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); + // 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 ( - <> - {beforeCursor} - {atCursor || " "} - {afterCursor} - + + {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 + return {line}; + } + })} + ); } else { - return {inputText}; + // Not in input mode, just display the text + return ( + + {inputText.split("\n").map((line, index) => ( + {line} + ))} + + ); } })()}