summaryrefslogtreecommitdiff
path: root/dev/tui/tui.zig
diff options
context:
space:
mode:
Diffstat (limited to 'dev/tui/tui.zig')
-rw-r--r--dev/tui/tui.zig39
1 files changed, 37 insertions, 2 deletions
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();