summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/Core.zig1
-rw-r--r--core/panes.zig5
-rw-r--r--core/replica.zig360
-rw-r--r--core/tagline.zig2
-rw-r--r--core/tape.zig10
-rw-r--r--core/term/terminal.zig40
6 files changed, 387 insertions, 31 deletions
diff --git a/core/Core.zig b/core/Core.zig
index efa2ac3d..a800cb68 100644
--- a/core/Core.zig
+++ b/core/Core.zig
@@ -1684,6 +1684,7 @@ fn updateInner(p: *Pardes, ev: Event) void {
pane.shell_spoke = true;
ctlfs.events.notePtyOutput(p, o.pane.index(), o.bytes);
pane.feedOutput(p, o.bytes);
+ pane.term_images = if (p.tape.screen_state) p.tape.value(.images, pardes.Term.hasImages, .{pane.terminal}) else pardes.Term.hasImages(pane.terminal);
ctlfs.pty.noteMarks(p, o.pane.index(), pane);
exec.flushReplEnter(p, o.pane.index(), true);
},
diff --git a/core/panes.zig b/core/panes.zig
index 8b152132..2f561d05 100644
--- a/core/panes.zig
+++ b/core/panes.zig
@@ -134,6 +134,11 @@ pub const Pane = struct {
/// cell), and its kitty graphics drawn as glyph art (Petscii).
term_mouse: ?Term.Coordinate = null,
term_petscii: bool = false,
+ /// Whether its terminal holds a kitty image, as the session's emulator
+ /// said after its program's last output (taped under screen state: a
+ /// client's emulator is fed the session's cells, never an image). Its
+ /// tag says so (tagline.zig), on every side.
+ term_images: bool = false,
gpa: std.mem.Allocator,
serial: u32 = 0,
/// What the control filesystem keeps of this pane -- addr and limit, the
diff --git a/core/replica.zig b/core/replica.zig
index 97da9c74..e26a9f17 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -806,7 +806,7 @@ pub const Replica = struct {
tape.adoptCore(&r.t, c);
const was = Stepping.begin(&r.t, &r.epoch);
defer was.end();
- r.view = Peers.viewFor(c, null) orelse return error.NoView;
+ r.view = Peers.viewFor(c, null, true) orelse return error.NoView;
break :boot c;
};
// The chrome's colours are local, never dumped: the restored theme's.
@@ -1022,10 +1022,6 @@ pub const Replica = struct {
r.toConfirmed();
if (r.diverged != null) return;
}
- // What it shows moved only when what a digest covers did: a unit of
- // the session's pump that read its clock and moved nothing draws
- // no frame.
- if (ends == null or !std.mem.eql(u8, ends.?, r.last_digest.items)) r.moved = true;
// What the unit opens or closes outside the copy (`keepSince` below).
var serials: [pardes.MAX_PANES]u32 = undefined;
var retiring = false;
@@ -1045,6 +1041,13 @@ pub const Replica = struct {
trace(r.gpa, "replica {d}: the unit of {d}/{d} diverged: {s}\n", .{ r.id, u.origin, u.seq, why }, &.{u.tape});
return r.fail(why);
}
+ // What it shows moved only when the unit's steps asked for a frame,
+ // as the session's own frontend draws (Pardes.pump): a pump that
+ // read its clock and stepped none, a 9P request that changed no
+ // pane (the close after a ctl write: its frame was drawn again),
+ // draw nothing. Read after the replay: before it, it is what the
+ // units before asked (and a first unit has no core yet).
+ if (r.core().needs_frame) r.moved = true;
if (check) {
r.since_check = 0;
r.stats.checks += 1;
@@ -1299,9 +1302,12 @@ pub const Replica = struct {
pub fn draw(r: *Replica, present: pardes.views.Present) bool {
rollback.Epoch.select(&r.epoch);
defer rollback.Epoch.select(null);
- // A frame drawing owes another is this process's, not the core's.
+ // A client's replica (`skips_render`) is never watched in its
+ // replay, which so never clears `needs_frame`: the frame its steps
+ // asked for is this one. One in process replays its session's own
+ // drawing (Pardes.pump), which reads and clears it as the session's.
const c = r.core();
- const needs = c.needs_frame;
+ const needs = c.needs_frame and !r.t.skips_render;
defer c.needs_frame = needs;
return pardes.views.drawOne(c, r.view, present) catch true;
}
@@ -2117,6 +2123,9 @@ pub const Peers = struct {
/// No frontend of its own (the host says): with no client either, the
/// session is unattended.
detached: bool = false,
+ /// A Restore let every client go (`restoring`): the restored core's one
+ /// view (views.only) is the first to join again's.
+ restored: bool = false,
/// Its last client left with a close (its window closed, its terminal
/// hung up) and the session is a pair's not kept (daemon.sessionState): it
/// ends there (runtime/session.zig).
@@ -2246,7 +2255,14 @@ pub const Peers = struct {
},
.screen => |in| {
defer ps.gpa.free(in.msg);
- const reply = (ps.screens.take(core, in.peer.sid, in.msg) catch null) orelse continue;
+ // An acknowledgement (or a reset): room again in its flight
+ // for what its terminals came to since. Its flush at this
+ // boundary came before it, and at the bound sent nothing:
+ // with no step after it, that client kept an old screen.
+ const reply = (ps.screens.take(core, in.peer.sid, in.msg) catch null) orelse {
+ ps.screensTo(core, in.peer, .flush);
+ continue;
+ };
defer ps.gpa.free(reply);
const head = wire.header(@backingInt(wire.ServerTag.screen), reply.len);
in.peer.send(&.{ &head, reply });
@@ -2346,10 +2362,26 @@ pub const Peers = struct {
ps.recording = true;
ps.samples = std.c.getenv("PARDES_REPLICA_STATS") != null;
}
- const k = viewFor(core, joining.view) orelse {
+ // A join with no room lets go a view kept past its bound by the
+ // core's clock (Core.openView), which a wake by mail alone, as this
+ // join's, does not move (tui.zig `clockNow`): moved now, on the tape.
+ if (@popCount(core.views_open) == pardes.views.max) if (core.host.call(.now, .{})) |now| core.advance(now);
+ // A pair's first client, or the first back after a Restore with no
+ // client and no view kept for one: the view the session has is the
+ // joiner's. Any other attach (a detached session's too) opens one.
+ const own = joining.own or (ps.restored and ps.count() == 0 and core.views_away == 0);
+ ps.restored = false;
+ const k = viewFor(core, joining.view, own) orelse {
core.unattended = ps.count() == 0 and ps.detached;
if (ps.count() == 0) ps.stop(core);
- return daemon.refuseReplica(fd, "that session has as many views as it takes");
+ const full = "that session has as many views as it takes";
+ var left: u64 = 0;
+ for (0..pardes.views.max) |i| if (pardes.views.isAway(core, @intCast(i))) {
+ const n = (core.views[i].?.away_ns + pardes.views.away_kept_ns) -| core.clock.now_ns;
+ left = if (left == 0) n else @min(left, n);
+ };
+ var why: [128]u8 = undefined;
+ return daemon.refuseReplica(fd, if (left == 0) full else std.fmt.bufPrint(&why, full ++ " (one is kept {d} s more for a client joining again)", .{(left + std.time.ns_per_s - 1) / std.time.ns_per_s}) catch full);
};
ps.seq += 1;
ps.unit(0, ps.seq);
@@ -2420,18 +2452,22 @@ pub const Peers = struct {
/// boot shell's spawn among them) is performed after it, on the tape
/// its client is sent, not by the view's settle, as a client that made
/// its core itself performs none of them.
+ /// `own`: the live view is the joiner's, in a session nobody looks at
+ /// (a pair's first client, Options.pair; the first back after a
+ /// Restore): not one beside it nobody looks at, which would size its
+ /// terminals (Pardes.sizes).
/// ponytail: a divergence's redial that came before its old link's end
/// (posted by that link's reader, microseconds after the bye; the
/// redial is a dial and the daemon's hand-over later) finds its view
/// not kept yet, and takes a new one.
- fn viewFor(core: *Pardes, back: ?u8) ?u8 {
+ fn viewFor(core: *Pardes, back: ?u8, own: bool) ?u8 {
const held = core.gpa.alloc(pardes.Effect, core.effects_len) catch return null;
defer core.gpa.free(held);
for (held, 0..) |*e, i| e.* = core.effects[(core.effects_head + i) % core.effects.len];
core.effects_len = 0;
defer queueFirst(core, held);
core.unattended = false;
- const k = (if (back) |v| (if (core.rejoinView(.at(v))) v else null) else null) orelse (core.openView() orelse return null).index();
+ const k = if (own) core.live else (if (back) |v| (if (core.rejoinView(.at(v))) v else null) else null) orelse (core.openView() orelse return null).index();
core.undrawn |= @as(u8, 1) << @intCast(k);
return k;
}
@@ -2536,6 +2572,36 @@ pub const Peers = struct {
ps.drop(core, true);
}
+ /// Every client told the session is restoring a dump and let go: each
+ /// joins again at once (host_io.restoreCore), as at an Upgrade, not
+ /// asking the daemon whether a link cut with no word was its
+ /// session's end (`cutListed`).
+ pub fn restoring(ps: *Peers, core: *Pardes) void {
+ var out: [600]u8 = undefined;
+ const words = wire.encodeServer(&out, .{ .refused = restore_words }) catch unreachable; // the words fit
+ for (ps.list) |slot| if (slot) |peer| peer.last(words);
+ ps.flushed200();
+ // One dialling again already (handed over meanwhile) joins the
+ // restored core at the next boundary, not hung up on: its redial
+ // would wait 100 ms for the next dial.
+ var again: std.ArrayListUnmanaged(Joining) = .empty;
+ defer again.deinit(ps.gpa);
+ ps.lock.lockUncancelable(ps.io);
+ var i: usize = 0;
+ while (i < ps.inbox.items.len) {
+ if (ps.inbox.items[i] != .join) {
+ i += 1;
+ continue;
+ }
+ again.append(ps.gpa, ps.inbox.items[i].join) catch break;
+ _ = ps.inbox.orderedRemove(i);
+ }
+ ps.lock.unlock(ps.io);
+ ps.drop(core, false);
+ ps.restored = true;
+ for (again.items) |j| ps.post(.{ .join = j });
+ }
+
/// What each client was sent goes first, for up to 200 ms.
fn flushed200(ps: *Peers) void {
const until = nowNs() + 200 * std.time.ns_per_ms;
@@ -3112,6 +3178,10 @@ pub fn ended(why: []const u8) bool {
/// What an Upgrade tells its clients (`Peers.refuseAll`): each dials again.
pub const upgrading = "the session is upgrading to another build of pardes";
+/// What a Restore tells its clients (`Peers.restoring`): each joins again,
+/// saying nothing (the own frontend's Restore shows nothing either).
+pub const restore_words = "the session is restoring a dump";
+
/// A link cut with no last word (`why` its hang-up) whose session its
/// daemon lists still: an Upgrade's exec cut it, a dump on the way past the
/// clients' flush (`Peers.refuseAll`), whichever build the session is, not
@@ -3639,7 +3709,7 @@ pub const Link = struct {
return true;
}
if (c.refused) |words| {
- c.notify("joined again: {s}", .{words});
+ if (!std.mem.eql(u8, words, restore_words)) c.notify("joined again: {s}", .{words});
std.heap.c_allocator.free(words);
c.refused = null;
return true;
@@ -4087,6 +4157,28 @@ test "the copy taken for an input of its own stays once the input is confirmed:
try std.testing.expectEqual(@as(u32, 1), r.stats.foreign);
}
+test "a client marks its view moved for a unit of the session's whose steps asked for a frame, and not for one whose steps asked none" {
+ // The own frontend's rule (Pardes.pump): a unit that only read the
+ // clock, or a ctl write's close, draws nothing; a client presented it
+ // again (the corpus's pet: one picture twice).
+ const gpa = std.testing.allocator;
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false);
+ defer a.deinit();
+ // A client as a dial joins it: it draws none of its replay.
+ const r = try joinedNow(a, 1, 0);
+ defer r.deinit();
+ _ = r.draw(.{ .ctx = null, .f = null });
+ r.moved = false;
+ r.confirm(try a.local(.{ .key = .{ .cp = 'l', .text = "l" } }));
+ try std.testing.expect(r.moved);
+ _ = r.draw(.{ .ctx = null, .f = null });
+ r.moved = false;
+ a.pump();
+ r.confirm((try a.pending()).?);
+ try std.testing.expect(!r.moved);
+ try agrees(a, r);
+}
+
test "the session's pump units that moved nothing, kept under a key applied on top, reach the kept list's bound: it is let go at the confirm that leaves nothing applied, never replayed whole" {
// The live pair's order (core/replica.zig Peers.boundary): before it
// steps a client's key the session sends its pump's unit, which moved
@@ -4716,6 +4808,40 @@ test "bytes: a session's output that moves nothing the digest covers (a colour s
try sameTerminals(b.a, b.r);
}
+test "bytes: a terminal's view a rollback puts back is the session's through a reflow: a view on a wrapped line's second row, joined into the row above by the session's widening, is the same row after its narrowing on every side" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
+ const b = try BytesPair.init();
+ defer b.deinit();
+ const id = b.shell.index();
+ // Lines a row and a half long, the view on the first's second row (put
+ // there alike on both sides: no step of either moved it).
+ const cols = pardes.Term.grid(b.a.core.panes[id].?.terminal).cols;
+ var line: [512]u8 = undefined;
+ const text = line[0 .. cols + cols / 2];
+ @memset(text, 'x');
+ b.a.output(.{ .output = .{ .pane = b.shell, .bytes = "\x1bc" } });
+ for (0..40) |_| {
+ b.a.output(.{ .output = .{ .pane = b.shell, .bytes = text } });
+ b.a.output(.{ .output = .{ .pane = b.shell, .bytes = "\r\n" } });
+ }
+ b.r.confirm((try b.a.pending()).?);
+ b.r.settle();
+ for ([_]*Pardes{ b.a.core, b.r.core() }) |core| pardes.Term.viewTo(core.panes[id].?.terminal.?, 1);
+ // The session's frontend widens: the line one row, the view at row 0,
+ // its cell mid-row.
+ b.r.confirm(try b.a.own(.{ .resize = .{ .cols = 200, .rows = 24 } }));
+ b.r.settle();
+ try agrees(b.a, b.r);
+ try std.testing.expectEqual(@as(?usize, 0), pardes.Term.grid(b.a.core.panes[id].?.terminal).view);
+ // A key of the client's on top, the session's first: the copy put back.
+ try b.rollback();
+ try agrees(b.a, b.r);
+ // The session narrows again: every side's view the same row.
+ b.r.confirm(try b.a.own(.{ .resize = .{ .cols = 80, .rows = 24 } }));
+ b.r.settle();
+ try agrees(b.a, b.r);
+}
+
test "bytes: a mouse motion a speculation told a terminal's program is told again when its input is replayed: the cell last told is the pane's, put back with it" {
if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
const gpa = std.testing.allocator;
@@ -4772,7 +4898,7 @@ test "a session's first client on the socket it starts with (Options.pair), join
try firstOnPair(false);
}
-test "bytes: a session's first client on its pair, taken before its first effects, has the boot shell's first bytes with no reload of them (the session's one at the join is of its emulator before any byte, the client's none); a client after them makes every side reload" {
+test "bytes: a session's first client on its pair, taken before its first effects, has the boot shell's first bytes with no reload at all (its view is the session's, none opened for it); a client after them makes every side reload" {
try firstOnPair(true);
}
@@ -4784,6 +4910,79 @@ test "a pair's client's own core, the file edited between the session's read and
try ownBootAgainst(true);
}
+test "a Restore tells its clients so before it lets them go (each joins again, saying nothing), and one handed over meanwhile is joined to the restored core at the next boundary, not hung up on" {
+ const gpa = std.testing.allocator;
+ var fds: [2]c_int = undefined;
+ if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket;
+ defer _ = libc.close(fds[1]);
+ var late: [2]c_int = undefined;
+ if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &late) != 0) return error.NoSocket;
+ defer _ = libc.close(late[1]);
+ const core = try Pardes.init(gpa, .{ .cols = 80, .rows = 24 });
+ defer core.deinit();
+ core.host = TestHost.host();
+ var ps: Peers = .{ .gpa = gpa, .io = std.testing.io, .name = "restore", .detached = true };
+ defer ps.deinit();
+ defer live_peers = null;
+ defer ps.dropAll(core);
+ ps.join(core, .{ .fd = fds[0] });
+ var why: [600]u8 = @splat(0);
+ var j = joinOn(gpa, fds[1], null, &why, null, null, null) orelse return error.JoinFailed;
+ gpa.free(j.dump);
+ defer j.rest.deinit(gpa);
+ // A client dialling again, handed over before the Restore lets the first go.
+ ps.handed(late[0], null);
+ ps.restoring(core);
+ const Last = struct {
+ words: ?[]u8 = null,
+ fn post(l: *@This(), msg: Said) bool {
+ switch (msg) {
+ .refused => |w| l.words = w,
+ .unit, .screen, .blob => |b| std.heap.c_allocator.free(b),
+ .checked, .ended, .left => {},
+ }
+ return true;
+ }
+ };
+ var last: Last = .{};
+ readLink(gpa, fds[1], &j.rest, &last, Last.post);
+ const words = last.words orelse return error.NoLastWord;
+ defer std.heap.c_allocator.free(words);
+ try std.testing.expectEqualStrings(restore_words, words);
+ try std.testing.expectEqual(1, ps.inbox.items.len);
+}
+
+test "a detached session's first attach opens a view of its own beside the session's; the first back after a Restore takes the session's" {
+ const gpa = std.testing.allocator;
+ const core = try Pardes.init(gpa, .{ .cols = 80, .rows = 24 });
+ defer core.deinit();
+ core.host = TestHost.host();
+ var ps: Peers = .{ .gpa = gpa, .io = std.testing.io, .name = "attach", .detached = true };
+ defer ps.deinit();
+ defer live_peers = null;
+ defer ps.dropAll(core);
+ const session_view = core.live;
+ var fds: [2]c_int = undefined;
+ if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket;
+ defer _ = libc.close(fds[1]);
+ ps.join(core, .{ .fd = fds[0] });
+ const first = for (ps.list) |slot| {
+ if (slot) |peer| break peer;
+ } else return error.NotJoined;
+ try std.testing.expect(first.view.index() != session_view);
+ try std.testing.expect(pardes.views.isOpen(core, session_view));
+ ps.restoring(core);
+ var again: [2]c_int = undefined;
+ if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &again) != 0) return error.NoSocket;
+ defer _ = libc.close(again[1]);
+ ps.join(core, .{ .fd = again[0] });
+ const back = for (ps.list) |slot| {
+ if (slot) |peer| break peer;
+ } else return error.NotJoined;
+ try std.testing.expectEqual(core.live, back.view.index());
+ try std.testing.expectEqual(1, pardes.views.count(core));
+}
+
/// A session's first client on its pair making its core itself
/// (`ownBoot`), the file it opens `edited` after the session read it.
fn ownBootAgainst(edited: bool) !void {
@@ -4805,7 +5004,7 @@ fn ownBootAgainst(edited: bool) !void {
defer ps.deinit();
defer live_peers = null;
defer ps.dropAll(core);
- ps.join(core, .{ .fd = fds[0] });
+ ps.join(core, .{ .fd = fds[0], .own = true });
// Edited after the session read it, before its client does.
if (edited) try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = "edited between\n" });
var o = opts;
@@ -4824,6 +5023,10 @@ fn ownBootAgainst(edited: bool) !void {
}
try std.testing.expectEqual(!edited, r == booted);
try std.testing.expectEqual(tape.digestOf(core), r.digest());
+ // The session's one view is its client's: none beside it, nobody's,
+ // sizes its terminals.
+ try std.testing.expectEqual(@as(u8, 1), core.views_open);
+ try std.testing.expectEqual(@as(u8, 0), r.view);
// What the client shows is the session's file as the session read it.
try std.testing.expectEqualStrings("the session's start\n", r.core().panes[0].?.file.?.content);
}
@@ -4855,7 +5058,7 @@ fn firstOnPair(bytes: bool) !void {
defer live_peers = null;
defer ps.dropAll(core);
const reloads = pardes.Term.reloads.n;
- ps.join(core, .{ .fd = fds[0] });
+ ps.join(core, .{ .fd = fds[0], .own = true });
var j: Joined = undefined;
var why: [600]u8 = @splat(0);
const r = if (bytes) r: {
@@ -4926,10 +5129,10 @@ fn firstOnPair(bytes: bool) !void {
if (r.diverged) |w| std.debug.print("diverged: {s}\n", .{w});
try std.testing.expect(r.diverged == null);
try std.testing.expectEqual(take.digest.?, r.digest());
- // Bytes: the session's reload at the join, of an emulator that held
- // nothing yet (Pardes.init made it, the spawn is a first effect); the
- // client loaded it from the dump and replayed every byte since.
- try std.testing.expectEqual(reloads + @intFromBool(bytes), pardes.Term.reloads.n);
+ // No reload at the join: the session's view is its first client's, none
+ // opened for it (Peers.viewFor); the client loaded the terminal from the
+ // dump and replayed every byte since.
+ try std.testing.expectEqual(reloads, pardes.Term.reloads.n);
var arena: std.heap.ArenaAllocator = .init(gpa);
defer arena.deinit();
const rows = try pardes.Term.viewport(r.core().panes[shell].?.terminal.?, arena.allocator(), false);
@@ -4940,7 +5143,7 @@ fn firstOnPair(bytes: bool) !void {
if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &late) != 0) return error.NoSocket;
defer _ = libc.close(late[1]);
ps.join(core, .{ .fd = late[0] });
- try std.testing.expectEqual(reloads + 2, pardes.Term.reloads.n);
+ try std.testing.expectEqual(reloads + 1, pardes.Term.reloads.n);
}
test "bytes: a join refused for want of a view reloads nothing on any side: a client with a key on top stays the session's" {
@@ -6295,6 +6498,107 @@ test "a closed terminal with a kitty image brought back on a client: its image d
try agrees(a, r);
}
+/// The core's fields whose digests differ between the session and a
+/// client, named (tape.partsDiffer); empty when they agree.
+fn partsApart(a: *Authority, r: *Replica, arena: std.mem.Allocator) ![]const u8 {
+ const want = try arena.create(tape.Parts);
+ const got = try arena.create(tape.Parts);
+ const w = digested: {
+ const was = Stepping.begin(&a.t, null);
+ defer was.end();
+ break :digested tape.digestParts(a.core, want);
+ };
+ const g = digested: {
+ const was = Stepping.begin(&r.t, &r.epoch);
+ defer was.end();
+ break :digested tape.digestParts(r.core(), got);
+ };
+ return if (std.mem.eql(u64, w, g)) "" else tape.partsDiffer(g, w, arena);
+}
+
+test "a terminal's program shows a kitty image under screen state: the client's emulator never has it, and its tag says what the session's does" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
+ const gpa = std.testing.allocator;
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true);
+ defer a.deinit();
+ var screens: Screens = .init(gpa);
+ defer screens.deinit(a.core);
+ const peer = try screens.addPeer();
+ const r = try Replica.init(gpa, 1, try a.unit(0, 0), true);
+ defer r.deinit();
+ const shell: u8 = for (a.core.panes, 0..) |slot, id| {
+ if (slot) |pane| if (pane.isTerminal()) break @intCast(id);
+ } else return error.NoShell;
+ try deliver(a, &screens, peer, r, .flush);
+ try std.testing.expectEqualStrings("", try partsApart(a, r, arena.allocator()));
+ // In the session's order: the pump's unit, then what its terminals came
+ // to; then a step after both, on every side.
+ a.output(.{ .output = .{ .pane = .at(shell), .bytes = "\x1b_Ga=T,i=7,f=24,s=2,v=2;ESIzESIzESIzESIz\x1b\\" } });
+ r.confirm((try a.pending()).?);
+ r.settle();
+ try deliver(a, &screens, peer, r, .flush);
+ r.confirm(try a.local(.{ .focus = true }));
+ r.settle();
+ try std.testing.expect(pardes.Term.hasImages(a.core.panes[shell].?.terminal));
+ try std.testing.expect(!pardes.Term.hasImages(r.core().panes[shell].?.terminal));
+ try std.testing.expectEqualStrings("", try partsApart(a, r, arena.allocator()));
+ try agrees(a, r);
+ const prefix = try pardes.tagline.prefixIn(r.core(), arena.allocator(), r.core().panes[shell].?);
+ try std.testing.expect(std.mem.indexOf(u8, prefix, " petscii:") != null);
+}
+
+test "a command typed into a shell with prompt marks under screen state: every side notes it as the session's emulator numbers it, and Kill finds it" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
+ const gpa = std.testing.allocator;
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ const ShellHost = struct {
+ fn taken(_: ?*anyopaque, _: pardes.Pane.Id) bool {
+ return false;
+ }
+ fn write(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void {}
+ const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .tty_taken = taken, .pty_write = write };
+ };
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, .{ .vtable = &ShellHost.vtable }, true);
+ defer a.deinit();
+ var screens: Screens = .init(gpa);
+ defer screens.deinit(a.core);
+ const peer = try screens.addPeer();
+ const r = try Replica.init(gpa, 1, try a.unit(0, 0), true);
+ defer r.deinit();
+ const shell: u8 = for (a.core.panes, 0..) |slot, id| {
+ if (slot) |pane| if (pane.isTerminal()) break @intCast(id);
+ } else return error.NoShell;
+ // pardes's own bash marks (host_io.zig): one command run, a prompt.
+ const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07";
+ a.output(.{ .output = .{ .pane = .at(shell), .bytes = prompt ++ "ls\r\n\x1b]133;C;aid=pardes\x07a b\r\n\x1b]133;D;0;aid=pardes\x07" ++ prompt } });
+ r.confirm((try a.pending()).?);
+ r.settle();
+ try deliver(a, &screens, peer, r, .flush);
+ a.core.view().active = shell;
+ r.confirm(try a.local(.{ .focus = true }));
+ r.settle();
+ try std.testing.expectEqualStrings("", try partsApart(a, r, arena.allocator()));
+ // Exec'd in the shell: typed at its prompt, the command noted.
+ r.confirm(try a.local(.{ .command = "Exec sleep 9" }));
+ r.settle();
+ // Its marks cross with the cells (Term.marksOf): numbered alike.
+ try std.testing.expectEqual(@as(u32, 2), a.core.panes[shell].?.sent_command.?.seq);
+ try std.testing.expectEqual(@as(u32, 2), r.core().panes[shell].?.sent_command.?.seq);
+ try std.testing.expectEqualStrings("", try partsApart(a, r, arena.allocator()));
+ // Its echo and its C mark: running. Kill is asked for it.
+ a.output(.{ .output = .{ .pane = .at(shell), .bytes = "sleep 9\r\n\x1b]133;C;aid=pardes\x07" } });
+ r.confirm((try a.pending()).?);
+ r.settle();
+ try deliver(a, &screens, peer, r, .flush);
+ r.confirm(try a.local(.{ .command = "Kill" }));
+ r.settle();
+ try std.testing.expectEqualStrings("", try partsApart(a, r, arena.allocator()));
+ try agrees(a, r);
+}
+
test "a far client's own Del of a pane whose kind's state is not plain, rolled back by another's unit: the pane comes back whole, and is closed again with the session's order" {
const gpa = std.testing.allocator;
const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false);
@@ -6561,7 +6865,7 @@ test "a client's n in a kind's pane is a motion: it steps to the next place a re
/// `addon`: a kind whose state is its own on the session (a PDF's), which
/// the walk does not stand in (its Still has no search, place or word):
/// the soak's (test/replicas.zig) PDF and pictures are, judged by name.
-const On = enum { any, file, kind, shell, shell_normal, addon };
+const On = enum { any, file, kind, shell, shell_normal, addon, dir };
/// What a far client may wait a round trip for (the user's line, P14: a
/// side effect on the system may be slow; motions, edits and layout are
@@ -6582,6 +6886,12 @@ const may_wait = [_]Wait{
.{ .what = "LookPrev", .on = .any, .stop = "resolve", .why = "a Look's walk: each word asked of the disk" },
.{ .what = "Mini *", .on = .any, .stop = "resolve", .why = "a file opened by name, from the session's disk" },
.{ .what = "Get", .on = .any, .stop = "read_file", .why = "the file read again from the session's disk" },
+ .{ .what = "Get", .on = .dir, .stop = "dir_listing", .why = "a directory listed again from the session's disk" },
+ // A directory's pane (Dir.zig): its entries are names on the session's disk.
+ .{ .what = "Enter", .on = .dir, .stop = "resolve", .why = "the entry under the cursor opened, from the session's disk" },
+ .{ .what = "* Enter", .on = .dir, .stop = "resolve", .why = "the entry opened (Enter after a prefix still opens it)" },
+ .{ .what = "Tab", .on = .dir, .stop = "kind", .why = "what the entry under the cursor is, asked of the session's disk" },
+ .{ .what = "* Tab", .on = .dir, .stop = "kind", .why = "what the entry is (Tab after a prefix still asks)" },
.{ .what = "Save", .on = .any, .stop = "write_file", .why = "a save" },
// A program run, a shell opened or closed: the session's processes.
.{ .what = "Exec *", .on = .any, .stop = "kind", .why = "a program run: its name asked of the disk first" },
@@ -6857,12 +7167,16 @@ test "a far client's every word and key, on a file's pane, a kind's and a shell'
_ = try a.local(.{ .command = "Still" });
const kind = a.core.view().active;
try std.testing.expect(file != kind and a.core.panes[file].?.file != null and a.core.panes[kind].?.addon != null);
+ // A directory's pane (Dir.zig), listed from the disk.
+ _ = try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}/", .{dir}) });
+ const listing = a.core.view().active;
+ try std.testing.expect(a.core.panes[listing].?.file.?.listing != null);
var seen: [may_wait.len]bool = @splat(false);
var hit: [may_wait.len]bool = @splat(false);
var unlisted: u32 = 0;
var tried: u32 = 0;
- const stands = [_]struct { On, ?usize }{ .{ .file, file }, .{ .kind, kind }, .{ .shell, shell }, .{ .shell_normal, shell } };
+ const stands = [_]struct { On, ?usize }{ .{ .file, file }, .{ .kind, kind }, .{ .shell, shell }, .{ .shell_normal, shell }, .{ .dir, listing } };
for (stands) |stand| {
const on, const at = stand;
const id = at orelse continue;
diff --git a/core/tagline.zig b/core/tagline.zig
index afee0659..26f83fba 100644
--- a/core/tagline.zig
+++ b/core/tagline.zig
@@ -160,7 +160,7 @@ pub fn pathPrefix(p: *const Pardes, arena: std.mem.Allocator, pane: *Pane) ![]co
if (pane.terminal != null) {
const chosen = pane.term_petscii;
const on = chosen or !p.view().frontend.native_images;
- if (chosen or panes.Term.hasImages(pane.terminal))
+ if (chosen or pane.term_images)
return std.fmt.allocPrint(arena, "{s} petscii:{s}", .{ (try shownDir(arena, pane)), if (on) "on" else "off" });
}
return (try shownDir(arena, pane));
diff --git a/core/tape.zig b/core/tape.zig
index d1e0527c..f231831d 100644
--- a/core/tape.zig
+++ b/core/tape.zig
@@ -140,6 +140,9 @@ pub const Site = enum(u16) {
/// A kitty image's medium as a terminal's emulator read it (a file, a
/// temporary file, shared memory: Term.media), its bytes or its failure.
medium,
+ /// Whether a terminal holds a kitty image after its program's output
+ /// (Pane.term_images): a screen-state replica's emulator never has one.
+ images,
};
/// What the sites return. wire.semantics hashes their shape with the
@@ -948,11 +951,11 @@ const pane_fields = walk.fieldNames(pardes.Pane);
const file_fields = walk.fieldNames(pardes.panes.File.State);
const places_fields = walk.fieldNames(pardes.panes.Places);
const view_fields = walk.fieldNames(Pardes.View);
-const Parts = [pardes_fields.len + stream_fields.len + pardes.views.max * view_fields.len + pardes.MAX_PANES * (2 + pane_fields.len + file_fields.len + places_fields.len)]u64;
+pub const Parts = [pardes_fields.len + stream_fields.len + pardes.views.max * view_fields.len + pardes.MAX_PANES * (2 + pane_fields.len + file_fields.len + places_fields.len)]u64;
/// The session's digest as one per field of the core's, its stream's, and
/// each pane's and its file's, so a divergence says where it is.
-fn digestParts(p: *const Pardes, parts: *Parts) []const u64 {
+pub fn digestParts(p: *const Pardes, parts: *Parts) []const u64 {
var n = walk.fieldDigests(Pardes, p, parts[0..pardes_fields.len]).len;
n += walk.fieldDigests(pardes.Stream, &p.stream, parts[n..][0..stream_fields.len]).len;
for (&p.views) |*slot| if (slot.*) |*v| {
@@ -974,7 +977,7 @@ fn digestParts(p: *const Pardes, parts: *Parts) []const u64 {
}
/// The fields whose digests differ, named.
-fn partsDiffer(got: []const u64, want: []const u64, a: std.mem.Allocator) []const u8 {
+pub fn partsDiffer(got: []const u64, want: []const u64, a: std.mem.Allocator) []const u8 {
var said: std.ArrayList(u8) = .empty;
if (got.len != want.len) said.print(a, "the panes there are ({d} parts, {d} recorded); ", .{ got.len, want.len }) catch {};
const n = @min(got.len, want.len);
@@ -1312,6 +1315,7 @@ pub fn recordDigest(t: *Tape) void {
t.record(.digest, []const u64, &d);
}
+
/// A core made outside the tape (a replica client's, from a dump) is the
/// one it replays into.
pub fn adoptCore(t: *Tape, p: *Pardes) void {
diff --git a/core/term/terminal.zig b/core/term/terminal.zig
index 1673a3d8..187ed144 100644
--- a/core/term/terminal.zig
+++ b/core/term/terminal.zig
@@ -631,19 +631,30 @@ pub fn resize(state: *State, cols: u16, rows: u16, later: bool) void {
state.resize_to = .{ .cols = cols, .rows = rows };
return;
}
- state.changes +%= 1;
- state.vt.resize(state.gpa, .{ .cols = cols, .rows = rows }) catch {};
+ reflow(state, cols, rows);
}
/// The size `resize` left for later, made; whether there was one.
pub fn resizeDeferred(state: *State) bool {
const size = state.resize_to orelse return false;
state.resize_to = null;
- state.changes +%= 1;
- state.vt.resize(state.gpa, .{ .cols = size.cols, .rows = size.rows }) catch {};
+ reflow(state, size.cols, size.rows);
return true;
}
+/// Its grid reflowed, and its view a row of its history again, as `viewTo`
+/// and `grid` have it: ghostty's view follows a cell, which a widening
+/// can leave mid-row (a wrapped line's second row joined to its first) and
+/// the next narrowing carries a row down, where a view put back at its row
+/// (a replica's rollback, core/replica.zig `putViews`) stays.
+fn reflow(state: *State, cols: u16, rows: u16) void {
+ state.changes +%= 1;
+ state.vt.resize(state.gpa, .{ .cols = cols, .rows = rows }) catch {};
+ inline for (.{ .primary, .alternate }) |which| if (state.vt.screens.get(which)) |screen| {
+ if (screen.pages.viewport == .pin) screen.pages.viewport_pin.x = 0;
+ };
+}
+
/// The emulator's size in pixels follows the core's cells: kitty graphics
/// placements are sized by it, and CSI 14/16 t report it.
pub fn cellPixels(state: *State, w: u32, h: u32) void {
@@ -1591,6 +1602,27 @@ pub fn images(state: *State, gpa: std.mem.Allocator, at: ImagesAt, out: *std.Arr
state.images.draw(state.gpa, gpa, &state.vt, at, rows_of[0..vp_rows], out, frame, frame_cols);
}
+test "a view put back at the row it reports is the view it was, through a reflow: a view on a wrapped line's second row, joined into the row above by a widening and parted again by a narrowing, is the same row put back or not" {
+ // The bytes soak's divergence (reports/pardes-term-join-6.md): a
+ // client's rollback puts each terminal's view back at its row
+ // (core/replica.zig `putViews`), and ghostty's view is a cell, which
+ // a widening leaves mid-row and the next narrowing carries a row down.
+ const gpa = std.testing.allocator;
+ const left = try open(gpa, 20, 2);
+ defer close(left);
+ const put = try open(gpa, 20, 2);
+ defer close(put);
+ for ([_]*State{ left, put }) |s| {
+ // Lines two rows long at 20 columns, one at 40.
+ for (0..30) |_| ingest(s, "abcdefghijklmnopqrstuvwxyz0123\r\n");
+ viewTo(s, 1);
+ resize(s, 40, 2, false);
+ }
+ viewTo(put, grid(put).view);
+ for ([_]*State{ left, put }) |s| resize(s, 20, 2, false);
+ try std.testing.expectEqual(grid(left), grid(put));
+}
+
test "the luminance table answers exactly what ghostty computes" {
for (0..256) |i| {
const c: u8 = @intCast(i);