summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--abi/term.zig3
-rw-r--r--core/Term.zig8
-rw-r--r--core/term/term.zig1
-rw-r--r--core/term/terminal.zig167
4 files changed, 175 insertions, 4 deletions
diff --git a/abi/term.zig b/abi/term.zig
index c437f62f..68434e8e 100644
--- a/abi/term.zig
+++ b/abi/term.zig
@@ -343,6 +343,9 @@ pub const Ops = struct {
snapshot: *const fn (s: *const State, gpa: std.mem.Allocator, w: *std.Io.Writer, slim: bool) anyerror!void,
/// `snapshot`'s bytes and `parts` back into a new terminal.
load: *const fn (gpa: std.mem.Allocator, snapshot: []const u8, parts: *const Parts) anyerror!*State,
+ /// Its emulator made again from its own snapshot where it is (a join:
+ /// every side does it); the snapshot's hash.
+ reload: *const fn (s: *State) anyerror!u64,
// ---- output and view ----
/// One read of its program's output (`Told` says where the tracked
diff --git a/core/Term.zig b/core/Term.zig
index fde36420..5025ba2b 100644
--- a/core/Term.zig
+++ b/core/Term.zig
@@ -123,6 +123,14 @@ pub fn close(s: *State, _: std.mem.Allocator) void {
own.destroy(s);
}
+/// Its emulator made again from its own snapshot, where it is: at a join
+/// every side does it, so that all hold the same pages (ghostty prunes its
+/// history a page at a time, and a snapshot does not say how its rows were
+/// cut into pages). The snapshot's hash, which every side compares.
+pub fn reload(s: *State) !u64 {
+ return the().reload(s.impl);
+}
+
/// When its next kitty animation frame is due (ms on the core's clock), as
/// its last drawn frame left it; null when nothing plays.
pub fn due(s: *const State) ?u64 {
diff --git a/core/term/term.zig b/core/term/term.zig
index 6e36816a..6f62b827 100644
--- a/core/term/term.zig
+++ b/core/term/term.zig
@@ -31,6 +31,7 @@ pub const ops: iface.Ops = .{
.parts = cast(iface.Ops, "parts", terminal_.parts),
.snapshot = cast(iface.Ops, "snapshot", terminal_.snapshot),
.load = cast(iface.Ops, "load", terminal_.load),
+ .reload = cast(iface.Ops, "reload", terminal_.reload),
.feed = cast(iface.Ops, "feed", terminal_.feed),
.grid = cast(iface.Ops, "grid", terminal_.grid),
.viewTo = cast(iface.Ops, "viewTo", terminal_.viewTo),
diff --git a/core/term/terminal.zig b/core/term/terminal.zig
index f92da3fd..a4120f5a 100644
--- a/core/term/terminal.zig
+++ b/core/term/terminal.zig
@@ -301,7 +301,7 @@ pub fn snapshot(s: *const State, gpa: std.mem.Allocator, w: *std.Io.Writer, slim
fn decode(gpa: std.mem.Allocator, bytes: []const u8) !ghostty_vt.snapshot.Decoded {
if (bytes.len == 4) {
const size = std.mem.bytesToValue([2]u16, bytes[0..4]);
- return .{ .terminal = try emulator(gpa, @max(1, size[0]), @max(1, size[1])), .continuation = .ground, .history_rows = .{} };
+ return .{ .terminal = try emulator(gpa, @max(1, size[0]), @max(1, size[1]), scrollback_max), .continuation = .ground, .history_rows = .{} };
}
var reader: std.Io.Reader = .fixed(bytes);
return ghostty_vt.snapshot.decodeExact(gpa, terminalIo(), &reader, .{
@@ -326,6 +326,34 @@ pub fn load(gpa: std.mem.Allocator, bytes: []const u8, saved: *const Term.Parts)
return out;
}
+/// Its emulator made again from its own snapshot, where it is, what pardes
+/// keeps beside it kept: what a replica and its session do at a join, so
+/// that both, and the joiner loaded from the session's, hold the same pages
+/// (ghostty cuts rows into pages, and prunes whole ones, by its history,
+/// which a snapshot does not carry). The snapshot's hash, which every side
+/// compares; on an error it is as it was.
+pub fn reload(s: *State) !u64 {
+ const gpa = s.gpa;
+ var bytes: std.Io.Writer.Allocating = .init(gpa);
+ defer bytes.deinit();
+ try snapshot(s, gpa, &bytes.writer, false);
+ var decoded = try decode(gpa, bytes.written());
+ defer decoded.deinit(gpa);
+ var saved = parts(s);
+ var reply: [@typeInfo(@FieldType(State, "reply")).array.len]u8 = undefined;
+ @memcpy(reply[0..saved.reply.len], saved.reply);
+ saved.reply = reply[0..saved.reply.len];
+ const kept = .{ s.changes, s.read, s.resize_to };
+ if (comptime images_enabled) s.images.deinit(gpa);
+ s.stream.deinit();
+ s.vt.deinit(gpa);
+ s.* = .{ .gpa = gpa, .vt = decoded.toOwned(), .stream = undefined, .changes = kept[0] +% 1, .read = kept[1], .resize_to = kept[2] };
+ // Short of room for its sequence in flight it goes on without it (lost
+ // at the next cut), as ghostty's stream does: not worth a terminal.
+ _ = install(s, decoded.continuation, &saved);
+ return std.hash.Wyhash.hash(0, bytes.written());
+}
+
/// A decoded emulator at its address made a terminal: its stream wired,
/// the sequence in flight fed again, what pardes kept beside it put back;
/// whether its stream had all the room it wanted (`wire`).
@@ -770,11 +798,14 @@ pub fn close(state: *State) void {
}
/// A terminal pane's emulator, `cols` by `rows`, as `open` makes one.
-fn emulator(gpa: std.mem.Allocator, cols: u16, rows: u16) !ghostty_vt.Terminal {
+/// How much history a terminal keeps, in bytes of ghostty's pages.
+const scrollback_max = 16 * 1024 * 1024;
+
+fn emulator(gpa: std.mem.Allocator, cols: u16, rows: u16, history: usize) !ghostty_vt.Terminal {
return ghostty_vt.Terminal.init(terminalIo(), gpa, .{
.cols = cols,
.rows = rows,
- .max_scrollback_bytes = 16 * 1024 * 1024,
+ .max_scrollback_bytes = history,
.kitty_image_storage_limit = if (images_enabled) TerminalImages.storage_limit else 0,
// Every kitty medium, as ghostty and kitty take them: a file (t=f),
// a temporary file it then deletes (t=t, in the temp directory and
@@ -789,11 +820,17 @@ fn emulator(gpa: std.mem.Allocator, cols: u16, rows: u16) !ghostty_vt.Terminal {
/// A new emulator, `cols` by `rows`, allocated with `gpa`, its own from
/// then on (`State.gpa`).
pub fn open(gpa: std.mem.Allocator, cols: u16, rows: u16) !*State {
+ return openKeeping(gpa, cols, rows, scrollback_max);
+}
+
+/// `open`, keeping `history` bytes of history (the tests below fill a
+/// smaller one).
+fn openKeeping(gpa: std.mem.Allocator, cols: u16, rows: u16, history: usize) !*State {
const state = try gpa.create(State);
errdefer gpa.destroy(state);
// Every other field its default, never what the heap had: a digest
// reads them, and two replicas must agree (core/replica.zig).
- state.* = .{ .gpa = gpa, .vt = try emulator(gpa, cols, rows), .stream = undefined };
+ state.* = .{ .gpa = gpa, .vt = try emulator(gpa, cols, rows, history), .stream = undefined };
if (!state.wire()) {
state.stream.deinit();
state.vt.deinit(gpa);
@@ -1626,3 +1663,125 @@ test "a snapshot taken inside a sequence carries it: over pardes's own handler t
};
};
}
+
+/// What the join tests below stream: plain text, a build's log, and heavy
+/// colour and links (a truecolour a cell, a link a word, marks piled on
+/// letters, wide characters: what runs a page out of room for styles,
+/// links and graphemes), `lines` of it, no clear.
+const Corpus = enum { text, logs, heavy };
+
+fn corpus(gpa: std.mem.Allocator, which: Corpus, lines: usize) ![]u8 {
+ var out: std.Io.Writer.Allocating = .init(gpa);
+ errdefer out.deinit();
+ const w = &out.writer;
+ var prng: std.Random.DefaultPrng = .init(@as(u64, @intFromEnum(which)) + 11);
+ const random = prng.random();
+ const words = [_][]const u8{ "the", "quick", "brown", "fox", "jumps", "over", "a", "lazy", "dog", "./core/replica.zig:1234:", "error:", "expected", "\u{6F22}\u{5B57}", "na\u{EF}ve" };
+ for (0..lines) |i| switch (which) {
+ .text => {
+ for (0..random.uintLessThan(usize, 18)) |_| try w.print("{s} ", .{words[random.uintLessThan(usize, words.len)]});
+ try w.print("{d}\r\n", .{i});
+ },
+ .logs => try w.print("[{d:>3}%] \x1b[32mBuilding CXX object\x1b[0m src/CMakeFiles/core.dir/module_{d}.cpp.o{s}\r\n", .{ i * 100 / lines, i, if (i % 9 == 0) " \x1b[1;33mwarning:\x1b[0m unused variable" else "" }),
+ .heavy => switch (i % 3) {
+ 0 => {
+ for (0..100) |x| try w.print("\x1b[38;2;{d};{d};{d}m{c}", .{ (i * 7 + x * 3) % 256, (i * 13 + x * 5) % 256, (x * 11 + i) % 256, @as(u8, @intCast(97 + (x + i) % 26)) });
+ try w.writeAll("\x1b[0m\r\n");
+ },
+ 1 => {
+ for (0..18) |x| try w.print("\x1b]8;;https://example.com/{d}/{d}\x1b\\w{d}\x1b]8;;\x1b\\ ", .{ i, x, x });
+ try w.writeAll("\r\n");
+ },
+ else => {
+ for (0..40) |x| {
+ try w.writeByte(@intCast(97 + (x + i) % 26));
+ for (0..1 + x % 6) |j| try w.print("{u}", .{@as(u21, @intCast(0x300 + (x * 3 + j + i) % 0x6f))});
+ }
+ try w.writeAll(" \u{6F22}\u{5B57}\u{6F22}\u{5B57} \u{1F469}\u{200D}\u{1F4BB}\r\n");
+ },
+ },
+ };
+ return out.toOwnedSlice();
+}
+
+/// `bytes[from..to]` fed in reads of 4 KiB, and two reflows on the way
+/// at `stops` (to a little over half the width and three rows taller, then
+/// back), as the join tests' every side takes them.
+fn feedReflowing(s: *State, bytes: []const u8, from: usize, to: usize, stops: [2]usize) void {
+ const cols = s.vt.cols;
+ const rows = s.vt.rows;
+ var at = from;
+ while (at < to) {
+ if (at == stops[0]) resize(s, cols / 2 + 1, rows + 3, false);
+ if (at == stops[1]) resize(s, cols * 2 - 2, rows - 3, false);
+ var end = @min(to, at + 4096);
+ for (stops) |stop| if (at < stop and stop < end) {
+ end = stop;
+ };
+ s.stream.nextSlice(bytes[at..end]);
+ at = end;
+ }
+}
+
+fn sameSnapshot(gpa: std.mem.Allocator, a: *const State, b: *const State) !bool {
+ const x = try snapshotOf(gpa, a);
+ defer gpa.free(x);
+ const y = try snapshotOf(gpa, b);
+ defer gpa.free(y);
+ return std.mem.eql(u8, x, y);
+}
+
+test "a join at a cut: every side reloaded from its own snapshot holds the same terminal as the joiner, after more bytes and after two reflows; a joiner nobody reloaded for with heavy colour does not" {
+ // The sizing pass's join check (reports/pardes-term-bytes.md, section 2)
+ // in the tree: a scrollback past its limit (pruned a page at a time,
+ // by the history of its pages), cut at a random read boundary, often
+ // inside a sequence.
+ const gpa = std.testing.allocator;
+ var prng: std.Random.DefaultPrng = .init(29);
+ const random = prng.random();
+ var heavy_apart: usize = 0;
+ for ([_]Corpus{ .text, .logs, .heavy }, [_]usize{ 6000, 6000, 3000 }) |which, lines| {
+ const bytes = try corpus(gpa, which, lines);
+ defer gpa.free(bytes);
+ for (0..2) |_| {
+ const cut = bytes.len / 3 + random.uintLessThan(usize, bytes.len / 3);
+ const mid = cut + (bytes.len - cut) / 2;
+ const stops: [2]usize = .{ mid + (bytes.len - mid) / 3, mid + 2 * (bytes.len - mid) / 3 };
+ // The session, and a replica that was there from the start.
+ const session = try openKeeping(gpa, 120, 40, 1 << 20);
+ defer close(session);
+ const there = try openKeeping(gpa, 120, 40, 1 << 20);
+ defer close(there);
+ feedReflowing(session, bytes, 0, cut, stops);
+ feedReflowing(there, bytes, 0, cut, stops);
+ // The same join made without the reload: the session as it
+ // would go on, and a joiner loaded from its snapshot.
+ const lineage = try openKeeping(gpa, 120, 40, 1 << 20);
+ defer close(lineage);
+ feedReflowing(lineage, bytes, 0, cut, stops);
+ const before = try snapshotOf(gpa, session);
+ defer gpa.free(before);
+ const unreloaded = try load(gpa, before, &parts(session));
+ defer close(unreloaded);
+ // The join as it is made: every side reloads, the joiner loads
+ // the session's snapshot taken after (the join's dump).
+ const hash = try reload(session);
+ try std.testing.expectEqual(hash, try reload(there));
+ try std.testing.expectEqual(hash, std.hash.Wyhash.hash(0, before));
+ const after = try snapshotOf(gpa, session);
+ defer gpa.free(after);
+ const joiner = try load(gpa, after, &parts(session));
+ defer close(joiner);
+ for ([_]*State{ session, there, joiner, unreloaded, lineage }) |s| feedReflowing(s, bytes, cut, mid, stops);
+ try expectSame(gpa, session, there);
+ try expectSame(gpa, session, joiner);
+ if (!try sameSnapshot(gpa, lineage, unreloaded)) heavy_apart += @intFromBool(which == .heavy);
+ for ([_]*State{ session, there, joiner }) |s| feedReflowing(s, bytes, mid, bytes.len, stops);
+ try expectSame(gpa, session, there);
+ try expectSame(gpa, session, joiner);
+ }
+ }
+ // The fault the reload mends is real: a joiner nobody reloaded for is
+ // another terminal once heavy colour has pruned pages cut otherwise.
+ try std.testing.expectEqual(2, heavy_apart);
+}