From 70de5ec5ff3dad01afcc45244ad5e5a481486da6 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 17:01:35 -0300 Subject: dump: one walker dump of the whole session, by name across builds, for Restore, -l, Handover and the store; dump.zig's hand-written schema gone A dump is walk.zig's walk of the core with a table of every type it reaches (fields, enum and union names, number widths), read back by name; terminals are ghostty's snapshot (screens, history, kitty images); a failed load frees what it made. Co-Authored-By: Claude Opus 5.5 --- src/File.zig | 387 ----------------- src/Handover.zig | 112 +++-- src/Output.zig | 34 +- src/Pipe.zig | 4 + src/Query.zig | 9 +- src/Store.zig | 57 +-- src/builtins.zig | 2 +- src/dump.zig | 1176 ++++++++++++-------------------------------------- src/file_watch.zig | 57 +-- src/image.zig | 63 --- src/ninep/ctl.zig | 64 +-- src/ninep/events.zig | 24 +- src/panes.zig | 4 +- src/pardes.zig | 55 ++- src/pdf.zig | 4 +- src/pdf_view.zig | 14 + src/tagline.zig | 8 - src/tape.zig | 40 +- src/terminal.zig | 316 +++++++------- src/walk.zig | 1120 ++++++++++++++++++++++++++++++++++++++++++++++- 20 files changed, 1771 insertions(+), 1779 deletions(-) (limited to 'src') diff --git a/src/File.zig b/src/File.zig index 84222191..48a996a4 100644 --- a/src/File.zig +++ b/src/File.zig @@ -13,7 +13,6 @@ const filesystem = @import("fs.zig"); const syntax = @import("syntax.zig"); const diff = @import("diff.zig"); const tracy = @import("tracy.zig"); -const dump = @import("dump.zig"); const limits = @import("memory.zig").limits; const locations = @import("locations.zig"); const Colors = @import("colors.zig"); @@ -221,231 +220,6 @@ pub const State = struct { pub const NodeStep = struct { ranges: []modal.Selection, primary: usize }; -pub fn dumpPane( - p: *Pardes, - arena: std.mem.Allocator, - pane: *const Pane, - file: *const State, - tag: []const u8, - body: []const u8, - scroll: usize, - origin: []const u8, - origin_arg: []const u8, -) !dump.Pane { - const dirty = file.revision != file.saved_revision; - const on_disk = onDisk(file.path); - // A clean file's text is its file's: read from disk when restored, so - // a change made there since is what comes back. Any other is kept, once. - const from_disk = file.output == null and !dirty and on_disk; - // What an unsaved pane's text was read against: the disk as it is, or, - // when a change there was seen already, something it is not. - const disk_hash: ?u64 = if (file.output != null or !dirty or !on_disk) null else if (file.disk_newer) |newer| newer +% 1 else blk: { - const bytes = filesystem.read(p, file.path) catch break :blk null; - defer p.gpa.free(bytes); - break :blk std.hash.Wyhash.hash(0, bytes); - }; - return .{ - .kind = .file, - .tag = tag, - .body = body, - .scroll = scroll, - .cols = pane.cols, - .rows = pane.rows, - .vweight = pane.vweight, - .collapsed = pane.collapsed, - .file = .{ - .tree_context = file.tree_context, - // A query pane's rows are its own (line 1 the query, gutter - // numbers), not location rows Restore reads: it comes back as - // plain text. - .location_rows = if (pane.query != null) &.{} else try dumpLocationRows(arena, file.location_rows), - .path = file.path, - .content_b64 = if (from_disk) "" else try dump.encodeBytes(arena, file.content), - .dirty = dirty, - .disk_hash = disk_hash, - .on_disk = on_disk, - .origin = origin, - .origin_arg = origin_arg, - .mini_source = if (file.mini) |mini| mini.source else "", - .mini_colors_b64 = if (file.mini) |mini| try dump.encodeBytes(arena, mini.colors) else "", - }, - }; -} - -fn dumpLocationRows(arena: std.mem.Allocator, rows: []const locations.Row) ![]const dump.LocationRow { - const saved = try arena.alloc(dump.LocationRow, rows.len); - for (rows, saved) |row, *out| out.* = .{ - .kind = @fromBackingInt(@intCast(@backingInt(row.kind))), - .declaration = row.declaration, - .mark_occurrence = row.mark_occurrence, - .source_column = row.source_column, - .path = row.path, - .line = row.at.line, - .col = row.at.col, - .end_line = row.at.end_line, - .end_col = row.at.end_col, - .location_end = row.location_end, - .code_start = row.code_start, - .colors_b64 = try dump.encodeBytes(arena, row.colors), - }; - return saved; -} - -fn sameSavedLocation(a: dump.LocationRow, b: dump.LocationRow) bool { - return std.mem.eql(u8, a.path, b.path) and a.line == b.line and a.col == b.col and - a.end_line == b.end_line and a.end_col == b.end_col; -} - -fn restoreLocationRows(gpa: std.mem.Allocator, content: []const u8, saved: []const dump.LocationRow) ![]locations.Row { - return restoreOutputLocationRows(gpa, content, saved, false); -} - -fn restoreOutputLocationRows(gpa: std.mem.Allocator, content: []const u8, saved: []const dump.LocationRow, callstack: bool) ![]locations.Row { - if (saved.len == 0) return &.{}; - if (saved.len > std.mem.count(u8, content, "\n") + 1) return error.InvalidLocationRows; - const rows = try gpa.alloc(locations.Row, saved.len); - var initialized: usize = 0; - errdefer { - for (rows[0..initialized]) |row| { - gpa.free(row.path); - gpa.free(row.colors); - } - gpa.free(rows); - } - var lines = std.mem.splitScalar(u8, content, '\n'); - var previous_line: []const u8 = ""; - for (saved, rows, 0..) |row, *out, index| { - const line = lines.next() orelse return error.InvalidLocationRows; - if ((!callstack and row.path.len == 0) or row.path.len >= 4096 or - row.location_end > row.code_start or row.code_start > line.len or - (row.kind == .match and row.location_end == 0) or - (row.declaration and row.kind != .context) or - row.line > std.math.maxInt(i32) or row.col > std.math.maxInt(i32) or - row.end_line > std.math.maxInt(i32) or row.end_col > std.math.maxInt(i32)) - return error.InvalidLocationRows; - if (callstack) { - if (row.location_end > 0) { - const prefix = locations.treePrefix(line); - const parsed = locations.parseRaw(line[prefix.end..]); - if (row.kind != .match or parsed.at.line == 0 or - !std.mem.eql(u8, parsed.path, row.path) or - parsed.at.line != row.line or parsed.at.col != row.col or - parsed.at.end_line != row.end_line or parsed.at.end_col != row.end_col or - prefix.end + parsed.end != row.location_end or - prefix.end + parsed.code_start != row.code_start) return error.InvalidLocationRows; - } else if (row.kind == .preview) { - if (index == 0 or saved[index - 1].kind != .match or !sameSavedLocation(saved[index - 1], row)) return error.InvalidLocationRows; - } else if (row.kind != .context or (row.path.len == 0 and (row.line != 0 or row.code_start != line.len))) return error.InvalidLocationRows; - } else { - const inline_row = row.code_start > row.location_end and row.code_start > 0 and line[row.code_start - 1] == '\t'; - const address_row = !inline_row and row.location_end > 0 and row.code_start == line.len; - const source_row = row.location_end == 0 and row.code_start == 0; - if (!inline_row and !address_row and !source_row) return error.InvalidLocationRows; - if (row.kind == .preview) { - if (!source_row or index == 0) return error.InvalidLocationRows; - const previous = saved[index - 1]; - if (previous.kind != .match or previous.location_end == 0 or previous.code_start != previous_line.len or previous_line[previous_line.len - 1] == '\t' or - !sameSavedLocation(previous, row)) return error.InvalidLocationRows; - } - if (address_row) { - if (index + 1 >= saved.len) return error.InvalidLocationRows; - const following = saved[index + 1]; - const expected: @TypeOf(row.kind) = if (row.kind == .match) .preview else .context; - if (following.kind != expected or following.code_start != 0 or following.location_end != 0 or - following.declaration != row.declaration or !sameSavedLocation(row, following)) return error.InvalidLocationRows; - } - } - const code_len = line.len - row.code_start; - if (row.colors_b64.len > std.base64.standard.Encoder.calcSize(code_len)) return error.InvalidLocationRows; - const colors = try dump.decodeBytes(gpa, row.colors_b64); - errdefer gpa.free(colors); - if (colors.len != 0 and colors.len != code_len) return error.InvalidLocationRows; - for (colors) |color| if (color > @backingInt(syntax.Syn.comment)) return error.InvalidLocationRows; - const path = try gpa.dupe(u8, row.path); - out.* = .{ - .kind = @fromBackingInt(@intCast(@backingInt(row.kind))), - .declaration = row.declaration, - .mark_occurrence = row.mark_occurrence, - .source_column = row.source_column, - .path = path, - .at = .{ .line = row.line, .col = row.col, .end_line = row.end_line, .end_col = row.end_col }, - .location_end = row.location_end, - .code_start = row.code_start, - .colors = colors, - }; - initialized += 1; - previous_line = line; - } - if (lines.next()) |remaining| if (remaining.len != 0 or lines.next() != null) return error.InvalidLocationRows; - return rows; -} - -test "a clean pane whose file was never on disk comes back clean from a Restore; one whose file has gone is said deleted" { - if (comptime !pardes.hosted) return; - for ([_]bool{ false, true }) |was_on_disk| { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - const id = p.freeSlot().?; - const pane = try restore(p, id, .{ - .kind = .file, - .tag = "", - .body = "", - .file = .{ .path = "/tmp/pardes-never-written/init", .content = "", .on_disk = was_on_disk }, - }); - try std.testing.expectEqual(was_on_disk, pane.file.?.disk_gone); - try std.testing.expectEqual(pane.file.?.saved_revision, pane.file.?.revision); - } -} - -test "location metadata restore rejects offsets and invalid syntax bytes" { - const gpa = std.testing.allocator; - var row: dump.LocationRow = .{ - .kind = .context, - .path = "x.zig", - .line = 1, - .location_end = 0, - .code_start = 3, - }; - const rows = try restoreLocationRows(gpa, "| \tx\n", &.{row}); - defer locations.freeRows(gpa, rows); - try std.testing.expectEqualStrings("x.zig", rows[0].path); - row.code_start = 8; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, "| \tx\n", &.{row})); - row.code_start = 3; - row.colors_b64 = "/w=="; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, "| \tx\n", &.{row})); - row.colors_b64 = ""; - row.kind = .match; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, "| \tx\n", &.{row})); -} - -test "stacked location metadata requires matching adjacent preview ownership" { - const gpa = std.testing.allocator; - const header = "x.c:2:1-3"; - const content = header ++ "\nfoo\n"; - const match: dump.LocationRow = .{ .kind = .match, .path = "x.c", .line = 2, .col = 1, .end_line = 2, .end_col = 3, .location_end = header.len, .code_start = header.len }; - const preview: dump.LocationRow = .{ .kind = .preview, .path = "x.c", .line = 2, .col = 1, .end_line = 2, .end_col = 3, .location_end = 0, .code_start = 0 }; - const restored = try restoreLocationRows(gpa, content, &.{ match, preview }); - defer locations.freeRows(gpa, restored); - try std.testing.expectEqual(.preview, restored[1].kind); - try std.testing.expectEqual(restored[0].at, restored[1].at); - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, "foo\n", &.{preview})); - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, header ++ "\n", &.{match})); - var inline_match = match; - inline_match.code_start += 1; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, header ++ "\t\nfoo\n", &.{ inline_match, preview })); - var wrong = preview; - wrong.path = "other.c"; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, content, &.{ match, wrong })); - wrong = preview; - wrong.line = 3; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, content, &.{ match, wrong })); - wrong = preview; - wrong.code_start = 1; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, content, &.{ match, wrong })); -} - pub fn graphemeDisplayWidth(grapheme: []const u8) usize { if (std.mem.eql(u8, grapheme, "\t")) return config.tab_now; if (grapheme.len == 1 and grapheme[0] >= 0x20 and grapheme[0] < 0x7f) return 1; @@ -1026,128 +800,6 @@ pub fn open(p: *Pardes, id: usize, path: []const u8, line: usize) !*Pane { return pane; } -pub fn restore(p: *Pardes, id: usize, src: dump.Pane) !*Pane { - const saved = src.file.?; - if (saved.mini_source.len > 0) { - const encoded_limit = std.base64.standard.Encoder.calcSize(Mini.max_output_bytes); - if (saved.mini_source.len >= 4096 or saved.content.len > Mini.max_output_bytes or - saved.content_b64.len > encoded_limit or saved.mini_colors_b64.len > encoded_limit) - return error.InvalidMini; - } - // A clean file's text is read from its file: one changed on disk since - // the dump comes back changed, never as the old text marked clean (and - // a dump from before, which kept it, is read so too). Gone from disk, the - // dump's text if it kept one. - const from_disk: ?[]u8 = if (Output.fromWord(saved.origin) == null and !saved.dirty and saved.on_disk and - saved.mini_source.len == 0 and filesystem.localPath(saved.path) != null) - filesystem.read(p, saved.path) catch null - else - null; - const content: []u8 = if (from_disk) |bytes| bytes else if (saved.content_b64.len > 0) - try dump.decodeBytes(p.gpa, saved.content_b64) - else - try p.gpa.dupe(u8, saved.content); - errdefer p.gpa.free(content); - const path = try p.gpa.dupe(u8, saved.path); - errdefer p.gpa.free(path); - - const output: ?Output.State = if (Output.fromWord(saved.origin)) |origin| blk: { - var value: Output.State = .{ .from = origin }; - try Output.setArg(&value, saved.origin_arg); - break :blk value; - } else null; - - if (saved.location_rows.len != 0 and output == null) return error.InvalidLocationRows; - const is_callstack = if (output) |state| std.meta.eql(state.from, Output.Origin{ .query = .callstack }) else false; - const location_rows = try restoreOutputLocationRows(p.gpa, content, saved.location_rows, is_callstack); - errdefer locations.freeRows(p.gpa, location_rows); - - var mini: ?Mini.State = null; - errdefer if (mini) |*state| state.deinit(p.gpa); - if (saved.mini_source.len > 0) { - if (output == null or !std.meta.eql(output.?.from, Output.Origin{ .cmd = .Mini })) return error.InvalidMini; - const source = try p.gpa.dupe(u8, saved.mini_source); - errdefer p.gpa.free(source); - const colors = try dump.decodeBytes(p.gpa, saved.mini_colors_b64); - errdefer p.gpa.free(colors); - if (colors.len != content.len or colors.len > Mini.max_output_bytes) return error.InvalidMini; - for (colors) |color| if (color > @backingInt(syntax.Syn.comment)) return error.InvalidMini; - mini = .{ .source = source, .colors = colors }; - } else if (saved.mini_colors_b64.len > 0) return error.InvalidMini; - - const history = try History.create(p.gpa); - errdefer p.gpa.destroy(history); - const pane = try p.newDocPane(id); - pane.file = .{ - .path = path, - .content = content, - .output = output, - .mini = mini, - .location_rows = location_rows, - .scroll = src.scroll, - .revision = @intFromBool(saved.dirty), - // Restored with unsaved text, it has text to lose as an edited pane - // has: a hash its text does not match, so Exit, Restore, Del and - // get ask first (builtins.warnModifiedIn reads only edited panes). - .saved_hash = if (saved.dirty) std.hash.Wyhash.hash(0, content) +% 1 else null, - .history = history, - }; - pane.file.?.tree_context = saved.tree_context and supportsContext(pane); - pane.body.cur_pinned = true; - pane.body.cur_row = @intCast(src.scroll); - // Its dot as it was, where the view the dump kept shows it. - if (saved.dot) |dot| if (dot[0] <= dot[1] and dot[1] <= content.len) { - const a = modal.positionAt(content, dot[0]); - const h = modal.positionAt(content, if (dot[1] > dot[0]) modal.prevGrapheme(content, dot[1]) else dot[0]); - pane.body.vsel = .{ .active = dot[1] > dot[0], .row = @intCast(a.row), .col = @intCast(a.col), .explicit = true }; - pane.body.cur_row = @intCast(h.row); - pane.body.cur_col = @intCast(h.col); - }; - pane.cols = @max(1, src.cols); - pane.rows = @max(1, src.rows); - // A directory pane is its directory as it is now, laid out at the next - // sync; one that cannot be read keeps the text the dump kept. - if (output) |o| if (o.from == .dir) { - const f = &pane.file.?; - if (panes.Dir.read(p.gpa, panes.Dir.ofName(path))) |names| { - f.listing = .{ .names = names, .revision = f.revision }; - } else |_| f.listing = .{ .names = &.{}, .revision = f.revision -% 1 }; - }; - if (output == null and filesystem.localPath(path) != null) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true, .mode = .baseline_disk } }); - // Its file gone from disk since the dump: its text is nowhere else now, - // so it comes back as one deleted on disk, dirty, and Del, Exit and - // Restore ask before throwing it away. - // One never on disk (Config's new file, a name not yet saved) is - // restored as it was: clean, and nothing deleted. - if (output == null and saved.on_disk and !onDisk(path)) deleted(p, @intCast(id)); - // Unsaved, and its file changed on disk since the text was read: said, - // and its first Save asks before overwriting the newer file. - if (saved.disk_hash) |read_against| if (output == null and onDisk(path)) { - if (filesystem.read(p, path)) |bytes| { - defer p.gpa.free(bytes); - if (std.hash.Wyhash.hash(0, bytes) != read_against) changed(p, @intCast(id), bytes); - } else |_| {} - }; - // An older dump says nothing of what the text was read against: its - // first Save asks, as for a file changed on disk, rather than guess. - if (saved.dirty and saved.disk_hash == null and output == null and onDisk(path)) { - if (filesystem.read(p, path)) |bytes| { - defer p.gpa.free(bytes); - const f = &pane.file.?; - f.disk_newer = std.hash.Wyhash.hash(0, bytes); - f.disk_newer_warned = false; - } else |_| {} - } - return pane; -} - -/// Whether `path` names a file on this host's disk now. -fn onDisk(path: []const u8) bool { - if (comptime !pardes.hosted) return false; - const local = filesystem.localPath(path) orelse return false; - return @import("exec.zig").kindOf(local) != null; -} - pub fn deinit(p: *Pardes, pane: *Pane, file: *State) void { if (file.output == null) for (p.panes, 0..) |slot, id| { if (slot == pane) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = false } }); @@ -1485,45 +1137,6 @@ pub fn leaveTwins(p: *Pardes, f: *State) void { if (n == 1) p.panes[twins[0]].?.file.?.twin = 0; } -/// Restore's: pane `id`, restored on its own, back on pane `of`'s buffer -/// as the Zerox twin it was dumped as (dump.File.twin_of). Not a file pane -/// there (a dump edited by hand), it stays on its own. -pub fn relink(p: *Pardes, of: usize, id: usize, dot: ?[2]u32) void { - const src = p.panes[of] orelse return; - const pane = p.panes[id] orelse return; - const sf = if (src.file) |*file| file else return; - const f = if (pane.file) |*file| file else return; - if (sf.output != null or f.output != null or sf.mini != null) return; - if (sf.twin == 0) { - const group = p.gpa.dupe(u8, sf.path) catch return; - p.twin_groups.append(p.gpa, .{ .id = p.next_twin, .saved_revision = sf.saved_revision, .saved_hash = sf.saved_hash, .saved_len = sf.saved_len, .disk_newer = sf.disk_newer, .disk_gone = sf.disk_gone, .path = group }) catch return p.gpa.free(group); - sf.twin = p.next_twin; - p.next_twin +%= 1; - } - pardes.memory.freeUnfilled(p.gpa, f.content); - if (f.line_starts.len > 0) pardes.memory.freeRaw(p.gpa, f.line_starts); - f.history.destroy(p.gpa); - f.content = sf.content; - f.line_starts = sf.line_starts; - f.history = sf.history; - f.revision = sf.revision; - f.saved_revision = sf.saved_revision; - f.saved_hash = sf.saved_hash; - f.saved_len = sf.saved_len; - f.twin = sf.twin; - if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights); - f.highlights = &.{}; - f.syntax_dirty = true; - // Its dot as dumped, read now that its text is back. - if (dot) |d| if (d[0] <= d[1] and d[1] <= f.content.len) { - const a = modal.positionAt(f.content, d[0]); - const h = modal.positionAt(f.content, if (d[1] > d[0]) modal.prevGrapheme(f.content, d[1]) else d[0]); - pane.body.vsel = .{ .active = d[1] > d[0], .row = @intCast(a.row), .col = @intCast(a.col), .explicit = true }; - pane.body.cur_row = @intCast(h.row); - pane.body.cur_col = @intCast(h.col); - }; -} - pub const TwinGroup = struct { id: u32, saved_revision: u32, diff --git a/src/Handover.zig b/src/Handover.zig index 6124e49f..549e4d58 100644 --- a/src/Handover.zig +++ b/src/Handover.zig @@ -1,7 +1,8 @@ //! Panes between windows, as a browser's tabs go between its windows. A //! pane's grip, or a column's, released past the window's edge carries it -//! out: dumped alone (dump.dumpBytes, `carried`), it opens in a new window -//! of its own, `pardes-gui -l` of that dump. The pane closes here only once +//! out: dumped alone (`Carried`, each pane as dump.savePane makes it), it +//! opens in a new window of its own, `pardes-gui -l` of that dump. The pane +//! closes here only once //! the new window answers on its 9P socket, so its text is never in no //! window at all; a failed handover leaves it where it was, and a pane //! edited while it was on its way stays here too (both then have it). @@ -25,15 +26,33 @@ const dump = @import("dump.zig"); const ninep_io = @import("9p_io.zig"); const panes = @import("panes.zig"); const tagline = @import("tagline.zig"); +const walk = @import("walk.zig"); const Pardes = pardes.Pardes; const MAX_PANES = pardes.MAX_PANES; const libc = std.c; +/// What a grip carries out of its window: one pane with the panes attached +/// to it, or a whole column. +pub const Kind = enum { pane, column }; + +/// A handover's dump: `carried_magic`, then this. +const Carried = struct { + kind: Kind, + /// A carried column's tag. + tag: ?[]const u8 = null, + /// Each pane as dump.savePane makes it, top to bottom. + panes: []const []const u8, + weights: []const f32, + /// Which of them each one is attached to (layout.zig, "Attached panes"). + owners: []const ?u32, +}; +pub const carried_magic = "pardes-carried\n"; + /// The handover in flight, one at a time: its number, what it carries, /// and each carried pane's serial and revision as it was dumped. seq: u32 = 0, flying: bool = false, -carried: dump.Carried = .pane, +carried: Kind = .pane, column_serial: u32 = 0, count: usize = 0, serials: [MAX_PANES]u32 = @splat(0), @@ -44,6 +63,10 @@ bytes: ?[]u8 = null, /// its answer comes in (`finish`), cancelled at the session's end. task: ?struct { future: std.Io.Future(void), io: std.Io } = null, +/// The dump in flight and its carrier are the host's errand, not the +/// session's (src/walk.zig). +pub const replica = .{ .authority = .{ "bytes", "task" } }; + pub fn deinit(h: *Handover, gpa: std.mem.Allocator) void { // The session ends with the carrier still out: it stops at its next // wait (a 9P call it is in finishes first, within its own budget). @@ -62,8 +85,7 @@ pub fn deinit(h: *Handover, gpa: std.mem.Allocator) void { /// an attachment), or column `at`, released past the window's edge: dumped, /// and the host asked to carry it. Only text panes travel: a terminal's /// shell is this process's child and would die with the pane here. -pub fn begin(h: *Handover, p: *Pardes, carried: dump.Carried, at: usize) void { - var keep: [MAX_PANES]bool = @splat(false); +pub fn begin(h: *Handover, p: *Pardes, carried: Kind, at: usize) void { var ids: [MAX_PANES]usize = undefined; var n: usize = 0; switch (carried) { @@ -92,8 +114,7 @@ pub fn begin(h: *Handover, p: *Pardes, carried: dump.Carried, at: usize) void { else "only text panes go to another window, for now"); } - for (ids[0..n]) |id| keep[id] = true; - const bytes = dump.dumpBytes(p, &keep, carried) catch |err| return p.reportError(said, "handover", err); + const bytes = dumpCarried(p, carried, at, ids[0..n]) catch |err| return p.reportError(said, "handover", err); h.deinit(p.gpa); h.bytes = bytes; h.seq +%= 1; @@ -158,6 +179,31 @@ pub fn finish(h: *Handover, p: *Pardes, seq: u32, to: []const u8, failure: ?[]co std.fmt.bufPrint(&buf, "copied to {s}; edited on the way, kept here too", .{to}) catch "copied"); } +/// The dump of panes `ids` (gpa's), carried as `kind` from column `at`'s +/// (a pane's, `at` is its slot). +fn dumpCarried(p: *Pardes, kind: Kind, at: usize, ids: []const usize) ![]u8 { + const arena = p.scratch.allocator(); + const bytes = try arena.alloc([]const u8, ids.len); + const weights = try arena.alloc(f32, ids.len); + const owners = try arena.alloc(?u32, ids.len); + for (ids, 0..) |id, i| { + const pane = p.panes[id].?; + bytes[i] = try dump.savePane(arena, pane, false); + weights[i] = pane.vweight; + const owner = layout.ownerOf(p, id); + owners[i] = if (owner) |o| if (std.mem.indexOfScalar(usize, ids, o)) |k| @intCast(k) else null else null; + } + const value: Carried = .{ + .kind = kind, + .tag = if (kind == .column) p.col_tags[at].own else null, + .panes = bytes, + .weights = weights, + .owners = owners, + }; + const body = try walk.save(Carried, arena, &value); + return std.mem.concat(p.gpa, u8, &.{ carried_magic, body }); +} + // ---- the receiving window ---- /// A carried dump (`begin`'s) taken into this window where the pointer is: @@ -166,13 +212,12 @@ pub fn finish(h: *Handover, p: *Pardes, seq: u32, to: []const u8, failure: ?[]co /// alone. Every other pane stays where it was. Answers the slot of the /// first pane taken; on an error nothing of it is left here. pub fn adopt(p: *Pardes, bytes: []const u8) !usize { - var parsed = try dump.readZon(p.gpa, bytes, "adopt"); - defer parsed.deinit(); - const st = parsed.value; - const carried = st.carried orelse return error.NotCarried; - if (st.columns.len != 1 or st.columns[0].panes.len == 0) return error.NotCarried; - const order = st.columns[0].panes; - for (order) |i| if (i >= st.panes.len or st.panes[i].kind != .file) return error.NotText; + if (!std.mem.startsWith(u8, bytes, carried_magic)) return error.NotCarried; + var arena: std.heap.ArenaAllocator = .init(p.gpa); + defer arena.deinit(); + const st = try walk.load(Carried, arena.allocator(), bytes[carried_magic.len..], null); + const carried = st.kind; + if (st.panes.len == 0 or st.weights.len != st.panes.len or st.owners.len != st.panes.len) return error.NotCarried; // Where the pointer is, else the middle of the pane with the keyboard. layout.compute(p); const here = p.rects[p.active]; @@ -181,19 +226,17 @@ pub fn adopt(p: *Pardes, bytes: []const u8) !usize { var slots: [MAX_PANES]usize = undefined; var n: usize = 0; errdefer for (slots[0..n]) |slot| p.removePane(slot, null) catch {}; - for (order) |i| { + for (st.panes) |one| { const slot = p.freeSlot() orelse return error.TooManyPanes; - const src = st.panes[i]; - const pane = try panes.File.restore(p, slot, src); + const pane = try dump.loadPane(p, slot, one); slots[n] = slot; n += 1; - tagline.restoreDumpTail(p, pane, src); - pane.collapsed = false; + if (pane.file == null) return error.NotText; } // An attachment names its owner by its place in the dump. - for (order, 0..) |i, k| { - const owner = st.panes[i].attached_to orelse continue; - const at = std.mem.indexOfScalar(usize, order, owner) orelse continue; + for (st.owners, 0..) |owner, k| { + const at = owner orelse continue; + if (at >= n) continue; p.panes[slots[k]].?.attached_to = p.panes[slots[at]].?.serial; } const tc = layout.targetColumn(p, x); @@ -201,12 +244,12 @@ pub fn adopt(p: *Pardes, bytes: []const u8) !usize { if (carried == .column or p.ncol == 0) { const c = if (p.ncol == 0) layout.insertColumn(p, 0, false) else layout.insertColumn(p, tc, x < p.col_x[tc] + p.col_w[tc] / 2); const col = c orelse return error.NoRoom; - if (carried == .column) if (st.columns[0].tag) |tag| { + if (carried == .column) if (st.tag) |tag| { p.col_tags[col].own = try p.gpa.dupe(u8, tag); }; for (slots[0..n], 0..) |slot, k| { layout.insert(p, col, k, slot); - p.panes[slot].?.vweight = st.panes[order[k]].vweight; + p.panes[slot].?.vweight = st.weights[k]; } p.active = slots[0]; p.setActiveColumn(col); @@ -240,10 +283,10 @@ pub fn adopt(p: *Pardes, bytes: []const u8) !usize { heights[owner] = keeps; // The carried share what was given as they shared their old rows. var total: f32 = 0; - for (order) |i| total += st.panes[i].vweight; + for (st.weights) |w| total += w; var left = give; for (slots[0..n], 0..) |slot, j| { - const share: u16 = if (j + 1 == n) left else @intFromFloat(@max(1, @floor(@as(f32, @floatFromInt(give)) * st.panes[order[j]].vweight / @max(total, 1)))); + const share: u16 = if (j + 1 == n) left else @intFromFloat(@max(1, @floor(@as(f32, @floatFromInt(give)) * st.weights[j] / @max(total, 1)))); heights[slot] = @min(share, left); left -|= share; } @@ -284,7 +327,7 @@ pub const Job = struct { pub fn dumpPath(gpa: std.mem.Allocator, seq: u32) ![:0]u8 { var dir_buf: [1024]u8 = undefined; const dir: []const u8 = if (libc.getenv("XDG_RUNTIME_DIR")) |x| std.mem.span(x) else dump.defaultDirectory(&dir_buf) orelse return error.NoDirectory; - return std.fmt.allocPrintSentinel(gpa, "{s}/pardes-handover-{d}-{d}.zon", .{ dir, libc.getpid(), seq }, 0); + return std.fmt.allocPrintSentinel(gpa, "{s}/pardes-handover-{d}-{d}.dump", .{ dir, libc.getpid(), seq }, 0); } /// Runs the handover's carrier on `io` (io.concurrent), kept in `h.task` @@ -451,12 +494,13 @@ test "a grip let go past the window's edge dumps its pane alone, and closes it o try std.testing.expect(p.drag == .none); try std.testing.expect(p.panes[0] != null); try std.testing.expectEqual(before, p.rects[other]); - var parsed = try dump.readZon(std.testing.allocator, p.handover.bytes.?, "test"); - defer parsed.deinit(); - try std.testing.expectEqual(@as(?dump.Carried, .pane), parsed.value.carried); - try std.testing.expectEqual(@as(usize, 1), parsed.value.panes.len); - try std.testing.expectEqual(@as(usize, 1), parsed.value.columns.len); - try std.testing.expectEqualStrings("/test.txt", parsed.value.panes[0].file.?.path); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const carried = try walk.load(Carried, arena.allocator(), p.handover.bytes.?[carried_magic.len..], null); + try std.testing.expectEqual(Kind.pane, carried.kind); + try std.testing.expectEqual(@as(usize, 1), carried.panes.len); + const pane = try walk.load(pardes.Pane, arena.allocator(), carried.panes[0], null); + try std.testing.expectEqualStrings("/test.txt", pane.file.?.path); // A second one waits for the first. releaseOutside(p, other); try std.testing.expect(p.panes[other] != null); @@ -543,7 +587,7 @@ test "a column let go over another window goes beside the column the pointer is from.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = from.screen_w - 1, .row = r.y } }); from.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = from.screen_w - 1, .row = r.y, .outside = true } }); try std.testing.expect(from.handover.flying); - try std.testing.expectEqual(dump.Carried.column, from.handover.carried); + try std.testing.expectEqual(Kind.column, from.handover.carried); const to = try Pardes.init(gpa, .{ .tty_only = true, .cols = 120, .rows = 30 }); defer to.deinit(); _ = try to.setTestFile("x\n"); diff --git a/src/Output.zig b/src/Output.zig index 83914d52..29e49180 100644 --- a/src/Output.zig +++ b/src/Output.zig @@ -7,7 +7,6 @@ const exec = @import("exec.zig"); const config = @import("config.zig"); const Pardes = pardes.Pardes; const modal = @import("modal.zig"); -const dump = @import("dump.zig"); const limits = @import("memory.zig").limits; const builtins = @import("builtins.zig"); const effect_sources = @import("effect_sources.zig"); @@ -40,7 +39,8 @@ pub const Origin = union(enum) { preview, }; -pub const max_arg = dump.max_origin_arg; +/// An output's argument is typed in the bounded one-line tag storage. +pub const max_arg = limits.max_tag_tail; pub const State = struct { pub const replica = .{ .len = .{.{ "arg_buf", "arg_len" }} }; @@ -320,36 +320,6 @@ pub fn grain(out: ?State) Grain { return if (tr.steps) .line else .word; } -pub fn word(o: Origin) []const u8 { - return switch (o) { - .cmd => |b| @tagName(b), - .query => |k| @tagName(k), - .search => "/", - .errors => config.errors_buffer, - .pdf_links => config.pdf_links_buffer, - .unsaved => config.unsaved_buffer, - .dir => dir_word, - .preview => config.preview_buffer, - }; -} - -/// A directory pane's origin as a dump names it. -const dir_word = "dir"; - -pub fn fromWord(w: []const u8) ?Origin { - if (w.len == 0) return null; - if (std.mem.eql(u8, w, "/")) return .search; - if (std.mem.eql(u8, w, config.errors_buffer)) return .errors; - if (std.mem.eql(u8, w, config.pdf_links_buffer)) return .pdf_links; - if (std.mem.eql(u8, w, config.unsaved_buffer)) return .unsaved; - if (std.mem.eql(u8, w, dir_word)) return .dir; - if (std.mem.eql(u8, w, config.preview_buffer)) return .preview; - if (std.meta.stringToEnum(Builtin, w)) |b| - if (builtins.registry.outputTraits(b) != null) return .{ .cmd = b }; - if (std.meta.stringToEnum(lsp.Kind, w)) |k| return .{ .query = k }; - return null; -} - pub fn resultsFrom(p: *Pardes, pane: *Pane, from: Origin) bool { const rp = p.panes[pane.search_pane orelse return false] orelse return false; const f = rp.file orelse return false; diff --git a/src/Pipe.zig b/src/Pipe.zig index 239a94e4..903c1dba 100644 --- a/src/Pipe.zig +++ b/src/Pipe.zig @@ -15,6 +15,10 @@ const Key = pardes.Key; const Pardes = pardes.Pardes; const Pipe = @This(); +/// A pipe's run, an Edit's and its writes, out with the host: errands of +/// the session that performs them, not a dump's (src/walk.zig). +pub const replica = .{ .authority = .{ "wait", "edit_run", "writes" } }; + /// Bumped for every request, so a late answer to an older one is ignored. seq: u32 = 0, /// The request in flight, and what it needs to apply its answer. diff --git a/src/Query.zig b/src/Query.zig index 7440dd0e..8296aa80 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -2284,7 +2284,7 @@ test "a / query: keystroke by keystroke, the owner shows the set, rows from its try std.testing.expectEqual(@as(i32, 2), pane.body.cur_row); } -test "a dump holding a query pane, live or frozen, restores, the pane as plain text" { +test "a dump holding a query pane, live or frozen, restores it as it was" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }); defer p.deinit(); _ = try p.setTestFile("alpha one\nbeta\nalphabet\n"); @@ -2295,9 +2295,10 @@ test "a dump holding a query pane, live or frozen, restores, the pane as plain t const restored = try pardes.dump.initFromDump(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }, p.dump_out.?); defer restored.deinit(); const back = restored.panes[qid].?; - try std.testing.expect(back.query == null); - try std.testing.expectEqual(@as(usize, 0), back.file.?.location_rows.len); - try std.testing.expect(std.mem.startsWith(u8, back.file.?.content, "/alpha\n")); + const was = p.panes[qid].?; + try std.testing.expectEqual(was.query.?.frozen, back.query.?.frozen); + try std.testing.expectEqual(was.file.?.location_rows.len, back.file.?.location_rows.len); + try std.testing.expectEqualStrings(was.file.?.content, back.file.?.content); p.update(.{ .key = .{ .cp = Key.enter } }); } } diff --git a/src/Store.zig b/src/Store.zig index 856a7fc1..95f5368f 100644 --- a/src/Store.zig +++ b/src/Store.zig @@ -498,31 +498,30 @@ pub fn keep(s: *Store, p: *Pardes, id: usize) ?Kept { const pane = p.panes[id] orelse return null; if (pane.fs.unannounced or pane.query != null) return null; if (pane.file) |f| if (f.output) |o| if (o.from == .preview) return null; - // An output pane's record holds its text whole (base64): one past the - // bound is known before a dump of it is made, which for a 256 MiB - // +Pager held the session up for seconds only to be thrown away. + // An output pane's record holds its text whole: one past the bound is + // known before a dump of it is made, which for a 256 MiB +Pager held + // the session up for seconds only to be thrown away. if (pane.file) |f| if (f.output != null and f.content.len > max_record) return null; const gpa = p.recentGpa(); - const arena = p.scratch.allocator(); - var dp = dump.dumpPane(p, arena, pane) catch return null; - var zon = oneDump(p, arena, dp) catch return null; - // A terminal's history is what makes it big: kept without it, it comes - // back a fresh shell in its directory. - if (zon.len > max_record) if (dp.terminal) |*t| { - t.stream = ""; - t.stream_b64 = ""; - dp.body = ""; - zon = oneDump(p, arena, dp) catch return null; - }; - if (zon.len > max_record) return null; + var owned = dump.savePane(gpa, pane, false) catch return null; + // A terminal's history or a file's undo is what makes it big: kept + // without them it comes back a fresh shell in its directory, or its + // file as it is on disk. + if (owned.len > max_record) { + gpa.free(owned); + owned = dump.savePane(gpa, pane, true) catch return null; + } + if (owned.len > max_record) { + gpa.free(owned); + return null; + } const what: []const u8 = if (pane.file) |f| f.path else if (pane.pdfPath()) |pdf| pdf else if (pane.image) |iv| iv.path else if (pane.command) |c| c else pane.cwdSlice(); - var name = gpa.alloc(u8, @min(what.len, 4096)) catch return null; - // One line, a tab its end: a newline or a tab in it is a space. - for (what[0..name.len], 0..) |c, i| name[i] = if (c == '\n' or c == '\t') ' ' else c; - const owned = gpa.dupe(u8, zon) catch { - gpa.free(name); + var name = gpa.alloc(u8, @min(what.len, 4096)) catch { + gpa.free(owned); return null; }; + // One line, a tab its end: a newline or a tab in it is a space. + for (what[0..name.len], 0..) |c, i| name[i] = if (c == '\n' or c == '\t') ' ' else c; const at = layout.findPane(p, id); const owner = layout.ownerOf(p, id); return .{ @@ -552,18 +551,6 @@ pub const Kept = struct { } }; -/// A Dump of the one pane `dp`, as a Restore would read it. -fn oneDump(p: *Pardes, arena: std.mem.Allocator, dp: dump.Pane) ![]const u8 { - const ids = [_]usize{0}; - const columns = [_]dump.Column{.{ .panes = &ids }}; - const one = [_]dump.Pane{dp}; - const state: dump.State = .{ .screen = .{ .cols = p.screen_w, .rows = p.screen_h }, .columns = &columns, .panes = &one }; - try dump.validate(state); - var out: std.Io.Writer.Allocating = .init(arena); - try std.zon.stringify.serialize(state, .{}, &out.writer); - return out.written(); -} - /// The pane `k` kept is gone, its rows given to `gainer` (a slot, after /// they moved; null none): listed, and its record framed for the store. pub fn close(s: *Store, p: *Pardes, k: Kept, gainer: ?usize) void { @@ -805,18 +792,14 @@ pub fn reopen(s: *Store, p: *Pardes, serial: u32, from: usize) bool { // Read from the file: this call's to free (`unlist` frees one held). defer if (place.body.len == 0) gpa.free(body); const m = parseMeta(body) orelse return false; - var parsed = dump.readZon(p.gpa, body[m.dump_at..], "closed pane") catch return false; - defer parsed.deinit(); const free = p.freeSlot() orelse { p.reportError(from, "Panes", error.NoPaneSlots); return false; }; - const src = parsed.value.panes[0]; - const pane = dump.restorePane(p, free, src) catch |err| { + _ = dump.loadPane(p, free, body[m.dump_at..]) catch |err| { p.reportError(from, "Panes", err); return false; }; - pane.collapsed = false; const owner = if (place.owner != 0) p.paneBySerial(place.owner) else null; const gainer = if (place.gainer != 0) p.paneBySerial(place.gainer) else null; const placed = if (owner) |o| diff --git a/src/builtins.zig b/src/builtins.zig index a89b1180..0ad59fd4 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -725,7 +725,7 @@ pub const Restore = struct { var why_buf: [256]u8 = undefined; pardes.dump.check(c.p.gpa, bytes, &why_buf) catch |err| { var buf: [limits.host_path_cap + 320]u8 = undefined; - const why = if (err == error.ParseZon) why_buf[0..std.mem.indexOfScalar(u8, &why_buf, 0).?] else pardes.Messages.errorWords(err, &why_buf); + const why = if (err == error.OutOfMemory) pardes.Messages.errorWords(err, &why_buf) else why_buf[0..std.mem.indexOfScalar(u8, &why_buf, 0).?]; return c.p.reportFailure(c.id, std.fmt.bufPrint(&buf, "Restore: {s}: not a pardes dump: {s}", .{ path, why }) catch "Restore: not a pardes dump"); }; } diff --git a/src/dump.zig b/src/dump.zig index f9f01950..ccbcd158 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -7,14 +7,13 @@ const limits = @import("memory.zig").limits; // diagnostic is the only thing that says WHICH field went bad. const log = std.log.scoped(.dump); const pardes = @import("pardes.zig"); -const tagline = @import("tagline.zig"); -const layout = @import("layout.zig"); +const walk = @import("walk.zig"); +const Handover = @import("Handover.zig"); const filesystem = @import("fs.zig"); +const layout = @import("layout.zig"); const ctlfs = @import("ninep/tree.zig"); const exec = @import("exec.zig"); -const body_layer = @import("body_layer.zig"); const colors = @import("colors.zig"); -const themes = colors.themes; const config = @import("config.zig"); const pdf_enabled = pardes.pdf_enabled; const platform = pardes.platform; @@ -22,15 +21,13 @@ const isolated = pardes.isolated; const terminal_panes = pardes.terminal_panes; const MAX_PANES = pardes.MAX_PANES; const MAX_COLS = pardes.MAX_COLS; -const column_weight_unit = layout.column_weight_unit; -const max_column_weight = layout.max_column_weight; const effect_path_cap = pardes.effect_path_cap; const Options = pardes.Options; const Pardes = pardes.Pardes; /// Where the next dump goes. $PARDES_DUMP wins verbatim (the snapshot harness /// pins it for deterministic goldens); else $XDG_DATA_HOME|~/.local/share -/// /pardes/pardes-.zon — timestamped so dumps never overwrite each other. +/// /pardes/pardes-.dump — timestamped so dumps never overwrite each other. /// Creates the directory and any parent missing (a fresh HOME has no /// ~/.local/share); a failure surfaces at open. pub fn outPath(buf: *[1024:0]u8, dir_setting: []const u8) ?[:0]const u8 { @@ -53,15 +50,15 @@ pub fn outPath(buf: *[1024:0]u8, dir_setting: []const u8) ?[:0]const u8 { return uniqueIn(buf, dir, base); } -/// `/.zon`, or `-2.zon` and on when two dumps land in one +/// `/.dump`, or `-2.dump` and on when two dumps land in one /// second: a dump never overwrites another. fn uniqueIn(buf: *[1024:0]u8, dir: []const u8, base: []const u8) ?[:0]const u8 { var n: usize = 1; while (n < 1000) : (n += 1) { const path = (if (n == 1) - std.fmt.bufPrintSentinel(buf, "{s}/{s}.zon", .{ dir, base }, 0) + std.fmt.bufPrintSentinel(buf, "{s}/{s}.dump", .{ dir, base }, 0) else - std.fmt.bufPrintSentinel(buf, "{s}/{s}-{d}.zon", .{ dir, base, n }, 0)) catch return null; + std.fmt.bufPrintSentinel(buf, "{s}/{s}-{d}.dump", .{ dir, base, n }, 0)) catch return null; if (comptime !pardes.hosted) return path; if (std.c.access(path.ptr, std.c.F_OK) != 0) return path; } @@ -76,9 +73,9 @@ test "two dumps in one second get names of their own" { var buf: [1024:0]u8 = undefined; const first = try std.testing.allocator.dupe(u8, uniqueIn(&buf, dir, "pardes-x").?); defer std.testing.allocator.free(first); - try std.testing.expect(std.mem.endsWith(u8, first, "/pardes-x.zon")); - try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pardes-x.zon", .data = "" }); - try std.testing.expect(std.mem.endsWith(u8, uniqueIn(&buf, dir, "pardes-x").?, "/pardes-x-2.zon")); + try std.testing.expect(std.mem.endsWith(u8, first, "/pardes-x.dump")); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pardes-x.dump", .data = "" }); + try std.testing.expect(std.mem.endsWith(u8, uniqueIn(&buf, dir, "pardes-x").?, "/pardes-x-2.dump")); } /// The host's environment; a freestanding core (web, the board) has none /// and no libc to ask, so only a DumpDir with no `~/` names a directory there. @@ -136,777 +133,168 @@ pub fn defaultDirectory(buf: []u8) ?[]const u8 { return null; } -pub const magic = "pardes-dump"; -pub const version: u32 = 1; -pub const max_panes: usize = MAX_PANES; -pub const max_cols: usize = MAX_COLS; -/// Output arguments are typed in the same bounded one-line tag storage. Keep -/// the schema limit named independently so a dump reader can validate it -/// without importing the output-pane implementation. The bound itself is -/// `limits.max_tag_tail` — the one place a board-shaped capacity is chosen. -pub const max_origin_arg: usize = limits.max_tag_tail; - -pub const Size = struct { - cols: u16, - rows: u16, -}; - -pub const Cursor = struct { - col: u16 = 0, - row: u16 = 0, -}; - -pub const PaneKind = enum { - terminal, - file, - image, -}; - -pub const Terminal = struct { - cwd: []const u8 = "", - /// The shell it runs when not the configured one (`Tty bash`); empty, - /// the configured one, as in dumps from before. - shell: []const u8 = "", - /// A command pane's line and how it ended (null: unknown, or still - /// running when dumped); empty for an interactive terminal. - command: []const u8 = "", - status: ?u8 = null, - /// The rendered scrollback and screen as plain text, one row per line. - stream: []const u8 = "", - stream_b64: []const u8 = "", - /// `stream_b64` is the emulator's primary screen and scrollback as clean - /// VT (ghostty's formatter), which replays to what the screen showed. - /// Absent (older dumps), it is the raw tail of the pty's output, which - /// replays faithfully only if it holds the whole session at one size. - stream_is_screen: bool = false, - cursor: Cursor = .{}, - /// Petscii on: its kitty graphics drawn as glyph art by choice. - petscii: bool = false, -}; - -pub const LocationRow = struct { - mark_occurrence: bool = true, - source_column: usize = 0, - kind: enum { match, context, preview }, - declaration: bool = false, - path: []const u8, - line: usize, - col: usize = 0, - end_line: usize = 0, - end_col: usize = 0, - location_end: usize, - code_start: usize, - colors_b64: []const u8 = "", -}; - -pub const File = struct { - path: []const u8 = "", - /// The text, in older dumps plain and base64 both; now base64 alone, - /// and only for a pane whose text is not its file's on disk (unsaved, - /// an output, a file never written): a clean file's is read from disk. - content: []const u8 = "", - content_b64: []const u8 = "", - dirty: bool = false, - /// An unsaved pane's: the hash of the file on disk its text was read - /// against. A Restore that finds another there says it changed, and - /// Save asks before overwriting it. - disk_hash: ?u64 = null, - /// Its file was on disk when dumped: one gone by the Restore is said - /// deleted, one never written (Config's new file) is not. True in - /// older dumps. - on_disk: bool = true, - tree_context: bool = false, - location_rows: []const LocationRow = &.{}, - // Builtin names remain stable when enum ordinals change. - origin: []const u8 = "", - origin_arg: []const u8 = "", - mini_source: []const u8 = "", - mini_colors_b64: []const u8 = "", - /// Its dot, q0 and q1 in bytes, as acme's dump keeps a window's; null - /// in older dumps. - dot: ?[2]u32 = null, - /// A Zerox twin: the index, in `panes`, of the earlier pane whose - /// buffer it shares, its text kept there once; null for none. - twin_of: ?u32 = null, -}; - -pub const ImagePalette = enum { - commodore, - terminal, -}; - -pub const Image = struct { - path: []const u8 = "", - bytes_b64: []const u8 = "", - // Defaults are the renderer's historical state, so version-1 dumps which - // predate these fields remain readable without a schema fork. - petscii: bool = false, - palette: ImagePalette = .commodore, - ascii: bool = true, - /// A PDF's PdfFit and PdfTint, by their tag words (`width`, `filtered`); - /// empty, the defaults, as in dumps from before. - pdf_fit: []const u8 = "", - pdf_tint: []const u8 = "", -}; - -pub const Pane = struct { - kind: PaneKind, - tag: []const u8, - body: []const u8, - scroll: usize = 0, - cols: u16 = 0, - rows: u16 = 0, - vweight: f32 = 1, - collapsed: bool = false, - /// Its serial when dumped (0 in older dumps): a Restore logs - /// `restored ` for it. - serial: u32 = 0, - /// Exact editable tail when the user touched it. Null retains the live - /// default; a present empty slice deliberately restores an empty tail. - tag_tail: ?[]const u8 = null, - /// The index in `panes` of the pane this one is attached to (layout.zig, - /// "Attached panes"); null when it is attached to none, and in older dumps. - attached_to: ?usize = null, - terminal: ?Terminal = null, - file: ?File = null, - image: ?Image = null, -}; - -pub const Column = struct { - /// Its serial when dumped (0 in older dumps): a Restore logs - /// `restoredcol ` for it. - serial: u32 = 0, - tag: ?[]const u8 = null, - weight: f32 = 1, - panes: []const usize = &.{}, -}; - -pub const Mount = struct { name: []const u8, dial: []const u8 }; - -/// What a grip carried out of its window (Handover.zig): one pane with the -/// panes attached to it, or a whole column. A session's own dump is none. -pub const Carried = enum { pane, column }; - -pub const State = struct { - magic: []const u8 = magic, - version: u32 = version, - screen: Size, - active: usize = 0, - topbar: []const u8 = "", - topbar_custom: ?[]const u8 = null, - theme: []const u8 = "dark", - locations_config: @import("locations.zig").Config = .{}, - tree_context_tag_style: bool = true, - columns: []const Column = &.{}, - panes: []const Pane = &.{}, - mounts: []const Mount = &.{}, - /// The settings that differ from a fresh session's, each a line as the - /// root ctl takes it (`Placement pardes`); the theme and the locations - /// config are kept on their own, and the font is the frontend's. Absent - /// in older dumps. - settings: []const []const u8 = &.{}, - /// Incl's directories, in order; null in older dumps (the defaults). - incl: ?[]const []const u8 = null, - /// A dump a grip carried to another window; null for a session's own. - carried: ?Carried = null, -}; - -/// A header is a text like any other: it may hold newlines, but not a -/// carriage return or a NUL. -fn validateTag(bar: []const u8) !void { - if (bar.len > limits.max_tag_tail or std.mem.indexOfAny(u8, bar, "\r\x00") != null or !std.unicode.utf8ValidateSlice(bar)) return error.BadDumpTag; +// ---- a whole session to and from a dump (acme keeps this in rows.c) ---- +// +// A dump (src/walk.zig) is everything the session is but what is the +// host's, the frame's or worked out again: its panes with their text and +// undo, terminals with their screens, history and kitty images (ghostty's +// snapshot), registers, places, tags, layout and settings. It crosses +// builds: its fields are matched by name. +// +// Loading is two halves. `load` makes the core the dump was and nothing +// else (a replica joining a session takes that, phase 5); `revivePane` and +// `reviveSession` give it to this host: new shells, watches, PDFs opened. + +/// A session's dump: its core, everything it is but what is the host's, +/// the frame's or worked out again (`replica` in each type, src/walk.zig), +/// then its 9P mounts, which live with the 9P server's own state, and which +/// of its panes' files were on disk when it was made (one gone by its load +/// is said deleted: its text is nowhere else now). +const Session = walk.Dump(&.{ Pardes, []const filesystem.Mount, OnDisk }); +const SessionLoad = walk.Load(&.{ Pardes, []const filesystem.Mount, OnDisk }); +const OnDisk = [MAX_PANES]bool; + +fn onDisk(pane: *const pardes.panes.Pane) bool { + if (comptime !pardes.hosted) return false; + const f = pane.file orelse return false; + if (f.output != null) return false; + const local = filesystem.localPath(f.path) orelse return false; + return exec.kindOf(local) != null; } -pub fn validate(state: State) !void { - if (state.topbar_custom) |bar| try validateTag(bar); - for (state.columns) |column| if (column.tag) |bar| try validateTag(bar); - if (!std.mem.eql(u8, state.magic, magic)) return error.BadDumpMagic; - if (state.version != version) return error.BadDumpVersion; - if (state.panes.len == 0 or state.panes.len > max_panes) return error.BadDumpPanes; - if (state.columns.len == 0 or state.columns.len > max_cols) return error.BadDumpColumns; - if (state.active >= state.panes.len) return error.BadDumpActive; - if (state.mounts.len > 8) return error.BadDumpMounts; - for (state.mounts, 0..) |mount, i| { - if (mount.name.len == 0 or mount.name.len > 255 or mount.dial.len == 0 or - std.mem.indexOfScalar(u8, mount.dial, 0) != null) return error.BadDumpMounts; - if (std.mem.eql(u8, mount.name, ".") or std.mem.eql(u8, mount.name, "..") or - std.mem.eql(u8, mount.name, "os") or std.mem.eql(u8, mount.name, "self")) return error.BadDumpMounts; - for (mount.name) |c| if (!std.ascii.isAlphanumeric(c) and c != '_' and c != '-' and c != '.') return error.BadDumpMounts; - for (state.mounts[0..i]) |previous| if (std.mem.eql(u8, mount.name, previous.name)) return error.BadDumpMounts; - } - for (state.columns) |col| { - if (!std.math.isFinite(col.weight) or col.weight <= 0) return error.BadDumpColumns; - // An empty column is a column, as in acme's dump (rows.c:639). - if (col.panes.len > max_panes) return error.BadDumpColumns; - for (col.panes) |pane| { - if (pane >= state.panes.len) return error.BadDumpPaneRef; - } - } - for (state.panes) |pane| { - if (!std.math.isFinite(pane.vweight) or pane.vweight <= 0) - return error.BadDumpPaneWeight; - if (pane.scroll > std.math.maxInt(i32)) return error.BadDumpPaneScroll; - if (pane.tag_tail) |tail| if (tail.len > limits.max_tag_tail) - return error.BadDumpTagTail; - if (pane.file) |file| if (file.origin_arg.len > max_origin_arg) - return error.BadDumpOriginArg; - if (pane.file) |file| { - if (file.mini_source.len > 4096 or std.mem.indexOfScalar(u8, file.mini_source, 0) != null or - (file.mini_source.len == 0 and file.mini_colors_b64.len != 0) or - (file.mini_source.len != 0 and !std.mem.eql(u8, file.origin, "Mini"))) return error.BadDumpMini; - } - switch (pane.kind) { - .terminal => if (pane.terminal == null) return error.BadDumpPaneKind, - .file => if (pane.file == null) return error.BadDumpPaneKind, - .image => if (pane.image == null) return error.BadDumpPaneKind, - } - } +pub fn dumpState(p: *Pardes) !void { + const bytes = try dumpBytes(p); + if (p.dump_out) |d| p.gpa.free(d); + p.dump_out = bytes; + p.emit(.write_dump); } -test "oversized recognized output argument is rejected before restore" { - var oversized: [max_origin_arg + 1]u8 = @splat('x'); - const pane_ids = [_]usize{0}; - const columns = [_]Column{.{ .panes = &pane_ids }}; - const panes = [_]Pane{.{ - .kind = .file, - .tag = "+Search", - .body = "", - .file = .{ - .path = "/tmp/+Search", - .origin = "Find", - .origin_arg = &oversized, - }, - }}; - const state: State = .{ - .screen = .{ .cols = 80, .rows = 24 }, - .columns = &columns, - .panes = &panes, +/// The session as a dump, gpa-owned. +pub fn dumpBytes(p: *Pardes) ![]u8 { + var d = try Session.init(p.gpa); + errdefer d.deinit(); + try d.put(Pardes, p); + const mounts: []const filesystem.Mount = p.fs.mounts.items; + try d.put([]const filesystem.Mount, &mounts); + var on_disk: OnDisk = @splat(false); + for (p.panes, &on_disk) |slot, *on| if (slot) |pane| { + on.* = onDisk(pane); }; - - // Serialize without writeFile: that writer correctly rejects the state - // too, while this exercises the hostile bytes a restore actually reads. - var encoded: std.Io.Writer.Allocating = .init(std.testing.allocator); - defer encoded.deinit(); - try std.zon.stringify.serialize(state, .{}, &encoded.writer); - try std.testing.expectError( - error.BadDumpOriginArg, - readZon(std.testing.allocator, encoded.written(), "oversized-origin-arg"), - ); + try d.put(OnDisk, &on_disk); + return d.finish(); } -pub const Parsed = struct { - value: State, - arena: std.heap.ArenaAllocator, - - pub fn deinit(parsed: *Parsed) void { - parsed.arena.deinit(); - } -}; - -/// Whether `bytes` are a dump a Restore could load, saying nothing: a +/// Whether `bytes` are a dump a Restore could read, saying nothing: a /// Restore asks before it warns about unsaved text or replaces anything. -/// Whether `bytes` are a dump; a ZON failure's line and reason are left in -/// `why`, ended by a 0. +/// Why not is left in `why`, ended by a 0. pub fn check(gpa: std.mem.Allocator, bytes: []const u8, why: []u8) !void { - var arena: std.heap.ArenaAllocator = .init(gpa); - defer arena.deinit(); - const source = try arena.allocator().dupeSentinel(u8, bytes, 0); - var diag: std.zon.parse.Diagnostics = undefined; - const state = std.zon.parse.fromSlice(State, .{ .gpa = gpa, .arena = arena.allocator(), .source = source, .diagnostics = &diag }) catch |err| { - const said = pardes.Messages.zonWhy(&diag, why[0 .. why.len - 1]); - if (said.ptr != why.ptr) @memcpy(why[0..said.len], said); + var l = SessionLoad.init(gpa, bytes) catch |err| { + const said = std.fmt.bufPrint(why[0 .. why.len - 1], "{s}", .{switch (err) { + error.BadDump => "it does not begin as one", + error.EndOfDump => "it is cut short", + else => @errorName(err), + }}) catch ""; why[said.len] = 0; return err; }; - try validate(state); -} - -pub fn readZon(gpa: std.mem.Allocator, bytes: []const u8, label: []const u8) !Parsed { - var parsed: Parsed = .{ .value = undefined, .arena = .init(gpa) }; - errdefer parsed.deinit(); - const arena = parsed.arena.allocator(); - const source = try gpa.dupeSentinel(u8, bytes, 0); - defer gpa.free(source); - - var diag: std.zon.parse.Diagnostics = undefined; - parsed.value = std.zon.parse.fromSlice(State, .{ .gpa = gpa, .arena = arena, .source = source, .diagnostics = &diag }) catch |err| { - if (err == error.ParseZon) log.err("{f}", .{diag.fmt(label)}); - return err; - }; - try validate(parsed.value); - return parsed; + l.deinit(); } -pub fn encodeBytes(alloc: std.mem.Allocator, bytes: []const u8) ![]const u8 { - const enc = std.base64.standard.Encoder; - const out = try alloc.alloc(u8, enc.calcSize(bytes.len)); - return enc.encode(out, bytes); +/// The core the dump `bytes` is, and nothing more: no shell started, no +/// file watched, nothing asked of a host (`revive` does that). What another +/// build's dump could not give is said in `notes` when given. `opts` are +/// this process's (its allocators, paths and name), not the dump's. +pub fn load(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, notes: ?*std.ArrayList(u8)) !*Pardes { + return loadKnowing(gpa, opts, bytes, notes, null); } -pub fn decodeBytes(alloc: std.mem.Allocator, b64: []const u8) ![]u8 { - const dec = std.base64.standard.Decoder; - const n = try dec.calcSizeForSlice(b64); - const out = try alloc.alloc(u8, n); - errdefer alloc.free(out); - try dec.decode(out, b64); - return out; -} - -test "dump zon roundtrip" { - const gpa = std.testing.allocator; - const tty_b64 = try encodeBytes(gpa, "old\nhello\nworld"); - defer gpa.free(tty_b64); - const file_b64 = try encodeBytes(gpa, "alpha\nbeta\n"); - defer gpa.free(file_b64); - const panes = [_]Pane{ - .{ - .kind = .terminal, - .tag = "nm /tmp Del", - .body = "hello\nworld", - .scroll = 1, - .cols = 40, - .rows = 10, - .terminal = .{ .cwd = "/tmp", .stream = "old\nhello\nworld", .stream_b64 = tty_b64, .cursor = .{ .col = 3, .row = 2 } }, - }, - .{ - .kind = .file, - .tag = "nm /tmp/a.txt Save Del", - .body = " 1 alpha", - .cols = 20, - .rows = 5, - .file = .{ .path = "/tmp/a.txt", .content = "alpha\nbeta\n", .content_b64 = file_b64, .dirty = true }, - }, - }; - const col_panes = [_]usize{ 0, 1 }; - const columns = [_]Column{.{ .weight = 1, .panes = &col_panes }}; - const state: State = .{ - .screen = .{ .cols = 80, .rows = 24 }, - .topbar = "Kill Newcol Dump", - .theme = "dark", - .columns = &columns, - .panes = &panes, +/// `load`, with which panes' files were on disk when it was dumped. +fn loadKnowing(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, notes: ?*std.ArrayList(u8), on_disk: ?*OnDisk) !*Pardes { + const p = try Pardes.create(gpa, opts); + var l = SessionLoad.init(gpa, bytes) catch |err| { + p.deinit(); + return err; }; - - var out: std.Io.Writer.Allocating = .init(gpa); - defer out.deinit(); - try std.zon.stringify.serialize(state, .{ .whitespace = true }, &out.writer); - var result = try readZon(gpa, out.written(), "roundtrip"); - defer result.deinit(); - const parsed = result.value; - try std.testing.expectEqual(@as(usize, 2), parsed.panes.len); - try std.testing.expectEqualStrings("dark", parsed.theme); - try std.testing.expectEqualStrings("old\nhello\nworld", parsed.panes[0].terminal.?.stream); - try std.testing.expectEqualStrings("alpha\nbeta\n", parsed.panes[1].file.?.content); - try std.testing.expect(parsed.panes[1].file.?.dirty); - { - const bytes = try decodeBytes(gpa, parsed.panes[0].terminal.?.stream_b64); - defer gpa.free(bytes); - try std.testing.expectEqualStrings("old\nhello\nworld", bytes); + defer l.deinit(); + // A load that fails leaves nothing: what it read is freed, and the + // core with it, its fields never torn down one by one. + errdefer { + l.rollback(); + p.fs.deinit(gpa); + gpa.destroy(p); } - { - const bytes = try decodeBytes(gpa, parsed.panes[1].file.?.content_b64); - defer gpa.free(bytes); - try std.testing.expectEqualStrings("alpha\nbeta\n", bytes); + l.allocs = .{ .image = p.image_gpa, .pdf = p.pdf_gpa }; + try l.into(Pardes, p); + const read = l.shared.items.len; + const mounts = try l.get([]const filesystem.Mount); + defer { + for (mounts) |m| { + gpa.free(m.name); + gpa.free(m.dial); + } + gpa.free(mounts); + // Freed here: no rollback of them. + l.shared.shrinkRetainingCapacity(read); } -} - -test "omitted dump defaults roundtrip without borrowing input" { - const gpa = std.testing.allocator; - const fixture = - \\.{ - \\ .screen = .{ .cols = 80, .rows = 24 }, - \\ .columns = .{.{ .panes = .{0, 1, 2} }}, - \\ .panes = .{ - \\ .{ .kind = .terminal, .tag = "terminal", .body = "", .terminal = .{} }, - \\ .{ .kind = .file, .tag = "file", .body = "", .file = .{ .path = "file.zig", .content = "const café = 1;" } }, - \\ .{ .kind = .image, .tag = "image", .body = "", .image = .{} }, - \\ }, - \\} - ; - const input = try gpa.dupe(u8, fixture); - defer gpa.free(input); - var parsed = try readZon(gpa, input, "omitted-defaults"); - defer parsed.deinit(); - @memset(input, 'x'); - const value = parsed.value; - try std.testing.expectEqualStrings(magic, value.magic); - try std.testing.expectEqualStrings("dark", value.theme); - try std.testing.expectEqualStrings("", value.topbar); - try std.testing.expectEqual(@as(usize, 0), value.mounts.len); - try std.testing.expectEqual(@as(usize, 3), value.panes.len); - try std.testing.expectEqualStrings("file.zig", value.panes[1].file.?.path); - try std.testing.expectEqualStrings("const café = 1;", value.panes[1].file.?.content); - try std.testing.expect(!value.panes[1].file.?.dirty); - try std.testing.expect(value.panes[2].image.?.ascii); - - var encoded: std.Io.Writer.Allocating = .init(gpa); - defer encoded.deinit(); - try std.zon.stringify.serialize(value, .{}, &encoded.writer); - var again = try readZon(gpa, encoded.written(), "default-roundtrip"); - defer again.deinit(); - try std.testing.expectEqualDeep(value, again.value); -} - -test "dump parser releases its arena at every allocation failure" { - const Check = struct { - fn run(gpa: std.mem.Allocator) !void { - var parsed = try readZon(gpa, - \\.{ - \\ .screen = .{ .cols = 80, .rows = 24 }, - \\ .columns = .{.{ .panes = .{0} }}, - \\ .panes = .{.{ .kind = .file, .tag = "file", .body = "", .file = .{ .content = "owned" } }}, - \\} - , "allocation-cleanup"); - defer parsed.deinit(); - try std.testing.expectEqualStrings("owned", parsed.value.panes[0].file.?.content); + const was = try l.get(OnDisk); + if (on_disk) |out| out.* = was; + // A mount this process was started with stands; one of the same name + // to elsewhere is refused, never silently put over it. + for (mounts) |m| { + const existing = for (p.fs.mounts.items) |e| { + if (std.mem.eql(u8, e.name, m.name)) break e; + } else null; + if (existing) |e| { + if (!std.mem.eql(u8, e.dial, m.dial)) return error.MountConflict; + continue; } - }; - try std.testing.checkAllAllocationFailures(std.testing.allocator, Check.run, .{}); -} - -test "dump mount validation bounds names ownership inputs and duplicates" { - const panes = [_]Pane{.{ .kind = .file, .tag = "", .body = "", .file = .{} }}; - const ids = [_]usize{0}; - const columns = [_]Column{.{ .panes = &ids }}; - var state: State = .{ - .screen = .{ .cols = 80, .rows = 24 }, - .columns = &columns, - .panes = &panes, - }; - try validate(state); - for ([_]Mount{ - .{ .name = "peer", .dial = "/tmp/peer.sock" }, - .{ .name = "build-1.local", .dial = "tcp!127.0.0.1!5640" }, - }) |mount| { - state.mounts = &.{mount}; - try validate(state); + p.fs.mount(gpa, m.name, m.dial) catch |err| l.note("mount {s}: {t}", .{ m.name, err }); } - for ([_]Mount{ - .{ .name = "", .dial = "/tmp/peer.sock" }, - .{ .name = ".", .dial = "/tmp/peer.sock" }, - .{ .name = "..", .dial = "/tmp/peer.sock" }, - .{ .name = "os", .dial = "/tmp/peer.sock" }, - .{ .name = "self", .dial = "/tmp/peer.sock" }, - .{ .name = "two/parts", .dial = "/tmp/peer.sock" }, - .{ .name = "two parts", .dial = "/tmp/peer.sock" }, - .{ .name = "peer", .dial = "" }, - .{ .name = "peer", .dial = "unix!/tmp/peer\x00.sock" }, - }) |mount| { - state.mounts = &.{mount}; - try std.testing.expectError(error.BadDumpMounts, validate(state)); + if (l.notes.items.len > 0) { + // A pane another build could not give is left out: so is its + // place in the layout. + dropMissing(p); + if (notes) |n| try n.appendSlice(gpa, l.notes.items); } - const duplicate = Mount{ .name = "peer", .dial = "/tmp/peer.sock" }; - state.mounts = &.{ duplicate, duplicate }; - try std.testing.expectError(error.BadDumpMounts, validate(state)); - var name: [256]u8 = @splat('x'); - state.mounts = &.{.{ .name = name[0..255], .dial = "/tmp/peer.sock" }}; - try validate(state); - state.mounts = &.{.{ .name = &name, .dial = "/tmp/peer.sock" }}; - try std.testing.expectError(error.BadDumpMounts, validate(state)); - const mounts = [_]Mount{ - .{ .name = "a", .dial = "/tmp/a" }, - .{ .name = "b", .dial = "/tmp/b" }, - .{ .name = "c", .dial = "/tmp/c" }, - .{ .name = "d", .dial = "/tmp/d" }, - .{ .name = "e", .dial = "/tmp/e" }, - .{ .name = "f", .dial = "/tmp/f" }, - .{ .name = "g", .dial = "/tmp/g" }, - .{ .name = "h", .dial = "/tmp/h" }, - .{ .name = "i", .dial = "/tmp/i" }, - }; - state.mounts = mounts[0..8]; - try validate(state); - state.mounts = &mounts; - try std.testing.expectError(error.BadDumpMounts, validate(state)); -} - -test "version-one image records default old fields and roundtrip new state" { - const gpa = std.testing.allocator; - const legacy = - \\.{ - \\ .magic = "pardes-dump", - \\ .version = 1, - \\ .screen = .{ .cols = 80, .rows = 24 }, - \\ .active = 0, - \\ .topbar = "", - \\ .theme = "dark", - \\ .columns = .{.{ .panes = .{0} }}, - \\ .panes = .{.{ - \\ .kind = .image, - \\ .tag = "img /tmp/old.ppm New Del", - \\ .body = "", - \\ .image = .{ .path = "/tmp/old.ppm", .bytes_b64 = "" }, - \\ }}, - \\} - ; - var old = try readZon(gpa, legacy, "legacy-image"); - defer old.deinit(); - const old_image = old.value.panes[0].image.?; - try std.testing.expect(!old_image.petscii); - try std.testing.expectEqual(ImagePalette.commodore, old_image.palette); - try std.testing.expect(old_image.ascii); - try std.testing.expect(old.value.panes[0].tag_tail == null); - - const pane = Pane{ - .kind = .image, - .tag = "img petscii:on palette:terminal ascii:off /tmp/new.ppm", - .body = "", - .tag_tail = "", - .image = .{ - .path = "/tmp/new.ppm", - .petscii = true, - .palette = .terminal, - .ascii = false, - }, - }; - const ids = [_]usize{0}; - const columns = [_]Column{.{ .panes = &ids }}; - const panes = [_]Pane{pane}; - const state = State{ - .screen = .{ .cols = 80, .rows = 24 }, - .columns = &columns, - .panes = &panes, - }; - var out: std.Io.Writer.Allocating = .init(gpa); - defer out.deinit(); - try std.zon.stringify.serialize(state, .{ .whitespace = true }, &out.writer); - var parsed = try readZon(gpa, out.written(), "new-image"); - defer parsed.deinit(); - const restored = parsed.value.panes[0].image.?; - try std.testing.expect(restored.petscii); - try std.testing.expectEqual(ImagePalette.terminal, restored.palette); - try std.testing.expect(!restored.ascii); - try std.testing.expect(parsed.value.panes[0].tag_tail != null); - try std.testing.expectEqualStrings("", parsed.value.panes[0].tag_tail.?); -} - -test "validation bounds pane restore state" { - const ids = [_]usize{0}; - const columns = [_]Column{.{ .panes = &ids }}; - var panes = [_]Pane{.{ - .kind = .terminal, - .tag = "term", - .body = "", - .terminal = .{}, - }}; - const state = State{ - .screen = .{ .cols = 80, .rows = 24 }, - .columns = &columns, - .panes = &panes, - }; - try validate(state); - - panes[0].vweight = 0; - try std.testing.expectError(error.BadDumpPaneWeight, validate(state)); - panes[0].vweight = std.math.inf(f32); - try std.testing.expectError(error.BadDumpPaneWeight, validate(state)); - panes[0].vweight = 1; - - panes[0].scroll = @as(usize, @intCast(std.math.maxInt(i32))) + 1; - try std.testing.expectError(error.BadDumpPaneScroll, validate(state)); - panes[0].scroll = 0; - - var oversized_tail: [limits.max_tag_tail + 1]u8 = @splat('x'); - panes[0].tag_tail = &oversized_tail; - try std.testing.expectError(error.BadDumpTagTail, validate(state)); - panes[0].tag_tail = oversized_tail[0..limits.max_tag_tail]; - try validate(state); -} - -// ---- a whole editor to and from a dump (acme keeps this in rows.c) ---- - -pub fn dumpState(p: *Pardes) !void { - const bytes = try dumpBytes(p, null, null); - if (p.dump_out) |d| p.gpa.free(d); - p.dump_out = bytes; - p.emit(.write_dump); + return p; } -/// The dump of the panes `keep` marks (every pane for null), gpa-owned: a -/// whole session's, or what a grip carries to another window (Handover.zig), -/// `carried` saying which. Only the columns holding a kept pane are in it, -/// and a carried pane's column tag is its old column's no longer. -pub fn dumpBytes(p: *Pardes, keep: ?*const [MAX_PANES]bool, carried: ?Carried) ![]u8 { - const arena = p.scratch.allocator(); - var slot_to_pane: [MAX_PANES]?usize = @splat(null); - var dump_panes: [MAX_PANES]Pane = undefined; - var panes_len: usize = 0; - for (p.panes, 0..) |slot, id| { - const pane = slot orelse continue; - if (keep) |k| if (!k[id]) continue; - slot_to_pane[id] = panes_len; - dump_panes[panes_len] = try dumpPane(p, arena, pane); - panes_len += 1; - } - // An attachment names its owner by its place in the dump: serials are - // handed out again on a Restore. - for (0..p.panes.len) |id| { - const i = slot_to_pane[id] orelse continue; - const owner = layout.ownerOf(p, id) orelse continue; - // An attachment carried without its owner stands on its own. - dump_panes[i].attached_to = slot_to_pane[owner]; - } - // Zerox's twins: the text once, in the first; each other names it. - for (p.panes, 0..) |slot, id| { - const f = (slot orelse continue).file orelse continue; - if (f.twin == 0) continue; - const i = slot_to_pane[id] orelse continue; - for (p.panes[0..id], 0..) |earlier, j_id| { - const of = (earlier orelse continue).file orelse continue; - if (of.twin != f.twin) continue; - const j = slot_to_pane[j_id] orelse break; - if (dump_panes[i].file) |*df| { - df.twin_of = @intCast(j); - df.content_b64 = ""; - } - break; - } - } - - var columns: [MAX_COLS]Column = undefined; - var columns_len: usize = 0; - var column_ids: [MAX_COLS][MAX_PANES]usize = undefined; +/// The panes a column lists that are not there (another build's dump left +/// them out) taken out of it, and the keyboard on one that is. +fn dropMissing(p: *Pardes) void { for (0..p.ncol) |c| { - var ids_len: usize = 0; - for (0..p.col_n[c]) |k| if (slot_to_pane[p.col_panes[c][k]]) |compact| { - column_ids[c][ids_len] = compact; - ids_len += 1; + var n: usize = 0; + for (p.col_panes[c][0..p.col_n[c]]) |id| if (id < MAX_PANES and p.panes[id] != null) { + p.col_panes[c][n] = id; + n += 1; }; - if (keep != null and ids_len == 0) continue; - columns[columns_len] = .{ - .serial = layout.columnSerial(p, c), - .tag = if (carried == .pane) null else p.col_tags[c].own, - .weight = @as(f32, @floatFromInt(p.col_weight[c])) / @as(f32, @floatFromInt(column_weight_unit)), - .panes = column_ids[c][0..ids_len], - }; - columns_len += 1; - } - - var mounts: [filesystem.max_mounts]Mount = undefined; - for (p.fs.mounts.items, 0..) |mount, i| mounts[i] = .{ .name = mount.name, .dial = mount.dial }; - const state: State = .{ - .screen = .{ .cols = p.screen_w, .rows = p.screen_h }, - .active = slot_to_pane[p.active] orelse 0, - .topbar = config.topbar_str, - .topbar_custom = p.global_tag.own, - .theme = p.theme().name, - .locations_config = p.locations_config, - .tree_context_tag_style = p.settings.tree_context_tag_style, - .columns = columns[0..columns_len], - .panes = dump_panes[0..panes_len], - .mounts = mounts[0..p.fs.mounts.items.len], - .settings = try changedSettings(p, arena), - .incl = p.incl.items, - .carried = carried, - }; - try validate(state); - var out: std.Io.Writer.Allocating = .init(p.gpa); - errdefer out.deinit(); - try std.zon.stringify.serialize(state, .{ .whitespace = true }, &out.writer); - return out.toOwnedSlice(); -} - -/// One pane as a dump keeps it, its strings in `arena`: a Restore's, and a -/// closed pane's in the store (Store.zig), which Panes brings back. -pub fn dumpPane(p: *Pardes, arena: std.mem.Allocator, pane: *pardes.panes.Pane) !Pane { - const tag = try tagline.tagText(p, arena, pane); - const body = try body_layer.bodyText(p, arena, pane); - const scroll: usize = @intCast(@max(0, pane.scroll())); - var dp: Pane = if (pane.file) |*f| try pardes.panes.File.dumpPane( - p, - arena, - pane, - f, - tag, - body, - scroll, - if (f.output) |o| pardes.panes.Output.word(o.from) else "", - if (f.output) |*o| o.arg() else "", - ) else if (pane.hasPdf()) blk: { - if (comptime !pdf_enabled) unreachable; - const pv = &pane.pdf.?; - break :blk try pardes.panes.Image.dumpPane(p, arena, pane, tag, body, pv.page, pv.path, &.{}); - } else if (pane.image) |iv| - try pardes.panes.Image.dumpPane(p, arena, pane, tag, body, scroll, iv.path, iv.raw) - else - try pardes.panes.terminal.dumpPane(pane, arena, tag, body, scroll); - dp.tag_tail = pane.tag.own; - if (dp.file) |*df| { - const dot = ctlfs.pane.dotOf(pane); - df.dot = .{ dot.q0, dot.q1 }; - } - dp.serial = pane.serial; - return dp; -} - -/// The setting lines that differ from a fresh session's, as the root ctl -/// reads them, less the ones a dump keeps apart. -fn changedSettings(p: *Pardes, arena: std.mem.Allocator) ![]const []const u8 { - var now: std.Io.Writer.Allocating = .init(arena); - var fresh: std.Io.Writer.Allocating = .init(arena); - const defaults: config.Runtime = .{}; - try pardes.ctlfs.ctl.writeSettingsOf(p, &p.settings, &now.writer); - try pardes.ctlfs.ctl.writeSettingsOf(p, &defaults, &fresh.writer); - var out: std.ArrayList([]const u8) = .empty; - var lines = std.mem.splitScalar(u8, now.written(), '\n'); - while (lines.next()) |line| { - if (line.len == 0) continue; - const word = line[0 .. std.mem.indexOfScalar(u8, line, ' ') orelse line.len]; - if (std.mem.eql(u8, word, "Theme") or std.mem.eql(u8, word, "Font")) continue; - var same = std.mem.splitScalar(u8, fresh.written(), '\n'); - while (same.next()) |other| { - if (std.mem.eql(u8, other, line)) break; - } else try out.append(arena, line); - } - return out.items; -} - -/// Applies a dump's setting lines, each as the root ctl would. -fn applySettings(p: *Pardes, lines: []const []const u8) void { - for (lines) |line| { - const sp = std.mem.indexOfScalar(u8, line, ' '); - // A ThemeFile is a request the host answers by reading the file, as - // a /ctl write of it is: asked again here, not dropped. - if (std.mem.eql(u8, line[0 .. sp orelse line.len], "ThemeFile")) { - if (sp) |s| colors.requestThemeFile(p, 0, line[s + 1 ..]); - continue; - } - const setting = config.Runtime.find(line[0 .. sp orelse line.len]) orelse continue; - _ = p.settings.apply(setting, if (sp) |s| line[s + 1 ..] else null); - if (setting.action == .tab) config.tab_now = p.settings.tab; + p.col_n[c] = n; } + if (p.active >= MAX_PANES or p.panes[p.active] == null) p.active = for (p.panes, 0..) |slot, i| { + if (slot != null) break i; + } else 0; } /// The replacement core for a Restore of the dump at `from`, whose log says /// so after its panes' `new`s, for a client that reconnects to read. -pub fn restore(p: *Pardes, zon_bytes: []const u8, from: []const u8) !*Pardes { - const taped = pardes.tape.enter(p, .{ .restore = .{ .bytes = zon_bytes, .from = from } }); +pub fn restore(p: *Pardes, bytes: []const u8, from: []const u8) !*Pardes { + const taped = pardes.tape.enter(p, .{ .restore = .{ .bytes = bytes, .from = from } }); defer pardes.tape.leave(p, taped); var opts = p.opts; opts.cols = p.screen_w; opts.rows = p.screen_h; - var old: OldSerials = .{}; - const replacement = try initDump(p.gpa, opts, zon_bytes, p, &old); + const replacement = try loadAndRevive(p.gpa, opts, bytes, p); replacement.pointRestoreAt(from); // The tape follows the replacement from here (it is the session). pardes.tape.adopt(replacement); pardes.ctlfs.events.notePath(replacement, "restore", from); - // Which serial each pane and column had, for a script that held them. - for (replacement.panes, old.panes) |slot, was| if (slot) |pane| if (was != 0) { - var rec: [32]u8 = undefined; - pardes.ctlfs.events.notePath(replacement, "restored", std.fmt.bufPrint(&rec, "{d} {d}", .{ was, pane.serial }) catch continue); - }; - for (old.cols[0..replacement.ncol], 0..) |was, c| if (was != 0) { - var rec: [32]u8 = undefined; - pardes.ctlfs.events.notePath(replacement, "restoredcol", std.fmt.bufPrint(&rec, "{d} {d}", .{ was, layout.columnSerial(replacement, c) }) catch continue); - }; return replacement; } -pub fn initFromDump(gpa: std.mem.Allocator, opts: Options, zon_bytes: []const u8) !*Pardes { +pub fn initFromDump(gpa: std.mem.Allocator, opts: Options, bytes: []const u8) !*Pardes { pardes.tape.startFromEnv(); - const taped = pardes.tape.enter(null, .{ .init_from_dump = .{ .opts = opts, .bytes = zon_bytes } }); - const p = initDump(gpa, opts, zon_bytes, null, null) catch |err| { + const taped = pardes.tape.enter(null, .{ .init_from_dump = .{ .opts = opts, .bytes = bytes } }); + const p = (if (std.mem.startsWith(u8, bytes, Handover.carried_magic)) carriedWindow(gpa, opts, bytes) else loadAndRevive(gpa, opts, bytes, null)) catch |err| { pardes.tape.leave(null, taped); return err; }; @@ -914,97 +302,65 @@ pub fn initFromDump(gpa: std.mem.Allocator, opts: Options, zon_bytes: []const u8 return p; } -/// The serials a dump's panes and columns had, read as it is restored. -const OldSerials = struct { - panes: [MAX_PANES]u32 = @splat(0), - cols: [MAX_COLS]u32 = @splat(0), -}; - -fn initDump(gpa: std.mem.Allocator, opts: Options, zon_bytes: []const u8, previous: ?*const Pardes, old_serials: ?*OldSerials) !*Pardes { +/// A window of its own for panes a grip carried out of another (Handover): +/// those panes and nothing else, its settings this process's. +fn carriedWindow(gpa: std.mem.Allocator, opts: Options, bytes: []const u8) !*Pardes { const p = try Pardes.create(gpa, opts); errdefer p.deinit(); + _ = try Handover.adopt(p, bytes); + try reviveSession(p, null); + return p; +} + +fn loadAndRevive(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, previous: ?*const Pardes) !*Pardes { + var notes: std.ArrayList(u8) = .empty; + defer notes.deinit(gpa); + var on_disk: OnDisk = @splat(false); + const p = try loadKnowing(gpa, opts, bytes, ¬es, &on_disk); + errdefer p.deinit(); + for (p.panes, on_disk, 0..) |slot, was_on_disk, i| if (slot) |pane| revivePane(p, i, pane, was_on_disk); + try reviveSession(p, previous); + if (notes.items.len > 0) { + log.warn("{s}", .{notes.items}); + const first = notes.items[0 .. std.mem.indexOfScalar(u8, notes.items, '\n') orelse notes.items.len]; + var buf: [320]u8 = undefined; + p.setMessage(p.active, std.fmt.bufPrint(&buf, "a dump of another build: {s}", .{first}) catch "a dump of another build: not all of it was read"); + } + return p; +} + +/// A loaded core given to this host (with `revivePane` for each of its +/// panes): the store and Recent read (a Restore's carried from the session +/// it replaces), its ThemeFile asked for again, and the screen this host's. +fn reviveSession(p: *Pardes, previous: ?*const Pardes) !void { if (previous) |old| { - p.next_serial = old.next_serial; - // Columns as panes: a restored one takes a fresh serial, never one - // a column of the old session had, living or deleted. - p.next_column_serial = old.next_column_serial; - p.lsp_seq = old.lsp_seq; - p.pipe.seq = old.pipe.seq; + // Never a serial or a request number the old session gave out. + p.next_serial = @max(p.next_serial, old.next_serial); + p.next_column_serial = @max(p.next_column_serial, old.next_column_serial); + p.lsp_seq = @max(p.lsp_seq, old.lsp_seq); + p.pipe.seq = @max(p.pipe.seq, old.pipe.seq); + // The host's facts, as this host told them. p.cell_pixels = old.cell_pixels; p.row_metrics = old.row_metrics; p.native_images = old.native_images; - // The workspace tag's Restore word names the dump it came from. - if (old.last_dump) |d| p.last_dump = try gpa.dupe(u8, d); - // Recent goes on across a Restore, the old session's own list. p.recent = old.recent.clone(p.recentGpa()); - // The same session: its closed panes go on being its own. p.store = old.store.clone(p.recentGpa()); } else p.loadRecent(); - var parsed = try readZon(gpa, zon_bytes, "load"); - defer parsed.deinit(); - const st = parsed.value; - p.locations_config = st.locations_config; - p.settings.tree_context_tag_style = st.tree_context_tag_style; - applySettings(p, st.settings); - if (st.incl) |dirs| { - for (p.incl.items) |dir| p.gpa.free(dir); - p.incl.clearRetainingCapacity(); - for (dirs) |dir| if (dir.len > 0 and dir[0] == '/' and std.mem.indexOfScalar(u8, dir, 0) == null) { - const copy = p.gpa.dupe(u8, dir) catch continue; - p.incl.append(p.gpa, copy) catch p.gpa.free(copy); - }; + config.tab_now = p.settings.tab; + // A ThemeFile is a request the host answers by reading the file again; + // until it has (and for good if it fails) the theme the dump had shows. + if (p.theme_file_path.get().len > 0) { + var path: [limits.host_path_cap]u8 = undefined; + const name = p.theme_file_path.get(); + @memcpy(path[0..name.len], name); + colors.requestThemeFile(p, 0, path[0..name.len]); } - // A dump naming a ThemeFile asks the host to load it again: until it - // has, and for good if it fails, the theme this session showed goes on - // (a failed ThemeFile changes nothing, a Restore's included), unless - // the dump names a Theme of its own. - if (previous) |old| if (p.theme_file_path.get().len > 0) { - const names_theme = for (st.settings) |line| { - if (std.mem.startsWith(u8, line, "Theme ")) break true; - } else false; - try colors.carryTheme(p, old, !names_theme); + for (p.panes) |slot| if (slot) |pane| { + pane.fs.unannounced = true; }; - for (st.mounts) |mount| { - var already_mounted = false; - for (p.fs.mounts.items) |existing| { - if (!std.mem.eql(u8, mount.name, existing.name)) continue; - if (!std.mem.eql(u8, mount.dial, existing.dial)) return error.MountConflict; - already_mounted = true; - break; - } - if (!already_mounted) try p.fs.mount(gpa, mount.name, mount.dial); - } - - for (themes, 0..) |t, i| { - if (std.mem.eql(u8, t.name, st.theme)) p.settings.theme = @intCast(i); - } - for (st.panes, 0..) |src, i| { - if (i >= MAX_PANES) break; - if (old_serials) |o| o.panes[i] = src.serial; - const pane = try restorePane(p, i, src); - pane.vweight = src.vweight; - pane.collapsed = src.collapsed; - } - p.ncol = @min(st.columns.len, MAX_COLS); - p.col_serial = @splat(0); - if (st.topbar_custom) |bar| p.global_tag.own = try p.gpa.dupe(u8, bar); - for (st.columns[0..p.ncol], 0..) |col, c| { - if (old_serials) |o| o.cols[c] = col.serial; - if (col.tag) |bar| p.col_tags[c].own = try p.gpa.dupe(u8, bar); - const scaled = @as(f64, @floatCast(col.weight)) * @as(f64, @floatFromInt(column_weight_unit)); - const bounded = @min(scaled, @as(f64, @floatFromInt(max_column_weight))); - p.col_weight[c] = @max(1, @as(u64, @intFromFloat(@round(bounded)))); - p.col_n[c] = @min(col.panes.len, MAX_PANES); - for (col.panes[0..p.col_n[c]], 0..) |pid, k| p.col_panes[c][k] = pid; - } - // A dump's pane i is restored into slot i: its owner's serial now. - for (st.panes[0..@min(st.panes.len, MAX_PANES)], 0..) |src, i| { - const owner = src.attached_to orelse continue; - if (owner >= MAX_PANES or owner == i) continue; - const pane = p.panes[i] orelse continue; - pane.attached_to = (p.panes[owner] orelse continue).serial; - } - p.active = @min(st.active, MAX_PANES - 1); + // This host's screen; the next sync lays the panes out on it. + p.screen_w = p.opts.cols; + p.screen_h = p.opts.rows; p.sync(); exec.applyStartupConfig(p); colors.finishThemeInitialization(p); @@ -1013,89 +369,109 @@ fn initDump(gpa: std.mem.Allocator, opts: Options, zon_bytes: []const u8, previo _ = p.takeHaptic(); // see init: a restored session is not a gesture try p.fs.log.buf.ensureTotalCapacityPrecise(p.gpa, p.fs.log.cap); ctlfs.events.announce(p); - return p; } -/// Dump pane `src` made a pane again in slot `i`, installed (a fresh -/// serial) and not yet placed: a Restore's, and a closed pane Panes brings -/// back (Store.zig). -pub fn restorePane(p: *Pardes, i: usize, src: Pane) !*pardes.panes.Pane { - const gpa = p.gpa; - const pane: *pardes.panes.Pane = switch (src.kind) { - .terminal => terminal: { - const t = src.terminal.?; - // A host that runs shells gets them back: the old history - // above a new shell in the directory it was in. The web - // shell and an isolated build only ever replay it. - // A command pane comes back finished, showing what it said - // and how it ended: its command is not run again. - const live = comptime (terminal_panes and platform != .web and !isolated); - const revive = live and t.cwd.len <= effect_path_cap and t.command.len == 0; - const restored = try pardes.panes.terminal.restore(p, src, revive); - p.installPane(i, restored); - try restored.setOwnedCwd(t.cwd); - if (t.shell.len > 0) restored.shell = try gpa.dupe(u8, t.shell); - if (t.command.len > 0) { - restored.command = try gpa.dupe(u8, t.command); - restored.command_done = true; - restored.command_status = t.status; - } - if (revive) p.emitSpawn(i, restored.serial, t.cwd); - if (t.petscii and pardes.panes.terminal.petscii(restored) == false) pardes.panes.terminal.togglePetscii(restored); - break :terminal restored; - }, - .file => file: { - const restored = try pardes.panes.File.restore(p, i, src); - // A twin: on the earlier pane's buffer again. - if (src.file.?.twin_of) |j| if (j < i) pardes.panes.File.relink(p, j, i, src.file.?.dot); - break :file restored; - }, - .image => restore_image: { - const im = src.image.?; - if (pardes.panes.Pdf.isPath(im.path)) { - var raw: []u8 = if (im.bytes_b64.len > 0) - try decodeBytes(gpa, im.bytes_b64) - else - &.{}; - errdefer if (raw.len > 0) gpa.free(raw); - if (comptime pdf_enabled) { - if (pardes.panes.Pdf.openPane(p, i, im.path, src.scroll + 1, .last) catch null) |restored| { - if (raw.len > 0) gpa.free(raw); - raw = &.{}; - // Its fit and tint as they were (PdfFit, PdfTint). - const pv = &restored.pdf.?; - if (std.meta.stringToEnum(@TypeOf(pv.fit), im.pdf_fit)) |fit| if (fit != pv.fit) pardes.panes.Pdf.toggleFit(restored); - if (std.meta.stringToEnum(@TypeOf(pv.tint), im.pdf_tint)) |tint| while (pv.tint != tint) pardes.panes.Pdf.toggleTint(restored); - restored.cols = @max(1, src.cols); - restored.rows = @max(1, src.rows); - break :restore_image restored; - } - } - // If the source path is unavailable, retain its bytes - // as an ordinary file rather than dropping the pane. - const path = try gpa.dupe(u8, im.path); - errdefer gpa.free(path); - const content = if (raw.len > 0) raw else try gpa.dupe(u8, ""); - raw = &.{}; - errdefer if (content.len > 0) gpa.free(content); - const history = try pardes.panes.File.History.create(gpa); - errdefer gpa.destroy(history); - const restored = try p.newDocPane(i); - restored.file = .{ .path = path, .content = content, .history = history }; - restored.body.cur_pinned = true; - restored.cols = @max(1, src.cols); - restored.rows = @max(1, src.rows); - p.emit(.{ .watch = .{ .pane = @intCast(i), .on = true, .mode = .baseline_disk } }); - break :restore_image restored; - } - break :restore_image try pardes.panes.Image.restore(p, i, src); - }, +/// A loaded pane given to this host: a terminal a new shell in its old +/// one's directory under its history, a file watched again (one changed on +/// disk since comes back as it is now, one unsaved is marked changed), a +/// PDF opened again. +fn revivePane(p: *Pardes, id: usize, pane: *pardes.panes.Pane, was_on_disk: bool) void { + if (pane.terminal != null) { + // A command pane comes back finished, showing what it said: its + // command is not run again. A host that runs shells starts one + // under the history in the old one's directory; the web shell and + // an isolated build only show it. + if (pane.command != null) { + pane.command_done = true; + return; + } + if (comptime !(terminal_panes and platform != .web and !isolated)) return; + if (pane.cwdSlice().len > effect_path_cap) return; + pardes.panes.terminal.reviveShell(pane); + p.emitSpawn(id, pane.serial, pane.cwdSlice()); + return; + } + if (comptime pdf_enabled) if (pane.pdf != null) { + pardes.panes.Pdf.reopen(p, pane) catch |err| { + p.reportError(id, "Restore", err); + p.removePane(id, null) catch {}; + }; + return; }; - tagline.restoreDumpTail(p, pane, src); + const f = if (pane.file) |*file| file else return; + if (f.output != null or filesystem.localPath(f.path) == null) return; + p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true } }); + // Its file gone from disk since the dump: its text is nowhere else, so + // it is said deleted, and Del, Exit and Restore ask first. One never on + // disk (a name not yet saved) comes back as it was. + if (was_on_disk and !onDisk(pane)) pardes.panes.File.deleted(p, @intCast(id)); +} + +// ---- one pane to and from a dump: the store's, a handover's ---- + +/// Pane `pane` alone as a dump (`gpa`'s): what it shares with another +/// pane is its own in it (a directory it follows), and nothing says where +/// it was. `slim`: without a terminal's history, a file's undo, or a clean +/// file's text, which is read from its file again (a store's record past +/// its bound). +pub fn savePane(gpa: std.mem.Allocator, pane: *pardes.panes.Pane, slim: bool) ![]u8 { + const was = pane.*; + defer pane.* = was; + if (was.cwd == .inherited) { + // Read before the union is written (its tag is set first). + const dir = pane.cwdSlice(); + pane.cwd = .{ .owned = @constCast(dir) }; + } + pane.attached_to = null; + var empty: pardes.panes.File.History = .{}; + if (slim) { + pane.terminal = null; + if (pane.file) |*f| { + f.history = ∅ + if (f.output == null and f.revision == f.saved_revision and filesystem.localPath(f.path) != null) f.content = &.{}; + } + } + return walk.save(pardes.panes.Pane, gpa, pane); +} + +/// A pane `savePane` made, installed in free slot `id` with a serial of +/// this session's and not yet placed, and given to the host as a loaded +/// session's are (`revive`). +pub fn loadPane(p: *Pardes, id: usize, bytes: []const u8) !*pardes.panes.Pane { + var l = try walk.Load(&.{pardes.panes.Pane}).init(p.gpa, bytes); + defer l.deinit(); + l.allocs = .{ .image = p.image_gpa, .pdf = p.pdf_gpa }; + const pane = try p.gpa.create(pardes.panes.Pane); + errdefer { + l.rollback(); + p.gpa.destroy(pane); + } + pane.* = try l.get(pardes.panes.Pane); + // The slots and serials of the session it was in name nothing here. + pane.search_pane = null; + pane.search_row = null; + pane.command_from = 0; + pane.places.from = 0; + pane.collapsed = false; + if (pane.file) |*f| if (f.content.len == 0 and (f.saved_len orelse 0) > 0) { + // A clean file kept slim: its text is its file's. + if (filesystem.read(p, f.path)) |text| { + f.content = text; + } else |_| {} + }; + if (pane.terminal == null and pane.file == null and pane.image == null and !pane.hasPdf()) { + // A terminal kept slim: a new emulator, a new shell. + const fresh = try pardes.panes.terminal.create(p.gpa, @max(1, pane.cols), @max(1, pane.rows)); + pane.terminal = fresh.terminal; + fresh.terminal = null; + p.gpa.destroy(fresh); + } + p.installPane(id, pane); + revivePane(p, id, pane, false); return pane; } -test "a dump keeps the settings that differ from a fresh session's, and a restore applies them" { +test "a dump keeps the session's settings, and a restore applies them" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true }); defer p.deinit(); @@ -1103,10 +479,6 @@ test "a dump keeps the settings that differ from a fresh session's, and a restor try std.testing.expect(p.executeBuiltinLine(p.active, "Verbose off")); try std.testing.expect(p.executeBuiltinLine(p.active, "JumpScope all")); try dumpState(p); - try std.testing.expect(std.mem.indexOf(u8, p.dump_out.?, "\"Placement pardes\"") != null); - try std.testing.expect(std.mem.indexOf(u8, p.dump_out.?, "\"JumpScope all\"") != null); - // What is left at its default is not written. - try std.testing.expect(std.mem.indexOf(u8, p.dump_out.?, "\"BootShell keep\"") == null); const restored = try initFromDump(gpa, .{ .tty_only = true }, p.dump_out.?); defer restored.deinit(); try std.testing.expectEqual(config.Runtime.Placement.pardes, restored.settings.placement); diff --git a/src/file_watch.zig b/src/file_watch.zig index 9b41d146..f640fa5f 100644 --- a/src/file_watch.zig +++ b/src/file_watch.zig @@ -669,30 +669,27 @@ pub fn identify(io: std.Io, path: []const u8) !Identity { }; } -test "restored file watches preserve snapshots and accept later disk changes" { +test "a restored file is watched again: one clean takes what its file became since the dump, one unsaved keeps its text" { if (comptime !supported or !filesystem.platform_has_fs) return; const io = std.testing.io; const gpa = std.testing.allocator; - for ([_]struct { dirty: bool, exists: bool }{ - .{ .dirty = false, .exists = true }, - .{ .dirty = true, .exists = true }, - .{ .dirty = true, .exists = false }, - }) |case| { + for ([_]bool{ false, true }) |dirty| { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); - if (case.exists) try tmp.dir.writeFile(io, .{ .sub_path = "watched", .data = "disk baseline\n" }); + try tmp.dir.writeFile(io, .{ .sub_path = "watched", .data = "dumped\n" }); var path_buf: [256]u8 = undefined; const path = try std.fmt.bufPrint(&path_buf, ".zig-cache/tmp/{s}/watched", .{tmp.sub_path}); - const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); + const source = try pardes.Pardes.init(gpa, .{ .tty_only = true }); + defer source.deinit(); + const id: u8 = @intCast(source.freeSlot().?); + const opened = try pardes.panes.File.open(source, id, path, 0); + if (dirty) _ = pardes.ctlfs.pane.spliceBody(source, opened, 0, 0, "unsaved ").?; + try pardes.dump.dumpState(source); + // Changed on disk between the dump and its load. + try tmp.dir.writeFile(io, .{ .sub_path = "watched", .data = "changed since\n" }); + const core = try pardes.dump.initFromDump(gpa, .{ .tty_only = true }, source.dump_out.?); defer core.deinit(); - while (core.nextEffect()) |_| {} - const id: u8 = @intCast(core.freeSlot().?); - const pane = try pardes.panes.File.restore(core, id, .{ - .kind = .file, - .tag = "", - .body = "", - .file = .{ .path = path, .content = "restored snapshot\n", .dirty = case.dirty }, - }); + const pane = core.panes[id].?; const fd = init(true); if (fd < 0) return error.NoWatcher; defer _ = libc.close(fd); @@ -701,36 +698,16 @@ test "restored file watches preserve snapshots and accept later disk changes" { var armed = false; while (core.nextEffect()) |effect| switch (effect) { .watch => |watch| if (watch.pane == id and watch.on) { - try std.testing.expectEqual(.baseline_disk, watch.mode); - try std.testing.expect(!applyEffect(core, io, fd, &watches, id, watch.on, watch.mode)); + _ = applyEffect(core, io, fd, &watches, id, watch.on, watch.mode); armed = true; }, else => {}, }; try std.testing.expect(armed and watches[id] != null); - // A clean pane's text is its file's, read at the Restore: a dump - // that kept other text (an older dump) is not what comes back. - const restored: []const u8 = if (case.dirty) "restored snapshot\n" else "disk baseline\n"; - try std.testing.expectEqualStrings(restored, pane.file.?.content); - try std.testing.expectEqual(@as(usize, 0), pane.file.?.history.undo_len); - try std.testing.expectEqual(case.dirty, pane.file.?.revision != pane.file.?.saved_revision); - _ = reloadChanged(core, io, gpa, &watches); - try std.testing.expectEqualStrings(restored, pane.file.?.content); - - try tmp.dir.writeFile(io, .{ .sub_path = "watched", .data = "later disk save\n" }); - _ = reloadChanged(core, io, gpa, &watches); - // A snapshot restored with unsaved text keeps it: a later disk - // change is said, never loaded over it (File.changed). - if (case.dirty) { - try std.testing.expectEqualStrings("restored snapshot\n", pane.file.?.content); + if (dirty) { + try std.testing.expectEqualStrings("unsaved dumped\n", pane.file.?.content); try std.testing.expect(pane.file.?.disk_newer != null); - continue; - } - try std.testing.expectEqualStrings("later disk save\n", pane.file.?.content); - pardes.panes.File.undo(core, pane); - try std.testing.expectEqualStrings(restored, pane.file.?.content); - _ = reloadChanged(core, io, gpa, &watches); - try std.testing.expectEqualStrings(restored, pane.file.?.content); + } else try std.testing.expectEqualStrings("changed since\n", pane.file.?.content); } } diff --git a/src/image.zig b/src/image.zig index 20d6d6f0..8f193674 100644 --- a/src/image.zig +++ b/src/image.zig @@ -11,7 +11,6 @@ const pdf_enabled = @import("pardes_config").mupdf; const pardes = @import("pardes.zig"); const config = @import("config.zig"); const filesystem = @import("fs.zig"); -const dump = @import("dump.zig"); // Glyph matching adapted from caioluders/petsciinator. pub const GlyphArt = struct { pub const commodore = [16][3]u8{ @@ -990,68 +989,6 @@ pub fn create(p: *pardes.Pardes, id: usize, path: []const u8, raw: []u8) !*parde return pane; } -/// Serialize the binary image record used by images and, for dump-schema -/// compatibility, PDFs. Common pane metadata is supplied by the core. -pub fn dumpPane( - p: *pardes.Pardes, - arena: std.mem.Allocator, - pane: *const pardes.Pane, - tag: []const u8, - body: []const u8, - scroll: usize, - path: []const u8, - raw: []const u8, -) !dump.Pane { - // A picture or PDF on disk is read from there at Restore, as a clean - // file's text is: the dump keeps only the bytes of one that is not - // (megabytes of a PDF per dump, and a stale copy, otherwise). - _ = p; - const on_disk = if (comptime !pardes.hosted) false else if (filesystem.localPath(path)) |local| @import("exec.zig").kindOf(local) == .file else false; - const bytes: []const u8 = if (on_disk) &.{} else raw; - return .{ - .kind = .image, - .tag = tag, - .body = body, - .scroll = scroll, - .cols = pane.cols, - .rows = pane.rows, - .vweight = pane.vweight, - .collapsed = pane.collapsed, - .image = .{ - .path = path, - .bytes_b64 = if (bytes.len > 0) try dump.encodeBytes(arena, bytes) else "", - .petscii = if (pane.image) |state| state.glyph_art else false, - .palette = if (pane.image) |state| switch (state.pmode) { - .commodore => .commodore, - .terminal => .terminal, - } else .commodore, - .ascii = if (pane.image) |state| state.ascii else true, - .pdf_fit = if (comptime pardes.pdf_enabled) (if (pane.pdf) |pv| @tagName(pv.fit) else "") else "", - .pdf_tint = if (comptime pardes.pdf_enabled) (if (pane.pdf) |pv| @tagName(pv.tint) else "") else "", - }, - }; -} - -pub fn restore(p: *pardes.Pardes, id: usize, src: dump.Pane) !*pardes.Pane { - const saved = src.image.?; - var raw: []u8 = if (saved.bytes_b64.len > 0) - try dump.decodeBytes(p.image_gpa, saved.bytes_b64) - else - &.{}; - errdefer if (raw.len > 0) p.image_gpa.free(raw); - const pane = try create(p, id, saved.path, raw); - raw = &.{}; - pane.image.?.glyph_art = saved.petscii; - pane.image.?.pmode = switch (saved.palette) { - .commodore => .commodore, - .terminal => .terminal, - }; - pane.image.?.ascii = saved.ascii; - pane.cols = @max(1, src.cols); - pane.rows = @max(1, src.rows); - return pane; -} - pub fn toggleGlyphArt(state: *State) void { state.glyph_art = !state.glyph_art; } diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index 3176ebc8..1b9d73f5 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -2075,8 +2075,7 @@ test "Zerox twins dump their text once and come back on one buffer from a Restor try pardes.dump.dumpState(p); const text = p.dump_out.?; // The text is kept once though three panes show it. - const encoded = try pardes.dump.encodeBytes(p.scratch.allocator(), p.panes[0].?.file.?.content); - try testing.expectEqual(@as(usize, 1), std.mem.count(u8, text, encoded)); + try testing.expectEqual(@as(usize, 1), std.mem.count(u8, text, p.panes[0].?.file.?.content)); const restored = try pardes.dump.restore(p, text, "/tmp/pardes.dump.zon"); defer restored.deinit(); var files: [3]*pardes.panes.File.State = undefined; @@ -2838,7 +2837,7 @@ test "a Restore of a file that is no dump fails the write before any warning, an const root_ctl = @backingInt(tree.TopFile.ctl); const refused = wr(p, root_ctl, line); try testing.expectEqual(E.IO, refused.errno()); - try testing.expect(std.mem.indexOf(u8, refused.reply.ename, "not a pardes dump: line 1: ") != null); + try testing.expect(std.mem.indexOf(u8, refused.reply.ename, "not a pardes dump: it does not begin as one") != null); try testing.expect(p.restore_req == null); try testing.expect(p.panes[0].?.discard_warned == null); @@ -4899,46 +4898,6 @@ test "Petscii, Palette and Ascii take the state their tag word names, set rather try testing.expect(th.logHas(p, "Petscii takes on or off")); } -test "a Restore reads a clean file from disk; an unsaved one whose file changed since is said so, and its first Save asks" { - if (comptime !pardes.hosted) return; - const gpa = testing.allocator; - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "one\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var path_buf: [4200]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, "{s}/a.txt", .{dir}); - const p = try Pardes.init(gpa, .{ .file = path, .tty_only = true, .cols = 80, .rows = 24 }); - defer p.deinit(); - // Clean: the dump keeps no text, and the newer disk is what comes back. - try pardes.dump.dumpState(p); - try testing.expect(std.mem.indexOf(u8, p.dump_out.?, "b25lCg==") == null); // "one\n" in base64 - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "two\n" }); - { - const restored = try pardes.dump.restore(p, p.dump_out.?, "/tmp/pardes.dump.zon"); - defer restored.deinit(); - const f = pane_files.fileOf(restored.panes[0].?).?; - try testing.expectEqualStrings("two\n", f.content); - try testing.expectEqual(f.saved_revision, f.revision); - } - // Unsaved: its text comes back, the disk changed under it is said, and - // Save asks once before overwriting it. - const f = pane_files.fileOf(p.panes[0].?).?; - const edited = try gpa.dupe(u8, "edited\n"); - pardes.panes.File.setContent(p, f, edited); - try testing.expect(f.revision != f.saved_revision); - try pardes.dump.dumpState(p); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "three\n" }); - const restored = try pardes.dump.restore(p, p.dump_out.?, "/tmp/pardes.dump.zon"); - defer restored.deinit(); - const rf = pane_files.fileOf(restored.panes[0].?).?; - try testing.expectEqualStrings("edited\n", rf.content); - try testing.expect(rf.disk_newer != null); - pardes.exec.saveFile(restored, 0); - try testing.expect(th.logHas(restored, "modified on disk since read (Save again to overwrite)")); -} - test "a Rename the server answered for this file alone, with other open files saying the name, is previewed and says so" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -5086,25 +5045,6 @@ test "a command or a terminal refused for room is refused before anything is log } } -test "an unsaved pane from a dump that says nothing of its disk asks at its first Save" { - if (comptime !pardes.hosted) return; - const gpa = testing.allocator; - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "old.txt", .data = "disk\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var path_buf: [4200]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, "{s}/old.txt", .{dir}); - const p = try withFile(gpa, "x\n"); - defer p.deinit(); - const id = p.freeSlot().?; - const pane = try pardes.panes.File.restore(p, id, .{ .kind = .file, .tag = "", .body = "", .file = .{ .path = path, .content = "unsaved\n", .dirty = true } }); - try testing.expect(pane.file.?.disk_newer != null); - pardes.exec.saveFile(p, id); - try testing.expect(th.logHas(p, "modified on disk since read (Save again to overwrite)")); -} - test "a Mount of a socket path that is not there says no such socket, ENOENT" { if (comptime !pardes.hosted) return; const p = try withFile(testing.allocator, "x\n"); diff --git a/src/ninep/events.zig b/src/ninep/events.zig index 78788c3c..f24c6120 100644 --- a/src/ninep/events.zig +++ b/src/ninep/events.zig @@ -1305,27 +1305,23 @@ test "a Dump written and a Restore made are in the log, with their files" { var want: [64]u8 = undefined; try testing.expect(std.mem.indexOf(u8, log.bytes, try std.fmt.bufPrint(&want, "{s} /tmp/pardes.dump.zon\n", .{what})) != null); } - // The replacement's last says which serial each pane had. - const log = try freezeLog(restored); - defer restored.gpa.free(log.bytes); - var want: [64]u8 = undefined; - try testing.expect(std.mem.indexOf(u8, log.bytes, try std.fmt.bufPrint(&want, "restore /tmp/pardes.dump.zon\nrestored {d} {d}\n", .{ p.panes[0].?.serial, restored.panes[0].?.serial })) != null); - // ...and each column's: restoredcol . - try testing.expect(std.mem.endsWith(u8, log.bytes, try std.fmt.bufPrint(&want, "restoredcol {d} {d}\n", .{ pardes.layout.columnSerial(p, 0), pardes.layout.columnSerial(restored, 0) }))); + // Its panes and columns are the dump's, serials and all. + try testing.expectEqual(p.panes[0].?.serial, restored.panes[0].?.serial); + try testing.expectEqual(pardes.layout.columnSerial(p, 0), pardes.layout.columnSerial(restored, 0)); } -test "a Restore gives columns fresh serials, past every serial the old session handed out" { +test "after a Restore a new pane or column takes a serial past every one the old session handed out" { const p = try withFile(testing.allocator, "one\n"); defer p.deinit(); - const first = pardes.layout.columnSerial(p, 0); - // Serials spent on columns since deleted. - p.next_column_serial += 5; - const spent = p.next_column_serial; try dump.dumpState(p); + // Serials spent since the dump, on panes and columns since deleted. + p.next_serial += 5; + p.next_column_serial += 5; + const spent = p.next_serial; + const spent_col = p.next_column_serial; const restored = try dump.restore(p, p.dump_out.?, "/tmp/pardes.dump.zon"); defer restored.deinit(); - for (0..restored.ncol) |c| try testing.expect(pardes.layout.columnSerial(restored, c) >= spent); - try testing.expect(pardes.layout.columnSerial(restored, 0) != first); + try testing.expect(restored.next_serial >= spent and restored.next_column_serial >= spent_col); } test "a pane named before it is announced has its new under that name, and no rename before it" { diff --git a/src/panes.zig b/src/panes.zig index 89f24eb6..64851787 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -9,7 +9,6 @@ const modal = @import("modal.zig"); const filesystem = @import("fs.zig"); const syntax = @import("syntax.zig"); const tracy = @import("tracy.zig"); -const dump = @import("dump.zig"); const limits = @import("memory.zig").limits; const builtins = @import("builtins.zig"); const effect_sources = @import("effect_sources.zig"); @@ -33,6 +32,9 @@ pub const Pane = struct { .derived = .{ "wrap_line", "wrap_col", "wrap_pad", "wrap_n", "body_wrapped", "context_rows", "body_rows", "body_visible_rows", "context_row_limit", "geometry_key", "geometry_len", "view", "notices", "pan" }, .authority = .{"fs"}, .len = .{ .{ "msg", "msg_len" }, .{ "msg_older", "msg_older_len" }, .{ "question", "question_len" }, .{ "shell_tail", "shell_tail_len" }, .{ "sel_snap", "nsel_snap" } }, + // A picture's and a PDF's memory is their own allocators' (a dump + // loaded makes it there). + .alloc = .{ .{ "image", "image" }, .{ "pdf", "pdf" } }, }; /// The words that throw unsaved text away (builtins.zig warnModified). pub const Discarding = enum { Exit, Restore, get, Del, Delcol }; diff --git a/src/pardes.zig b/src/pardes.zig index eb09b1f2..0806cc78 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -2035,14 +2035,6 @@ test "image dump restores source bytes renderer choices and exact custom tail" { pane.tag.own = try p.gpa.dupe(u8, " Keep Del"); try dump.dumpState(p); - var first_dump = try dump.readZon(gpa, p.dump_out.?, "image-first-dump"); - defer first_dump.deinit(); - const first = first_dump.value; - try std.testing.expect(first.panes[0].image.?.petscii); - try std.testing.expectEqual(dump.ImagePalette.terminal, first.panes[0].image.?.palette); - try std.testing.expect(!first.panes[0].image.?.ascii); - try std.testing.expectEqualStrings(" Keep Del", first.panes[0].tag_tail.?); - const restored = try dump.initFromDump(gpa, .{ .tty_only = true }, p.dump_out.?); defer restored.deinit(); const restored_pane = restored.panes[0].?; @@ -2053,18 +2045,11 @@ test "image dump restores source bytes renderer choices and exact custom tail" { try std.testing.expectEqualStrings(" Keep Del", tagline.curTail(restored_pane)); try std.testing.expectEqualSlices(u8, source, restored_pane.image.?.raw); + // And dumped again, its bytes still there. try dump.dumpState(restored); - var parsed = try dump.readZon(gpa, restored.dump_out.?, "image-redump"); - defer parsed.deinit(); - const redump = parsed.value; - const encoded = redump.panes[0].image.?.bytes_b64; - const decoded = try dump.decodeBytes(gpa, encoded); - defer gpa.free(decoded); - try std.testing.expectEqualSlices(u8, source, decoded); - try std.testing.expect(redump.panes[0].image.?.petscii); - try std.testing.expectEqual(dump.ImagePalette.terminal, redump.panes[0].image.?.palette); - try std.testing.expect(!redump.panes[0].image.?.ascii); - try std.testing.expectEqualStrings(" Keep Del", redump.panes[0].tag_tail.?); + const again = try dump.load(gpa, .{ .tty_only = true }, restored.dump_out.?, null); + defer again.deinit(); + try std.testing.expectEqualSlices(u8, source, again.panes[0].?.image.?.raw); } test "a Look on a directory does not type ls into an occupied terminal" { @@ -2366,25 +2351,30 @@ test "owned cwd EOF failure retains the pane and Del retries its close" { } test "owned cwd restore either copies the directory or preserves the old core" { + // No terminal: ghostty 1.4's snapshot decode leaks when an allocation + // inside it fails, and this fails every allocation of a Restore in turn + // (what pardes's own load made is freed, src/walk.zig rollback). var allocator = std.testing.FailingAllocator.init(std.testing.allocator, .{}); - const p = try Pardes.init(allocator.allocator(), .{ .tty_only = true }); + const p = try Pardes.init(allocator.allocator(), .{ .tty_only = true, .file = "/nonexistent/pardes-owned-cwd" }); defer p.deinit(); - p.setCwd(0, "/retained/terminal/directory"); + p.newScratchBelow(0); + const id = p.active; + try p.panes[id].?.setOwnedCwd("/retained/terminal/directory"); try dump.dumpState(p); - const original = p.panes[0].?; + const original = p.panes[id].?; var completed = false; for (0..256) |failure| { allocator.has_induced_failure = false; allocator.fail_index = allocator.alloc_index + failure; const replacement = dump.restore(p, p.dump_out.?, "test") catch { try std.testing.expect(allocator.has_induced_failure); - try std.testing.expectEqual(original, p.panes[0].?); + try std.testing.expectEqual(original, p.panes[id].?); try std.testing.expectEqualStrings("/retained/terminal/directory", original.cwdSlice()); continue; }; defer replacement.deinit(); - try std.testing.expectEqualStrings("/retained/terminal/directory", replacement.panes[0].?.cwdSlice()); - try std.testing.expectEqual(original, p.panes[0].?); + try std.testing.expectEqualStrings("/retained/terminal/directory", replacement.panes[id].?.cwdSlice()); + try std.testing.expectEqual(original, p.panes[id].?); if (!allocator.has_induced_failure) { completed = true; break; @@ -2939,8 +2929,7 @@ test "restored terminals keep monotonic nonzero pane identities" { const gpa = std.testing.allocator; const source = try Pardes.init(gpa, .{ .shells = 3 }); defer source.deinit(); - // Filter is a fresh presentation default, not emulator/dump state. Even a - // pane toggled off in the source session comes back with the default on. + // Filter is the pane's, and a dump keeps it: one toggled off comes back off. source.panes[1].?.tty_filter = false; try dump.dumpState(source); @@ -2953,7 +2942,7 @@ test "restored terminals keep monotonic nonzero pane identities" { for (restored.panes, 0..) |slot, id| { const pane = slot orelse continue; if (!pane.isTerminal()) continue; - try std.testing.expect(pane.tty_filter); + try std.testing.expectEqual(id != 1, pane.tty_filter); try std.testing.expect(pane.serial != 0); for (serials[0..n]) |seen| try std.testing.expect(pane.serial != seen); ids[n] = id; @@ -4739,15 +4728,19 @@ pub const Pardes = struct { "needs_frame", "present_skipped", "shell_continuous", "native_image_patches", // Drained by every pump before its digest; a replica's queues // are its own. - "effects", "in_q", + "effects", "effects_head", "effects_len", "in_q", "in_head", "in_len", // The views not live: written whenever one goes out, so the // live one's slot is stale (views.zig). "parked", + // How this process was started, and the last dump it made for + // its host to write: a session loaded elsewhere has its own. + "opts", "dump_out", }, .derived = .{ "shell_rows", "diff_output", "locations_cache", "retired_panes", "tty_filter_palette", "body_chrome" }, // The host's handles, the 9P server's fids and log, the store file - // every window of the user shares (and Recent read from it). - .authority = .{ "host", "fs", "store", "recent" }, + // every window of the user shares (and Recent read from it), and + // the files of a host with no file system of its own. + .authority = .{ "host", "fs", "store", "recent", "fallback" }, .len = .{ .{ "jumps", "njumps" }, .{ "look_src", "n_look_src" }, .{ "leader_keys", "leader_n" }, .{ "col_x", "ncol" }, .{ "col_w", "ncol" }, .{ "session_dir", "session_dir_len" } }, }; diff --git a/src/pdf.zig b/src/pdf.zig index d3796d70..262af8b9 100644 --- a/src/pdf.zig +++ b/src/pdf.zig @@ -700,7 +700,9 @@ test "oriented selection containment delegates to MuPDF quad geometry" { } pub const Document = struct { - handle: *c.pardes_pdf_document, + /// None in a document loaded from a dump until its PDF is opened again + /// (pdf_view.reopen). + handle: ?*c.pardes_pdf_document = null, pages: usize, /// The open document is MuPDF's, made from the file's bytes (src/walk.zig). diff --git a/src/pdf_view.zig b/src/pdf_view.zig index b2b9c5a3..0745a6fc 100644 --- a/src/pdf_view.zig +++ b/src/pdf_view.zig @@ -2848,6 +2848,20 @@ pub fn deinitPane(core: *pardes.Pardes, pane: *pardes.Pane, state: *State) void state.deinit(core.pdf_gpa); } +/// A PDF pane loaded from a dump given its document again (MuPDF's, so +/// never in the dump) from its file; one changed since is laid out anew. +pub fn reopen(core: *pardes.Pardes, pane: *pardes.Pane) !void { + if (comptime !enabled) return; + const state = &(pane.pdf orelse return); + const local = filesystem.localPath(state.path) orelse return error.NonLocalPath; + const bytes = try filesystem.readFile(core.pdf_gpa, local); + defer core.pdf_gpa.free(bytes); + state.document = try Document.openBytes(bytes); + if (state.document.pages != state.page_count or fileKey(state.path, bytes) != state.file) { + try state.reload(core.pdf_gpa); + } +} + pub fn reloadPane( core: *pardes.Pardes, pane: *pardes.Pane, diff --git a/src/tagline.zig b/src/tagline.zig index f669e98b..7af9ba66 100644 --- a/src/tagline.zig +++ b/src/tagline.zig @@ -10,7 +10,6 @@ const edit = @import("edit.zig"); const exec = @import("exec.zig"); const config = @import("config.zig"); const pdf_enabled = panes.Pdf.enabled; -const dump = @import("dump.zig"); const Layer = @import("Layer.zig"); const Pane = panes.Pane; const Text = panes.Text; @@ -424,13 +423,6 @@ pub fn enterHeader(p: *Pardes, column: ?usize) void { t.normal.clear(); } -/// A dump's tag tail, what was typed after the name: a dump from 0.4 on -/// writes it, and an older one's saved tag is not read. -pub fn restoreDumpTail(p: *Pardes, pane: *Pane, src: dump.Pane) void { - const tail = src.tag_tail orelse return; - pane.tag.own = p.gpa.dupe(u8, tail) catch return; -} - /// The span a header's selection covers on line `row` of its text, in /// that line's bytes: up to the caret while typing, and over the /// cursor's own cell too in normal mode, where a cursor is a cell. diff --git a/src/tape.zig b/src/tape.zig index 70609858..3573d887 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -145,6 +145,11 @@ pub const Tape = struct { /// No digests recorded (a speculation's: a replica compares what it /// was given and what it asked, from a state the authority shares). no_digest: bool = false, + /// PARDES_REPLAY_DUMP (phase 2's gate): at the session's end, its dump + /// loaded must be it, digest for digest, and dump again byte for byte. + dump_check: bool = false, + dumps: usize = 0, + dump_bytes: usize = 0, fn record(t: *Tape, kind: Kind, comptime T: type, v: *const T) void { t.scratch.clearRetainingCapacity(); @@ -375,6 +380,34 @@ pub const Tape = struct { t.digests += 1; } + /// The session's dump loaded into a core of its own, which must be the + /// same session (its digest) and dump to the same bytes. The tape is + /// put aside meanwhile: a dump is no input. + fn dumpRoundTrip(t: *Tape, p: *Pardes) void { + on = null; + defer on = t; + const bytes = pardes.dump.dumpBytes(p) catch |err| return t.diverge("dump failed: {t}", .{err}); + defer t.gpa.free(bytes); + var notes: std.ArrayList(u8) = .empty; + defer notes.deinit(t.gpa); + const q = pardes.dump.load(t.gpa, p.opts, bytes, ¬es) catch |err| return t.diverge("dump load failed: {t}", .{err}); + defer q.deinit(); + if (notes.items.len > 0) return t.diverge("dump of this build had notes: {s}", .{notes.items}); + var want: Parts = undefined; + var got: Parts = undefined; + const a = digestParts(p, &want); + const b = digestParts(q, &got); + if (!std.mem.eql(u64, a, b)) return t.diverge("dump round trip: the loaded digest differs in {s}", .{partsDiffer(b, a, t.arena.allocator())}); + const again = pardes.dump.dumpBytes(q) catch |err| return t.diverge("dump of the loaded failed: {t}", .{err}); + defer t.gpa.free(again); + if (!std.mem.eql(u8, bytes, again)) { + const at = std.mem.indexOfDiff(u8, bytes, again) orelse 0; + return t.diverge("dump is no fixpoint: {d} bytes then {d}, first differing at {d}", .{ bytes.len, again.len, at }); + } + t.dumps += 1; + t.dump_bytes += bytes.len; + } + fn applySet(t: *Tape, r: *walk.Reader) void { const which = (r.take(1) catch return)[0]; if (which == set_mask) { @@ -438,7 +471,10 @@ pub const Tape = struct { // The session is over (or the core a Restore replaced is // freed): nothing done now is compared. .deinit => |own| { - if (own) t.core = null; + if (own) { + if (t.dump_check) if (t.core) |core| t.dumpRoundTrip(core); + t.core = null; + } return t.skip(); }, .update => |ev| p.update(ev), @@ -926,6 +962,7 @@ pub fn replayMain(io: std.Io, path: []const u8) u8 { var failed = false; for (runs) |run| { var t: Tape = .{ .gpa = gpa, .replay = true, .in = .{ .bytes = bytes }, .headless = run.headless, .scratch = .init(gpa), .arena = .init(gpa) }; + t.dump_check = std.mem.eql(u8, run.name, "first") and libc.getenv("PARDES_REPLAY_DUMP") != null; on = &t; defer on = null; while (t.diverged == null) { @@ -950,6 +987,7 @@ pub fn replayMain(io: std.Io, path: []const u8) u8 { failed = true; std.debug.print("replay {s}: {s} replica diverged after {d} digests: {s}\n", .{ path, run.name, t.digests, why }); } else std.debug.print("replay {s}: {s} replica: {d} digests agree\n", .{ path, run.name, t.digests }); + if (t.dump_check and t.diverged == null) std.debug.print("replay {s}: {d} dumps loaded to their digests and dumped to their bytes again ({d} bytes)\n", .{ path, t.dumps, t.dump_bytes }); } return if (failed) 1 else 0; } diff --git a/src/terminal.zig b/src/terminal.zig index 81fae5db..6fdd46f0 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -8,7 +8,6 @@ const Pardes = pardes.Pardes; const Surface = pardes.Surface; const modal = @import("modal.zig"); const tracy = @import("tracy.zig"); -const dump = @import("dump.zig"); const tagline = @import("tagline.zig"); const Key = pardes.Key; const Pane = panes.Pane; @@ -183,6 +182,121 @@ pub const State = struct { text.writer.flush() catch {}; h.* = text.hasher; } + + /// What the dump keeps of a terminal beside its emulator, by name as + /// every dump is (src/walk.zig). + const Parts = struct { + reply: []const u8 = "", + reply_dropped: u32 = 0, + mouse_cell: ?ghostty_vt.Coordinate = null, + petscii: bool = false, + /// The view's top row, counted from the top of the history. + view: usize = 0, + phase: @FieldType(MarkedHandler, "phase") = .none, + started: u32 = 0, + prompts: u32 = 0, + declined: u32 = 0, + continuation: bool = false, + lost: bool = false, + finished: @FieldType(MarkedHandler, "finished") = null, + out_start: ?ghostty_vt.Coordinate = null, + out_end: ?ghostty_vt.Coordinate = null, + }; + + fn pinAt(s: *const State, slot: ?*ghostty_vt.Pin) ?ghostty_vt.Coordinate { + const pin = slot orelse return null; + const screen = s.vt.screens.get(.primary) orelse return null; + return (screen.pages.pointFromPin(.screen, pin.*) orelse return null).screen; + } + + /// The dump's: ghostty's snapshot of the emulator (both screens, their + /// history, styles, links and kitty images), then `Parts`. A sequence + /// its program had begun and not ended is not kept: the snapshot is at + /// the ground state. + pub fn dumpWrite(s: *const State, gpa: std.mem.Allocator, w: *std.Io.Writer) !void { + if (comptime !enabled) return; + const h = &s.stream.handler; + const parts: Parts = .{ + .reply = s.reply[0..s.reply_len], + .reply_dropped = s.reply_dropped, + .mouse_cell = s.mouse_cell, + .petscii = if (comptime images_enabled) s.images.petscii else false, + .view = s.vt.screens.active.pages.scrollbar().offset, + .phase = h.phase, + .started = h.started, + .prompts = h.prompts, + .declined = h.declined, + .continuation = h.continuation, + .lost = h.lost, + .finished = h.finished, + .out_start = s.pinAt(h.out_start), + .out_end = s.pinAt(h.out_end), + }; + const bytes = try pardes.walk.save(Parts, gpa, &parts); + defer gpa.free(bytes); + try w.writeAll(std.mem.asBytes(&@as(u64, bytes.len))); + try w.writeAll(bytes); + try ghostty_vt.snapshot.encode(gpa, w, &s.vt, .{ .continuation = .ground }); + } + + /// What `dumpRead` made, freed: a load that failed after it. + pub fn dumpFree(s: *State, gpa: std.mem.Allocator) void { + if (comptime !enabled) return; + if (comptime images_enabled) s.images.deinit(gpa); + s.stream.deinit(); + s.vt.deinit(gpa); + } + + /// `dumpWrite`'s back into `out`, a new State, as `create` makes one. + pub fn dumpRead(out: *State, gpa: std.mem.Allocator, bytes: []const u8) !void { + if (comptime !enabled) { + out.* = .{ .vt = {}, .stream = {} }; + return; + } + if (bytes.len < 8) return error.EndOfDump; + const n = std.mem.bytesToValue(u64, bytes[0..8]); + if (bytes.len - 8 < n) return error.EndOfDump; + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const parts = try pardes.walk.load(Parts, arena.allocator(), bytes[8..][0..n], null); + var reader: std.Io.Reader = .fixed(bytes[8 + n ..]); + var decoded = try ghostty_vt.snapshot.decodeExact(gpa, terminalIo(), &reader, .{ + .max_continuation_bytes = 0, + .kitty_image_storage_limit = if (images_enabled) TerminalImages.storage_limit else 0, + }); + defer decoded.deinit(gpa); + out.* = .{ .vt = decoded.toOwned(), .stream = undefined }; + if (comptime images_enabled) inline for (.{ .primary, .alternate }) |key| { + if (out.vt.screens.get(key)) |screen| screen.kitty_images.image_limits = .allWithTempDir(tempDir()); + }; + out.stream = .init(.{ .handler = .{ .inner = out.vt.vtHandler() }, .allocator = gpa }); + out.stream.handler.inner.effects.write_pty = ptyReport; + out.stream.handler.inner.effects.device_attributes = ptyDeviceAttrs; + out.stream.handler.inner.effects.size = ptySize; + const reply = parts.reply[0..@min(parts.reply.len, out.reply.len)]; + @memcpy(out.reply[0..reply.len], reply); + out.reply_len = @intCast(reply.len); + out.reply_dropped = parts.reply_dropped; + out.mouse_cell = parts.mouse_cell; + out.images = if (images_enabled) .{ .petscii = parts.petscii } else {}; + if (comptime images_enabled) TerminalImages.installPngDecoder(); + const h = &out.stream.handler; + h.phase = parts.phase; + h.started = parts.started; + h.prompts = parts.prompts; + h.declined = parts.declined; + h.continuation = parts.continuation; + h.lost = parts.lost; + h.finished = parts.finished; + if (out.vt.screens.get(.primary)) |screen| { + inline for (.{ .{ "out_start", parts.out_start }, .{ "out_end", parts.out_end } }) |pair| if (pair[1]) |at| { + if (screen.pages.pin(.{ .screen = at })) |pin| @field(h, pair[0]) = screen.pages.trackPin(pin) catch null; + }; + } + const screen = out.vt.screens.active; + const bottom = screen.pages.scrollbar().offset; + screen.scroll(.{ .delta_row = @as(isize, @intCast(parts.view)) - @as(isize, @intCast(bottom)) }); + } }; const GColor = ghostty_vt.color; @@ -714,42 +828,25 @@ pub fn createDoc(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { /// Where a restored terminal's history ends and its new shell begins. pub const restored_banner = "\x1b[0m\r\n\x1b[2m\u{2500}\u{2500} restored history \u{2500}\u{2500}\x1b[0m\r\n"; -/// Rebuild a dump's terminal emulator from its recorded output. `live`: a -/// shell will be spawned into it, so the history is marked off from what -/// that shell prints and the pane waits for it like any new terminal. -/// Registration and tag/cwd policy stay with the core; raw VT replay and -/// viewport restoration belong here. -pub fn restore(p: *Pardes, src: dump.Pane, live: bool) !*Pane { - const terminal = src.terminal.?; - if (comptime !enabled) { - const pane = try create(p.gpa, @max(1, src.cols), @max(1, src.rows)); - errdefer p.gpa.destroy(pane); - if (terminal.stream.len > 0) - pane.ovl = .{ .row = 0, .rows = 1, .text = try p.gpa.dupe(u8, terminal.stream) }; - return pane; - } - const bytes = if (terminal.stream_b64.len > 0) - try dump.decodeBytes(p.scratch.allocator(), terminal.stream_b64) - else - &.{}; - const pane = try create(p.gpa, @max(1, src.cols), @max(1, src.rows)); - if (live) { - if (bytes.len > 0) { - ingest(pane, bytes); - ingest(pane, restored_banner); - } - followOutput(pane); - // A view left scrolled back stays on what it was showing: the - // banner's line breaks scrolled a full screen by that many more. - if (src.scroll > 0) scrollGrid(pane, -@as(i32, @intCast(src.scroll + comptime std.mem.count(u8, restored_banner, "\n")))); - armShellSpawn(pane); - } else if (bytes.len > 0) { - ingest(pane, bytes); - followOutput(pane); - if (src.scroll > 0) - scrollGrid(pane, -@as(i32, @intCast(src.scroll))); - } - return pane; +/// A terminal loaded from a dump, its shell not there, made ready for a new +/// one: its program's screen modes reset (the old program is gone), its +/// history marked off from what the new shell prints, and the pane waiting +/// for it like any new terminal (a line queued for the old one goes to it). +pub fn reviveShell(pane: *Pane) void { + if (comptime !enabled) return; + if (pane.terminal == null) return; + // The primary screen, modes and mouse as a shell starts with them. + ingest(pane, "\x1b[?1049l\x1b[!p\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?2004l\x1b[