@@ -8,7 +8,17 @@ import { kittyEncode } from "./test-helpers.js";
88
99 const ENTER = kittyEncode("enter");
1010 const BACKSPACE = kittyEncode("backspace");
11+const CTRL_A = kittyEncode("ctrl+a");
12+const CTRL_E = kittyEncode("ctrl+e");
13+const CTRL_LEFT = kittyEncode("ctrl+left");
14+const CTRL_RIGHT = kittyEncode("ctrl+right");
1115 const CTRL_S = kittyEncode("ctrl+s");
16+const CTRL_W = kittyEncode("ctrl+w");
17+const ALT_B = kittyEncode("alt+b");
18+const ALT_D = kittyEncode("alt+d");
19+const ALT_F = kittyEncode("alt+f");
20+const UP = kittyEncode("up");
21+const DOWN = kittyEncode("down");
1222 const LEFT = kittyEncode("left");
1323 const LEGACY_CTRL_R = "\x12";
1424 const LEGACY_ALT_X = "\x1bx";
@@ -290,6 +300,221 @@ describe("TextInput integration", () => {
290300 expect(value).toBe("ab");
291301 });
292302
303+ test("ctrl+a and ctrl+e move to the start and end of the value", async () => {
304+ const term = setup(20, 3);
305+ let value = "hello";
306+ const onChange = (v: string) => {
307+ value = v;
308+ };
309+ cel.viewport(() =>
310+ VStack({}, [
311+ TextInput({
312+ value,
313+ focused: true,
314+ onChange,
315+ }),
316+ ]),
317+ );
318+ await waitForRender();
319+
320+ term.sendInput(CTRL_A);
321+ await waitForRender();
322+ term.sendInput("X");
323+ await waitForRender();
324+
325+ expect(value).toBe("Xhello");
326+
327+ term.sendInput(CTRL_E);
328+ await waitForRender();
329+ term.sendInput("!");
330+ await waitForRender();
331+
332+ expect(value).toBe("Xhello!");
333+ });
334+
335+ test("alt+b and alt+f move by whitespace-delimited words", async () => {
336+ const term = setup(20, 3);
337+ let value = "hello brave world";
338+ const onChange = (v: string) => {
339+ value = v;
340+ };
341+ cel.viewport(() =>
342+ VStack({}, [
343+ TextInput({
344+ value,
345+ focused: true,
346+ onChange,
347+ }),
348+ ]),
349+ );
350+ await waitForRender();
351+
352+ term.sendInput(ALT_B);
353+ await waitForRender();
354+ term.sendInput("!");
355+ await waitForRender();
356+
357+ expect(value).toBe("hello brave !world");
358+
359+ term.sendInput(CTRL_A);
360+ await waitForRender();
361+ term.sendInput(ALT_F);
362+ await waitForRender();
363+ term.sendInput("?");
364+ await waitForRender();
365+
366+ expect(value).toBe("hello? brave !world");
367+ });
368+
369+ test("ctrl+left and ctrl+right move by whitespace-delimited words", async () => {
370+ const term = setup(20, 3);
371+ let value = "hello brave world";
372+ const onChange = (v: string) => {
373+ value = v;
374+ };
375+ cel.viewport(() =>
376+ VStack({}, [
377+ TextInput({
378+ value,
379+ focused: true,
380+ onChange,
381+ }),
382+ ]),
383+ );
384+ await waitForRender();
385+
386+ term.sendInput(CTRL_LEFT);
387+ await waitForRender();
388+ term.sendInput("!");
389+ await waitForRender();
390+
391+ expect(value).toBe("hello brave !world");
392+
393+ term.sendInput(CTRL_A);
394+ await waitForRender();
395+ term.sendInput(CTRL_RIGHT);
396+ await waitForRender();
397+ term.sendInput("?");
398+ await waitForRender();
399+
400+ expect(value).toBe("hello? brave !world");
401+ });
402+
403+ test("ctrl+w and alt+d delete by whitespace-delimited words", async () => {
404+ const term = setup(20, 3);
405+ let value = "hello brave world";
406+ const onChange = (v: string) => {
407+ value = v;
408+ };
409+ cel.viewport(() =>
410+ VStack({}, [
411+ TextInput({
412+ value,
413+ focused: true,
414+ onChange,
415+ }),
416+ ]),
417+ );
418+ await waitForRender();
419+
420+ term.sendInput(CTRL_W);
421+ await waitForRender();
422+
423+ expect(value).toBe("hello brave ");
424+
425+ term.sendInput(CTRL_A);
426+ await waitForRender();
427+ term.sendInput(ALT_D);
428+ await waitForRender();
429+
430+ expect(value).toBe(" brave ");
431+ });
432+
433+ test("up follows visual wrapped lines in TextInput", async () => {
434+ const term = setup(20, 3);
435+ let value = "foo bar baz";
436+ const onChange = (v: string) => {
437+ value = v;
438+ };
439+ cel.viewport(() =>
440+ VStack({}, [
441+ TextInput({
442+ width: 6,
443+ value,
444+ focused: true,
445+ onChange,
446+ }),
447+ ]),
448+ );
449+ await waitForRender();
450+
451+ term.sendInput(UP);
452+ await waitForRender();
453+ term.sendInput("!");
454+ await waitForRender();
455+
456+ expect(value).toBe("foo bar! baz");
457+ });
458+
459+ test("down follows visual wrapped lines in TextInput", async () => {
460+ const term = setup(20, 3);
461+ let value = "foo bar baz";
462+ const onChange = (v: string) => {
463+ value = v;
464+ };
465+ cel.viewport(() =>
466+ VStack({}, [
467+ TextInput({
468+ width: 6,
469+ value,
470+ focused: true,
471+ onChange,
472+ }),
473+ ]),
474+ );
475+ await waitForRender();
476+
477+ term.sendInput(CTRL_A);
478+ await waitForRender();
479+ term.sendInput(DOWN);
480+ await waitForRender();
481+ term.sendInput("?");
482+ await waitForRender();
483+
484+ expect(value).toBe("foo ?bar baz");
485+ });
486+
487+ test("TextInput consumes editing modifier shortcuts instead of bubbling them", async () => {
488+ const term = setup(20, 3);
489+ let parentKey = "";
490+ let value = "hello brave world";
491+ cel.viewport(() =>
492+ VStack(
493+ {
494+ onKeyPress: (key) => {
495+ parentKey = key;
496+ },
497+ },
498+ [
499+ TextInput({
500+ value,
501+ focused: true,
502+ onChange: (v) => {
503+ value = v;
504+ },
505+ }),
506+ ],
507+ ),
508+ );
509+ await waitForRender();
510+
511+ term.sendInput(CTRL_W);
512+ await waitForRender();
513+
514+ expect(value).toBe("hello brave ");
515+ expect(parentKey).toBe("");
516+ });
517+
293518 test("enter inserts newline by default", async () => {
294519 const term = setup(20, 3);
295520 let value = "ab";