Skip to content

The agent that grows with you

fix(tui): text and a CSI-u Enter in one read clear the composer after submit

Tekniumf4383ac930cd
diff --git a/ui-tui/src/__tests__/textInputSubmitClear.test.tsx b/ui-tui/src/__tests__/textInputSubmitClear.test.tsx
index 358e3a55e53..81b0fde1c34 100644
--- a/ui-tui/src/__tests__/textInputSubmitClear.test.tsx
+++ b/ui-tui/src/__tests__/textInputSubmitClear.test.tsx
@@ -104,4 +104,53 @@ describe('TextInput submit clearing', () => {
expect(changes.at(-1)).toBe('x')
expect(changes).not.toContain(`${full}x`)
})
+
+ it('clears the composer when text and a CSI-u Enter arrive in one read', async () => {
+ const streams = makeStreams()
+ const changes: string[] = []
+ const submits: string[] = []
+
+ function Harness() {
+ const [value, setValue] = useState('')
+
+ return (
+ <TextInput
+ columns={80}
+ onChange={next => {
+ changes.push(next)
+ setValue(next)
+ }}
+ onSubmit={text => {
+ submits.push(text)
+ setValue('')
+ }}
+ value={value}
+ />
+ )
+ }
+
+ const instance = renderSync(React.createElement(Harness), {
+ patchConsole: false,
+ stderr: streams.stderr as NodeJS.WriteStream,
+ stdin: streams.stdin as unknown as NodeJS.ReadStream,
+ stdout: streams.stdout as NodeJS.WriteStream
+ })
+
+ await settle()
+
+ // Text and the Enter key share one read and one React batch, so the parent value ends
+ // where it started ('') and the draft only clears if TextInput re-syncs on submit.
+ streams.stdin.send('hello\x1b[13u')
+ await settle(25)
+
+ streams.stdin.send('x')
+ await settle(25)
+
+ instance.unmount()
+ instance.cleanup()
+
+ expect(submits).toEqual(['hello'])
+ expect(changes.at(-1)).toBe('x')
+ expect(changes).not.toContain('hellox')
+ })
})
diff --git a/ui-tui/src/components/textInput.tsx b/ui-tui/src/components/textInput.tsx
index daca6ae643f..f650e926fba 100644
--- a/ui-tui/src/components/textInput.tsx
+++ b/ui-tui/src/components/textInput.tsx
@@ -796,6 +796,11 @@ export function TextInput({
)
const [sel, setSel] = useState<null | { end: number; start: number }>(null)
+ // Bumped on every submit so the value-sync effect below re-runs even when the parent's value
+ // ends where it started: text + Return in ONE stdin read (`tmux send-keys 'text' Enter`) emits
+ // the text and the submit's clear in the same React batch, so `value` never visibly changes and
+ // the submitted text would stay in the composer.
+ const [submitSeq, setSubmitSeq] = useState(0)
const fwdDel = useFwdDelete(focus)
const termFocus = useTerminalFocus()
const { stdout } = useStdout()
@@ -958,7 +963,7 @@ export function TextInput({
lineWidthRef.current = stringWidth(value.includes('\n') ? value.slice(value.lastIndexOf('\n') + 1) : value)
undo.current = []
redo.current = []
- }, [value])
+ }, [value, submitSeq])
// The composer unmounts while full-screen monitors own input. Keep its
// insertion point with the shell, not with transient steer/secret inputs.
@@ -1455,6 +1460,7 @@ export function TextInput({
if (insertNewline) {
commit(ins(pending.value, pending.cursor, '\n'), pending.cursor + 1)
} else {
+ setSubmitSeq(n => n + 1)
cbSubmit.current?.(pending.value)
}