From aec6c0861e8244cdaefda14840844e922705c8fa Mon Sep 17 00:00:00 2001 From: DeWitt Clinton Date: Wed, 14 May 2025 17:33:37 -0700 Subject: Add readline-like keybindings to the input prompts. (#354) New keybindings in the main input prompt (when auto-suggestions are not active): - `Ctrl+L`: Clears the entire screen. - `Ctrl+A`: Moves the cursor to the beginning of the current input line. - `Ctrl+E`: Moves the cursor to the end of the current input line. - `Ctrl+P`: Navigates to the previous command in the input history. - `Ctrl+N`: Navigates to the next command in the input history. In the multiline text editor (e.g., when editing a previous message): - `Ctrl+K`: Deletes text from the current cursor position to the end of the line ("kill line right"). --- .../cli/src/ui/components/shared/text-buffer.ts | 60 +++++++++++++++++++++- 1 file changed, 58 insertions(+), 2 deletions(-) (limited to 'packages/cli/src/ui/components/shared/text-buffer.ts') diff --git a/packages/cli/src/ui/components/shared/text-buffer.ts b/packages/cli/src/ui/components/shared/text-buffer.ts index f2cb1ae2..661df70c 100644 --- a/packages/cli/src/ui/components/shared/text-buffer.ts +++ b/packages/cli/src/ui/components/shared/text-buffer.ts @@ -372,9 +372,9 @@ export function useTextBuffer({ pushUndo, cursorRow, cursorCol, - lines, currentLine, currentLineLen, + lines.length, setPreferredCol, ]); @@ -521,12 +521,58 @@ export function useTextBuffer({ pushUndo, cursorRow, cursorCol, - lines, currentLine, del, + lines.length, setPreferredCol, ]); + const killLineRight = useCallback((): void => { + const lineContent = currentLine(cursorRow); + if (cursorCol < currentLineLen(cursorRow)) { + // Cursor is before the end of the line's content, delete text to the right + pushUndo(); + setLines((prevLines) => { + const newLines = [...prevLines]; + newLines[cursorRow] = cpSlice(lineContent, 0, cursorCol); + return newLines; + }); + // Cursor position and preferredCol do not change in this case + } else if ( + cursorCol === currentLineLen(cursorRow) && + cursorRow < lines.length - 1 + ) { + // Cursor is at the end of the line's content (or line is empty), + // and it's not the last line. Delete the newline. + // `del()` handles pushUndo and setPreferredCol. + del(); + } + // If cursor is at the end of the line and it's the last line, do nothing. + }, [ + pushUndo, + cursorRow, + cursorCol, + currentLine, + currentLineLen, + lines.length, + del, + ]); + + const killLineLeft = useCallback((): void => { + const lineContent = currentLine(cursorRow); + // Only act if the cursor is not at the beginning of the line + if (cursorCol > 0) { + pushUndo(); + setLines((prevLines) => { + const newLines = [...prevLines]; + newLines[cursorRow] = cpSlice(lineContent, cursorCol); + return newLines; + }); + setCursorCol(0); + setPreferredCol(null); + } + }, [pushUndo, cursorRow, cursorCol, currentLine, setPreferredCol]); + const move = useCallback( (dir: Direction): void => { const before = [cursorRow, cursorCol]; @@ -772,6 +818,8 @@ export function useTextBuffer({ replaceRange, deleteWordLeft, deleteWordRight, + killLineRight, + killLineLeft, handleInput, openInExternalEditor, @@ -872,6 +920,14 @@ export interface TextBuffer { * follows the caret and the next contiguous run of word characters. */ deleteWordRight: () => void; + /** + * Deletes text from the cursor to the end of the current line. + */ + killLineRight: () => void; + /** + * Deletes text from the start of the current line to the cursor. + */ + killLineLeft: () => void; /** * High level "handleInput" – receives what Ink gives us. */ -- cgit v1.2.3