diff options
Diffstat (limited to 'src/pardes.zig')
| -rw-r--r-- | src/pardes.zig | 91 |
1 files changed, 46 insertions, 45 deletions
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); |
