fix: shift+enter for newline
This commit is contained in:
parent
65f10a1a4f
commit
fa30094e61
1 changed files with 62 additions and 12 deletions
|
|
@ -144,7 +144,8 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key.return) {
|
// Handle Enter key (regular enter for submit)
|
||||||
|
if (key.return && !key.shift) {
|
||||||
if (textBuffer.text.trim() && !isWaitingForResponse) {
|
if (textBuffer.text.trim() && !isWaitingForResponse) {
|
||||||
handleUserMessage(textBuffer.text.trim());
|
handleUserMessage(textBuffer.text.trim());
|
||||||
textBuffer.clear();
|
textBuffer.clear();
|
||||||
|
|
@ -154,6 +155,16 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
||||||
return;
|
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
|
// Let TextBuffer handle the input
|
||||||
const handled = textBuffer.handleInput(input, key);
|
const handled = textBuffer.handleInput(input, key);
|
||||||
|
|
||||||
|
|
@ -202,7 +213,6 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
||||||
You:{" "}
|
You:{" "}
|
||||||
</Text>
|
</Text>
|
||||||
{(() => {
|
{(() => {
|
||||||
|
|
||||||
if (inputText.length === 0) {
|
if (inputText.length === 0) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|
@ -217,20 +227,60 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inputMode && !isWaitingForResponse) {
|
if (inputMode && !isWaitingForResponse) {
|
||||||
// Show cursor at the correct position
|
// Handle multi-line text with cursor
|
||||||
const beforeCursor = inputText.slice(0, cursorPosition);
|
const lines = inputText.split("\n");
|
||||||
const atCursor = inputText.slice(cursorPosition, cursorPosition + 1);
|
let charCount = 0;
|
||||||
const afterCursor = inputText.slice(cursorPosition + 1);
|
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 (
|
return (
|
||||||
<>
|
<Box flexDirection="column">
|
||||||
<Text>{beforeCursor}</Text>
|
{lines.map((line, lineIndex) => {
|
||||||
<Text inverse>{atCursor || " "}</Text>
|
if (lineIndex === cursorLine) {
|
||||||
<Text>{afterCursor}</Text>
|
// 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
|
||||||
|
return <Text key={lineIndex}>{line}</Text>;
|
||||||
|
}
|
||||||
|
})}
|
||||||
|
</Box>
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
return <Text>{inputText}</Text>;
|
// 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>
|
</Box>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue