From c22b57e13fec318ff4df96fb07b096ea53e31122 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 25 Nov 2026 05:18:36 -0300 Subject: tui, gui, test/process/window: Escape is the Escape key at once (walk UI P3 item 3, DESIGN D15, POLISH 16): MEASURED in a pty (.scratch/walkui/P3/esc/measure.py, the Debug tty on a legacy terminal, keys written as a terminal writes them, the body read over 9P after `i x Esc A y`): an Esc in a write of its own was taken at once (`xinitialy`); one that reached pardes glued after the key before it in one read (keys typed faster than read: a burst over a link) waited 50 ms for a sequence's rest, and `A` coming in those 50 ms joined it as Alt-A (`xyinitial`: Escape lost, insert mode kept); 100 ms later it was taken; with the kitty keyboard protocol Escape is `CSI 27 u`, never held; so a user of a legacy terminal (tmux, ssh) lost an Escape typed in a burst, never a lone one; THE CAUSE (dev/tui/tui.zig readInput): the ESC a wait saw more after was carried into the next read whatever that read held; THE FIX: carried so, it is the Escape key unless the next read goes on with a sequence's rest, its introducer (`[`, `O`, `]`, `P`, `_`, `^`, `\`) or another ESC: an ssh packet's cut after an ESC still joins (the test "terminal input split at every byte with a gap" unchanged), the key typed after Escape is its own; the 50 ms a glued ESC waits when nothing follows stays (its trade-off for those cuts); after it the pty measurement takes Escape in all three cases; AND the window's test stdin (dev/gui/gui.zig StdinFeed.pump, the screenshot kit's and the gui cases' keys, S's POLISH 16) held a read's trailing lone ESC as the possible start of its `ESC ]777;` events until the next write, with no timeout, so that write's first key came as Alt on it: a read that ends in a lone ESC ends in the Escape key now, as the tty's reader has a read of one; TESTS FIRST: tui.zig "an ESC that ends a read, a key read after it within the wait, is the Escape key and then that key" (on the base: Alt-A, `expected 27, found 65`), test/process/window.zig "a lone Esc written to its test stdin is the Escape key at once, the key written after it its own" (gui, run with the gui gates); shell-test 1281/1284 (3 skipped), core-test 1063/1233 (170 skipped) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE --- dev/tui/tui.zig | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'dev/tui') diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index 542ef59a..aa1f8849 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -179,6 +179,9 @@ fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void { var carried: usize = 0; // The sequence carried is past max_sequence, and said so. var dropping = false; + // The ESC the last read ended in, carried because more came while it + // waited for a sequence's rest. + var esc_held = false; while (true) { // stopInput cancels the task; the read then fails with Canceled if (carried == buf.len and buf.len < max_sequence) { if (gpa.realloc(buf, @min(2 * buf.len, max_sequence))) |grown| buf = grown else |_| {} @@ -227,6 +230,12 @@ fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void { if (scalar_len > end - (lead - 1)) parse_end = lead - 1; } var consumed: usize = 0; + // That ESC is the Escape key unless this read goes on with a + // sequence's rest (an ssh packet cut it after its ESC): its + // introducer, CSI's `[`, SS3's `O`, a string's, or another ESC. A + // key typed just after Escape is its own, not Alt on it. + const lone_esc = esc_held and end > 1 and buf[0] == 0x1b and std.mem.indexOfScalar(u8, "[O]P_^\\\x1b", buf[1]) == null; + esc_held = false; // A read that is one ESC and nothing else is the Escape key: a // terminal writes each key's sequence whole and ssh carries each of // its writes whole, so no sequence arrives cut right after its ESC. @@ -247,9 +256,12 @@ fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void { std.mem.startsWith(u8, FrameAck.answer, rest)) break; if (!waited and parse_end == end and rest[0] == 0x1b and (rest.len == 1 or (rest.len == 2 and rest[1] == 0x1b))) { waited = true; - if (try inputFollows(tty, loop.io, if (vx.caps.kitty_keyboard) 1000 else 50)) break; + if (try inputFollows(tty, loop.io, if (vx.caps.kitty_keyboard) 1000 else 50)) { + esc_held = rest.len == 1; + break; + } } - const result = try parser.parse(buf[consumed..parse_end], vx.opts.system_clipboard_allocator); + const result = try parser.parse(buf[consumed..if (lone_esc and consumed == 0) 1 else parse_end], vx.opts.system_clipboard_allocator); if (result.n == 0) break; consumed += result.n; // A modifier pressed on its own, which the kitty keyboard @@ -2668,6 +2680,29 @@ test "an ESC read by itself is the Escape key at once" { t.vx.caps.kitty_keyboard = false; } +test "an ESC that ends a read, a key read after it within the wait, is the Escape key and then that key" { + var t: TestInput = undefined; + try t.init(); + defer t.deinit(); + // Typed faster than read (a burst over a link): `x` and Escape in one + // read, `A` in the next while the ESC waits for a sequence's rest. Joined + // to it, it was Alt-A and the Escape was lost (insert mode kept, `A` + // typed nothing). A sequence cut after its ESC still joins (the test + // above: `[`, `O` and the strings' introducers, another ESC). + var reader: TestReader = .{ .parts = &.{ "x\x1b", "A" } }; + try readInput(&t.loop, &reader, &t.vx, &t.feed); + try std.testing.expect(reader.asked); + try std.testing.expectEqual(.winsize, std.meta.activeTag((try t.loop.tryEvent()).?)); + try std.testing.expectEqual(@as(u21, 'x'), (try t.loop.tryEvent()).?.key_press.codepoint); + const escape = (try t.loop.tryEvent()).?.key_press; + try std.testing.expectEqual(vaxis.Key.escape, escape.codepoint); + try std.testing.expect(!escape.mods.alt); + const a = (try t.loop.tryEvent()).?.key_press; + try std.testing.expectEqual(@as(u21, 'A'), a.codepoint); + try std.testing.expect(!a.mods.alt); + try std.testing.expect(try t.loop.tryEvent() == null); +} + test "an Escape read with the click after it is the Escape key, then the click" { var t: TestInput = undefined; try t.init(); -- cgit v1.3