diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/mouse.zig | 43 | ||||
| -rw-r--r-- | src/pardes.zig | 91 |
2 files changed, 62 insertions, 72 deletions
diff --git a/src/mouse.zig b/src/mouse.zig index 1292c06f..426b7d8e 100644 --- a/src/mouse.zig +++ b/src/mouse.zig @@ -1999,15 +1999,6 @@ test "a chorded exec an event reader holds is told with its argument, and writte _ = th.call(p, .{ .tag = 4, .op = .release, .node = event, .handle = h }); } -/// Every byte the core wrote to pane 0's pty since the last call. -fn ptyWrites(p: *Pardes, out: *std.ArrayList(u8)) ![]const u8 { - out.clearRetainingCapacity(); - while (p.nextEffect()) |effect| { - if (effect == .write and effect.write.pane == 0) try out.appendSlice(p.gpa, effect.write.bytes.slice()); - } - return out.items; -} - test "a wheel over a program that owns it is the program's, in the form it asked for; a shell's scrolls" { if (comptime !panes.terminal.enabled) return error.SkipZigTest; const gpa = std.testing.allocator; @@ -2016,9 +2007,8 @@ test "a wheel over a program that owns it is the program's, in the form it asked var frame = std.heap.ArenaAllocator.init(gpa); defer frame.deinit(); _ = try p.render(frame.allocator()); - var out: std.ArrayList(u8) = .empty; - defer out.deinit(gpa); - _ = try ptyWrites(p, &out); + var out: [4096]u8 = undefined; + _ = pardes.test_api.ptyBytes(p, 0, &out); const pane = p.panes[0].?; const r = p.rects[0]; // Grid cell 6,4 (1-based), as a program counts. @@ -2047,9 +2037,9 @@ test "a wheel over a program that owns it is the program's, in the form it asked }; for (cases) |case| { p.update(.{ .output = .{ .pane = 0, .bytes = case.modes } }); - _ = try ptyWrites(p, &out); + _ = pardes.test_api.ptyBytes(p, 0, &out); p.update(.{ .mouse = .{ .button = case.button, .kind = .press, .col = col, .row = row, .ctrl = case.ctrl, .alt = case.alt, .shift = case.shift } }); - try std.testing.expectEqualStrings(case.sent, try ptyWrites(p, &out)); + try std.testing.expectEqualStrings(case.sent, pardes.test_api.ptyBytes(p, 0, &out)); } // A shell on the main screen: the wheel scrolls its scrollback, and // the shell hears nothing. @@ -2059,21 +2049,21 @@ test "a wheel over a program that owns it is the program's, in the form it asked p.update(.{ .output = .{ .pane = 0, .bytes = try std.fmt.bufPrint(&line, "{d}\r\n", .{i}) } }); } _ = try p.render(frame.allocator()); - _ = try ptyWrites(p, &out); + _ = pardes.test_api.ptyBytes(p, 0, &out); const was = pane.scroll(); p.update(.{ .mouse = .{ .button = .wheel_up, .kind = .press, .col = col, .row = row } }); - try std.testing.expectEqualStrings("", try ptyWrites(p, &out)); + try std.testing.expectEqualStrings("", pardes.test_api.ptyBytes(p, 0, &out)); try std.testing.expect(pane.scroll() != was); // A program tracking the mouse on the main screen: Shift and the // wheel scroll the scrollback, the program hears nothing. p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?1000h\x1b[?1006h" } }); - _ = try ptyWrites(p, &out); + _ = pardes.test_api.ptyBytes(p, 0, &out); const tracked = pane.scroll(); p.update(.{ .mouse = .{ .button = .wheel_up, .kind = .press, .col = col, .row = row, .shift = true } }); - try std.testing.expectEqualStrings("", try ptyWrites(p, &out)); + try std.testing.expectEqualStrings("", pardes.test_api.ptyBytes(p, 0, &out)); try std.testing.expect(pane.scroll() != tracked); p.update(.{ .mouse = .{ .button = .wheel_up, .kind = .press, .col = col, .row = row } }); - try std.testing.expectEqualStrings("\x1b[<64;6;4M", try ptyWrites(p, &out)); + try std.testing.expectEqualStrings("\x1b[<64;6;4M", pardes.test_api.ptyBytes(p, 0, &out)); } test "Alt-click is B2 and Super-click B3, kept from the press through the release; Ctrl-click stays B1" { @@ -2113,9 +2103,8 @@ test "a program tracking the mouse gets its clicks, drags and moves over its gri var frame = std.heap.ArenaAllocator.init(gpa); defer frame.deinit(); _ = try p.render(frame.allocator()); - var out: std.ArrayList(u8) = .empty; - defer out.deinit(gpa); - _ = try ptyWrites(p, &out); + var out: [4096]u8 = undefined; + _ = pardes.test_api.ptyBytes(p, 0, &out); const pane = p.panes[0].?; try std.testing.expectEqual(.tty, pane.body.mode); const r = p.rects[0]; @@ -2123,7 +2112,7 @@ test "a program tracking the mouse gets its clicks, drags and moves over its gri const row = p.bodyTop(pane, r) + 3; const Ev = struct { button: Mouse.Button, kind: Mouse.Kind, dx: i32 = 0, dy: i32 = 0, shift: bool = false, alt: bool = false, ctrl: bool = false, super: bool = false }; const send = struct { - fn f(pp: *Pardes, o: *std.ArrayList(u8), c: u16, rw: u16, e: Ev) ![]const u8 { + fn f(pp: *Pardes, o: []u8, c: u16, rw: u16, e: Ev) ![]const u8 { pp.update(.{ .mouse = .{ .button = e.button, .kind = e.kind, @@ -2134,12 +2123,12 @@ test "a program tracking the mouse gets its clicks, drags and moves over its gri .ctrl = e.ctrl, .super = e.super, } }); - return ptyWrites(pp, o); + return pardes.test_api.ptyBytes(pp, 0, o); } }.f; // htop's modes: clicks in SGR, a release its own `m`. p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?1049h\x1b[?1h\x1b[?1006;1000h" } }); - _ = try ptyWrites(p, &out); + _ = pardes.test_api.ptyBytes(p, 0, &out); try std.testing.expectEqualStrings("\x1b[<0;6;4M", try send(p, &out, col, row, .{ .button = .left, .kind = .press })); try std.testing.expect(p.drag == .none); // 1000 has no drags; the release is reported where it lands. @@ -2181,7 +2170,7 @@ test "a program tracking the mouse gets its clicks, drags and moves over its gri // codex's any-event tracking: a drag once per cell, carried past the // grid's edge and clamped there, and bare moves. p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?1003h" } }); - _ = try ptyWrites(p, &out); + _ = pardes.test_api.ptyBytes(p, 0, &out); try std.testing.expectEqualStrings("\x1b[<35;9;4M", try send(p, &out, col, row, .{ .button = .none, .kind = .motion, .dx = 3 })); try std.testing.expectEqualStrings("", try send(p, &out, col, row, .{ .button = .none, .kind = .motion, .dx = 3 })); try std.testing.expectEqualStrings("\x1b[<0;6;4M", try send(p, &out, col, row, .{ .button = .left, .kind = .press })); @@ -2205,7 +2194,7 @@ test "a program tracking the mouse gets its clicks, drags and moves over its gri // Legacy X10 bytes: a release is button 3. (Turning 1003 off turns // tracking off, as xterm's one tracking mode goes.) p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?1003l\x1b[?1006l\x1b[?1000h" } }); - _ = try ptyWrites(p, &out); + _ = pardes.test_api.ptyBytes(p, 0, &out); try std.testing.expectEqualStrings("\x1b[M &$", try send(p, &out, col, row, .{ .button = .left, .kind = .press })); try std.testing.expectEqualStrings("\x1b[M#&$", try send(p, &out, col, row, .{ .button = .left, .kind = .release })); diff --git a/src/pardes.zig b/src/pardes.zig index 72201360..e0311749 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -531,6 +531,23 @@ pub const test_api = if (@import("builtin").is_test) struct { exec.runBuiltin(p, command, id, txt, arg); return true; } + + /// What the core wrote to pane `pane`'s pty (every pane's, for null) + /// since the last drain, every effect taken off: as much as `buf` holds. + pub fn ptyBytes(p: *Pardes, pane: ?u8, buf: []u8) []const u8 { + var n: usize = 0; + while (p.nextEffect()) |effect| switch (effect) { + .write => |w| { + if (pane != null and w.pane != pane.?) continue; + const b = w.bytes.slice(); + if (n + b.len > buf.len) break; + @memcpy(buf[n..][0..b.len], b); + n += b.len; + }, + else => {}, + }; + return buf[0..n]; + } } else struct {}; fn drainForSavePath(p: *Pardes, buf: []u8) ?[]const u8 { @@ -8037,21 +8054,21 @@ test "raw tty Esc backs out and Shift-Esc is the program's Esc" { const shell = p.active; const pane = p.panes[shell].?; var buf: [256]u8 = undefined; - _ = drainWrites(p, &buf); + _ = test_api.ptyBytes(p, null, &buf); p.update(.{ .key = .{ .cp = 'x', .text = "x" } }); - _ = drainWrites(p, &buf); + _ = test_api.ptyBytes(p, null, &buf); p.update(.{ .key = .{ .cp = Key.escape } }); // past typed text too try std.testing.expectEqual(@as(usize, 0), p.active); - try std.testing.expectEqualStrings("", drainWrites(p, &buf)); + try std.testing.expectEqualStrings("", test_api.ptyBytes(p, null, &buf)); try std.testing.expectEqual(Text.Mode.tty, pane.body.mode); p.update(.{ .key = .{ .cp = Key.escape } }); // back into the shell try std.testing.expectEqual(shell, p.active); - _ = drainWrites(p, &buf); + _ = test_api.ptyBytes(p, null, &buf); p.update(.{ .key = .{ .cp = Key.escape, .shift = true } }); try std.testing.expectEqual(shell, p.active); - try std.testing.expectEqualStrings("\x1b", drainWrites(p, &buf)); + try std.testing.expectEqualStrings("\x1b", test_api.ptyBytes(p, null, &buf)); } test "with ProgramEsc raw tty Escape only leaves at a shell prompt" { @@ -8063,26 +8080,26 @@ test "with ProgramEsc raw tty Escape only leaves at a shell prompt" { const shell = p.active; const pane = p.panes[shell].?; var buf: [256]u8 = undefined; - _ = drainWrites(p, &buf); + _ = test_api.ptyBytes(p, null, &buf); // Ordinary keys reach the child; Shift-Escape is the one unconditional way // out and sends it nothing. p.update(.{ .key = .{ .cp = 'x', .text = "x" } }); try std.testing.expectEqual(shell, p.active); - try std.testing.expect(drainWrites(p, &buf).len > 0); + try std.testing.expect(test_api.ptyBytes(p, null, &buf).len > 0); p.update(.{ .key = .{ .cp = Key.escape, .shift = true } }); try std.testing.expectEqual(@as(usize, 0), p.active); - try std.testing.expectEqualStrings("", drainWrites(p, &buf)); + try std.testing.expectEqualStrings("", test_api.ptyBytes(p, null, &buf)); try std.testing.expectEqual(Text.Mode.tty, pane.body.mode); p.update(.{ .key = .{ .cp = Key.escape } }); // back into the shell try std.testing.expectEqual(shell, p.active); - _ = drainWrites(p, &buf); + _ = test_api.ptyBytes(p, null, &buf); p.update(.{ .key = .{ .cp = Key.escape } }); // bare Escape leaves at a prompt try std.testing.expectEqual(@as(usize, 0), p.active); - try std.testing.expectEqualStrings("", drainWrites(p, &buf)); + try std.testing.expectEqualStrings("", test_api.ptyBytes(p, null, &buf)); try std.testing.expectEqual(Text.Mode.tty, pane.body.mode); } @@ -8143,11 +8160,11 @@ test "raw tty Ctrl-B switches to editor mode without sending child input" { const shell = p.active; p.sync(); var buf: [256]u8 = undefined; - _ = drainWrites(p, &buf); + _ = test_api.ptyBytes(p, null, &buf); p.update(.{ .key = .{ .cp = 'b', .ctrl = true } }); try std.testing.expectEqual(shell, p.active); try std.testing.expectEqual(Text.Mode.normal, p.panes[shell].?.body.mode); - try std.testing.expectEqualStrings("", drainWrites(p, &buf)); + try std.testing.expectEqualStrings("", test_api.ptyBytes(p, null, &buf)); } test "the jump list never holds two entries in a row the same, when the one between them goes" { @@ -8232,28 +8249,12 @@ test "a Rename the language server finds nothing for says so, and changes nothin try std.testing.expectEqualStrings("const value=1;\n", pane.file.?.content); } -/// Every `.write` a keystroke produced, joined — what the pane's program would -/// actually have read off its pty. -fn drainWrites(p: *Pardes, buf: []u8) []const u8 { - var n: usize = 0; - while (p.nextEffect()) |effect| switch (effect) { - .write => |w| { - const b = w.bytes.slice(); - if (n + b.len > buf.len) break; - @memcpy(buf[n..][0..b.len], b); - n += b.len; - }, - else => {}, - }; - return buf[0..n]; -} - test "raw tty forwards Ctrl letters and former global shortcuts to the child" { if (platform == .web) return; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); defer p.deinit(); var buf: [256]u8 = undefined; - _ = drainWrites(p, &buf); + _ = test_api.ptyBytes(p, null, &buf); edit.setYank(p, "register text"); for ('a'..'z' + 1) |letter| { if (letter == p.tty_toggle) continue; // editor/TTY mode shortcut @@ -8266,7 +8267,7 @@ test "raw tty forwards Ctrl letters and former global shortcuts to the child" { 'm' => "\x1b[109;5u", else => &expected, }; - try std.testing.expectEqualStrings(encoded, drainWrites(p, &buf)); + try std.testing.expectEqualStrings(encoded, test_api.ptyBytes(p, null, &buf)); try std.testing.expectEqual(@as(usize, 0), p.active); try std.testing.expectEqual(Text.Mode.tty, p.panes[0].?.body.mode); try std.testing.expect(!p.ctrl_w_pending and p.clip_pending == null); @@ -8274,26 +8275,26 @@ test "raw tty forwards Ctrl letters and former global shortcuts to the child" { // ...and the two paste chords are the exception: Ctrl-V types the register // at the program, Ctrl-Shift-V asks the desktop first and types nothing yet. p.update(.{ .key = .{ .cp = 'v', .ctrl = true } }); - try std.testing.expectEqualStrings("register text", drainWrites(p, &buf)); + try std.testing.expectEqualStrings("register text", test_api.ptyBytes(p, null, &buf)); try std.testing.expect(p.clip_pending == null); p.update(.{ .key = .{ .cp = 'v', .ctrl = true, .shift = true } }); - try std.testing.expectEqualStrings("", drainWrites(p, &buf)); + try std.testing.expectEqualStrings("", test_api.ptyBytes(p, null, &buf)); try std.testing.expect(p.clip_pending != null); const unopened = p.freeSlot(); // whatever the boot layout already used for ([_]u8{ 'n', 'c' }) |letter| { p.update(.{ .key = .{ .cp = letter, .alt = true } }); const expected = [_]u8{ 0x1b, letter }; - try std.testing.expectEqualStrings(&expected, drainWrites(p, &buf)); + try std.testing.expectEqualStrings(&expected, test_api.ptyBytes(p, null, &buf)); try std.testing.expectEqual(@as(usize, 0), p.active); try std.testing.expectEqual(unopened, p.freeSlot()); } p.update(.{ .key = .{ .cp = Key.up } }); - try std.testing.expectEqualStrings("\x1b[A", drainWrites(p, &buf)); + try std.testing.expectEqualStrings("\x1b[A", test_api.ptyBytes(p, null, &buf)); p.panes[0].?.terminal.?.vt.modes.set(.cursor_keys, true); p.update(.{ .key = .{ .cp = Key.up } }); - try std.testing.expectEqualStrings("\x1bOA", drainWrites(p, &buf)); + try std.testing.expectEqualStrings("\x1bOA", test_api.ptyBytes(p, null, &buf)); p.update(.{ .key = .{ .cp = Key.left, .ctrl = true } }); - try std.testing.expectEqualStrings("\x1b[1;5D", drainWrites(p, &buf)); + try std.testing.expectEqualStrings("\x1b[1;5D", test_api.ptyBytes(p, null, &buf)); } test "an unasked desktop paste reaches a tty pane's program, not its buffer" { @@ -8303,14 +8304,14 @@ test "an unasked desktop paste reaches a tty pane's program, not its buffer" { defer p.deinit(); p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); var buf: [256]u8 = undefined; - _ = drainWrites(p, &buf); + _ = test_api.ptyBytes(p, null, &buf); const pane = p.panes[0].?; panes.terminal.enterTty(p, 0); // No request behind it: the window manager's own paste, or SDL answering a // Ctrl-Shift-V the desktop handled. It still has to reach the shell. p.update(.{ .paste = "ls -la" }); - try std.testing.expectEqualStrings("ls -la", drainWrites(p, &buf)); + try std.testing.expectEqualStrings("ls -la", test_api.ptyBytes(p, null, &buf)); // and nothing was typed into an overlay the pty does not know about try std.testing.expect(pane.ovl == null); } @@ -8323,14 +8324,14 @@ test "a paste larger than the effect ring reaches the program whole and in order p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); const buf = try gpa.alloc(u8, 1 << 20); defer gpa.free(buf); - _ = drainWrites(p, buf); + _ = test_api.ptyBytes(p, null, buf); panes.terminal.enterTty(p, 0); const text = try gpa.alloc(u8, 300 * 1024); defer gpa.free(text); for (text, 0..) |*c, i| c.* = 'a' + @as(u8, @intCast(i % 26)); p.update(.{ .paste = text }); - try std.testing.expectEqualSlices(u8, text, drainWrites(p, buf)); + try std.testing.expectEqualSlices(u8, text, test_api.ptyBytes(p, null, buf)); // ...and the core is not still holding a copy afterwards try std.testing.expectEqual(@as(usize, 0), p.pending_write_bytes); } @@ -8344,7 +8345,7 @@ test "a bracketed paste larger than the ring still closes its bracket" { p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); const buf = try gpa.alloc(u8, 1 << 20); defer gpa.free(buf); - _ = drainWrites(p, buf); + _ = test_api.ptyBytes(p, null, buf); panes.terminal.enterTty(p, 0); p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?2004h" } }); @@ -8353,7 +8354,7 @@ test "a bracketed paste larger than the ring still closes its bracket" { defer gpa.free(text); @memset(text, 'z'); p.update(.{ .paste = text }); - const got = drainWrites(p, buf); + const got = test_api.ptyBytes(p, null, buf); try std.testing.expect(std.mem.startsWith(u8, got, "\x1b[200~")); try std.testing.expect(std.mem.endsWith(u8, got, "\x1b[201~")); try std.testing.expectEqualSlices(u8, text, got["\x1b[200~".len .. got.len - "\x1b[201~".len]); @@ -8368,7 +8369,7 @@ test "a terminal's answer to its program waits behind the paste still queued for p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); const buf = try gpa.alloc(u8, 1 << 20); defer gpa.free(buf); - _ = drainWrites(p, buf); + _ = test_api.ptyBytes(p, null, buf); panes.terminal.enterTty(p, 0); const text = try gpa.alloc(u8, 300 * 1024); @@ -8380,7 +8381,7 @@ test "a terminal's answer to its program waits behind the paste still queued for const taken = p.nextEffect().?; const off = if (taken == .write) taken.write.bytes.len else 0; p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[6n" } }); - const got = drainWrites(p, buf); + const got = test_api.ptyBytes(p, null, buf); // The whole paste first, then the answer, `CSI row ; col R`, whole. try std.testing.expectEqualSlices(u8, text[off..], got[0 .. text.len - off]); const answer = got[text.len - off ..]; @@ -8395,7 +8396,7 @@ test "pasted bytes still queued at shutdown are freed, not leaked" { p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); const buf = try gpa.alloc(u8, 1 << 20); defer gpa.free(buf); - _ = drainWrites(p, buf); + _ = test_api.ptyBytes(p, null, buf); panes.terminal.enterTty(p, 0); const text = try gpa.alloc(u8, 300 * 1024); |
