From 59a7da9a894943039bf8870859da5870ceb62f82 Mon Sep 17 00:00:00 2001 From: Brooklyn Nicholson Date: Thu, 30 Jul 2026 04:26:42 -0500 Subject: [PATCH] fix(tui): scope Ctrl+U / Ctrl+K to the current line, per readline MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both kills operated on the whole buffer: Ctrl+U wiped everything before the cursor and Ctrl+K everything after, regardless of newlines. Readline scopes them to the current logical line, and Claude Code documents Ctrl+U as "repeat to clear across lines in multiline input" — which only works if a press at a line boundary consumes the newline and makes progress. Extract killToLineStart / killToLineEnd and route all four call sites through them, so the Cmd chords and their Ctrl equivalents cannot drift. --- .../src/__tests__/textInputKillLine.test.ts | 62 +++++++++++++++++++ ui-tui/src/components/textInput.tsx | 39 ++++++++++-- 2 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 ui-tui/src/__tests__/textInputKillLine.test.ts diff --git a/ui-tui/src/__tests__/textInputKillLine.test.ts b/ui-tui/src/__tests__/textInputKillLine.test.ts new file mode 100644 index 00000000000..6c2f3614ff9 --- /dev/null +++ b/ui-tui/src/__tests__/textInputKillLine.test.ts @@ -0,0 +1,62 @@ +import { describe, expect, it } from 'vitest' + +import { killToLineEnd, killToLineStart } from '../components/textInput.js' + +// Ctrl+U / Ctrl+K are readline motions scoped to the *current logical line*, +// not the whole buffer. Claude Code documents Ctrl+U as "repeat to clear +// across lines in multiline input", which only works if a press at a line +// boundary consumes the newline and makes progress. + +describe('killToLineStart', () => { + it('clears the whole value in single-line input', () => { + expect(killToLineStart('hello world', 11)).toEqual({ cursor: 0, value: '' }) + }) + + it('keeps text after the cursor', () => { + expect(killToLineStart('hello world', 6)).toEqual({ cursor: 0, value: 'world' }) + }) + + it('only kills the current line, leaving earlier lines intact', () => { + expect(killToLineStart('one\ntwo', 7)).toEqual({ cursor: 4, value: 'one\n' }) + }) + + it('consumes the newline when already at a line start, so repeats progress', () => { + // Second press from the position the first press left us at. + expect(killToLineStart('one\n', 4)).toEqual({ cursor: 3, value: 'one' }) + }) + + it('repeated presses walk up a multiline draft to empty', () => { + let state = { cursor: 11, value: 'one\ntwo\nsix' } + const seen: string[] = [] + + for (let i = 0; i < 6 && state.value !== ''; i++) { + state = killToLineStart(state.value, state.cursor) + seen.push(state.value) + } + + expect(seen).toEqual(['one\ntwo\n', 'one\ntwo', 'one\n', 'one', '']) + expect(state).toEqual({ cursor: 0, value: '' }) + }) + + it('is a no-op at the very start of the buffer', () => { + expect(killToLineStart('abc', 0)).toEqual({ cursor: 0, value: 'abc' }) + }) +}) + +describe('killToLineEnd', () => { + it('kills to end of a single-line value', () => { + expect(killToLineEnd('hello world', 6)).toEqual({ cursor: 6, value: 'hello ' }) + }) + + it('stops at the newline, leaving later lines intact', () => { + expect(killToLineEnd('one\ntwo', 0)).toEqual({ cursor: 0, value: '\ntwo' }) + }) + + it('consumes the newline when already at a line end, joining the next line', () => { + expect(killToLineEnd('one\ntwo', 3)).toEqual({ cursor: 3, value: 'onetwo' }) + }) + + it('is a no-op at the very end of the buffer', () => { + expect(killToLineEnd('abc', 3)).toEqual({ cursor: 3, value: 'abc' }) + }) +}) diff --git a/ui-tui/src/components/textInput.tsx b/ui-tui/src/components/textInput.tsx index 6fafe03b57b..4f2f4cea47f 100644 --- a/ui-tui/src/components/textInput.tsx +++ b/ui-tui/src/components/textInput.tsx @@ -321,6 +321,35 @@ export function resolveCursorLayout(display: string, cur: number, curRefCurrent: return cursorLayout(display, curRefCurrent, columns) } +/** + * Readline `unix-line-discard` (Ctrl+U / Cmd+Backspace): kill backward to + * the start of the *current logical line*, not to the start of the whole + * buffer. In single-line input the two are identical; in multiline input + * they are not, and repeating the keystroke walks up one line at a time. + * + * When the cursor already sits at a line start, consume the preceding + * newline so a repeat press makes progress instead of wedging — this is + * what makes "repeat to clear across lines" work. + */ +export function killToLineStart(value: string, cursor: number): { value: string; cursor: number } { + const start = value.lastIndexOf('\n', Math.max(0, cursor - 1)) + 1 + const from = start === cursor && cursor > 0 ? start - 1 : start + + return { value: value.slice(0, from) + value.slice(cursor), cursor: from } +} + +/** + * Readline `kill-line` (Ctrl+K / Cmd+ForwardDelete): kill forward to the + * end of the current logical line. At a line end, consume the newline so a + * repeat press joins the next line rather than doing nothing. + */ +export function killToLineEnd(value: string, cursor: number): { value: string; cursor: number } { + const nl = value.indexOf('\n', cursor) + const to = nl < 0 ? value.length : nl === cursor ? nl + 1 : nl + + return { value: value.slice(0, cursor) + value.slice(to), cursor } +} + /** * True when a Backspace / ForwardDelete keystroke should kill to the line * boundary rather than delete a single word. @@ -1219,8 +1248,7 @@ export function TextInput({ if (isLineKillModifier(k)) { // Cmd+Backspace — kill backward to start of line, matching the // Ctrl+U (unix-line-discard) path below. - v = v.slice(c) - c = 0 + ;({ cursor: c, value: v } = killToLineStart(v, c)) } else if (wordMod) { const t = wordLeft(v, c) v = v.slice(0, t) + v.slice(c) @@ -1247,7 +1275,7 @@ export function TextInput({ } else if (delFwd && c < v.length) { if (isLineKillModifier(k)) { // Cmd+ForwardDelete — kill to end of line, matching Ctrl+K. - v = v.slice(0, c) + ;({ cursor: c, value: v } = killToLineEnd(v, c)) } else if (wordMod) { const t = wordRight(v, c) v = v.slice(0, c) + v.slice(t) @@ -1271,15 +1299,14 @@ export function TextInput({ v = v.slice(0, range.start) + v.slice(range.end) c = range.start } else { - v = v.slice(c) - c = 0 + ;({ cursor: c, value: v } = killToLineStart(v, c)) } } else if (actionKillToEnd) { if (range) { v = v.slice(0, range.start) + v.slice(range.end) c = range.start } else { - v = v.slice(0, c) + ;({ cursor: c, value: v } = killToLineEnd(v, c)) } } else if (event.keypress.isPasted || inp.length > 0) { const bracketed = event.keypress.isPasted || inp.includes('[200~')