diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 02:44:25 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 0a75f3f333e795e741c83c1193e3b833d40caf46 (patch) | |
| tree | ce627350732398e3dc071495742693c271855f86 /src/Plumb.zig | |
| parent | 757cdaa826131dc5ff2d3ff0b270b8abe041c81a (diff) | |
| download | pardes-0a75f3f333e795e741c83c1193e3b833d40caf46.tar.gz pardes-0a75f3f333e795e741c83c1193e3b833d40caf46.zip | |
plumber: the pager sends (docs/next-plumber.md, phase 4): a +Pager filled with a diff (File.looksLikeDiff, pager_diff) is a plumbing message, as acme's `plumb -i` sends what it shows: fillPager's last call is Plumb.paged, which sends src pager, dst edit, wdir the +Pager's directory, type text/x-diff, `action=showdata filename=<the +Pager's name>` (quoted as mesg.c quotes), the text as data; the default rules' last set makes it showdiff (attr delete action, attr add action=showdiff, plumb to edit), and the edit port's showdata and showdiff (Plumb.show, acme's plumbshow) show the data in the pane filename names, the sender's own first, else a new clean pane of that name, showdiff then restacking it in a +Diff in its place (hunks.paged, the code the pager's hook called, unchanged); text paged that is no diff is not sent, where the record had every page sent: the look rules would read it whole, `type is text` holding (a `matches` over a MiB of output, up to the regular expressions' 300 ms budget, at each page), and a page of one line naming a file would ask the disk; the rules in force are read once and kept until their text changes (Plumb.parsed, by its hash, derived state: each process's own), measured in a fast build per send: 29k instructions to read the default rules, a URL's send 37k before, 8.3k after, a word's look 69k before, 40k after, a look that opens a file 262k before (its send 63k), 233k after (34k); test: a diff paged is stacked by the default rule, text that is no diff stays a +Pager, without the rule the +Pager stays as text, and another sender's showdata and showdiff; the design record gains what phases 2-4 did otherwise (detached starts with no output kept, only diffs paged sent, own ports asking nobody, the rules read once); plumber.zig's defaults end with the paged rule; the reference, the default rules' file and the changelog say so
Diffstat (limited to 'src/Plumb.zig')
| -rw-r--r-- | src/Plumb.zig | 130 |
1 files changed, 129 insertions, 1 deletions
diff --git a/src/Plumb.zig b/src/Plumb.zig index e2edef77..5299a8c3 100644 --- a/src/Plumb.zig +++ b/src/Plumb.zig @@ -29,6 +29,13 @@ written: bool = false, /// named, never dropped (plumber(4)'s directory): plumb/<port>'s files, /// a port's index its node. ports: std.ArrayList([]u8) = .empty, +/// The rules in force read, kept until their text changes (by its hash): +/// a send parses nothing (29k instructions a send in a fast build, half +/// of what a URL's costs). Each process's own. +parsed: ?struct { hash: u64, rules: Rules } = null, +parsed_arena: ?std.heap.ArenaAllocator = null, + +pub const replica = .{ .derived = .{ "parsed", "parsed_arena" } }; /// The default rules, which `include basic` names, as Plan 9's names /// /lib/plumb/basic. @@ -50,6 +57,7 @@ pub const e_client = "no port for \"client\" rule"; pub const e_empty = "empty argument list"; pub fn deinit(pl: *Plumb, gpa: std.mem.Allocator) void { + if (pl.parsed_arena) |*a| a.deinit(); pl.text.deinit(gpa); for (pl.ports.items) |name| gpa.free(name); pl.ports.deinit(gpa); @@ -503,6 +511,22 @@ fn rulesOf(p: *Pardes, arena: std.mem.Allocator) Parser.Error!Rules { return ps.rules; } +/// The rules in force, read once for every send until they change. A +/// send made while a rule's word runs (a look it makes) reads them into +/// the step's scratch: the send it runs in holds the kept ones. +fn inForceRules(p: *Pardes) Parser.Error!Rules { + const pl = &p.plumber; + const hash = std.hash.Wyhash.hash(0, source(p)); + if (pl.parsed) |c| if (c.hash == hash) return c.rules; + if (p.plumb_declined != null) return rulesOf(p, p.scratch.allocator()); + pl.parsed = null; + if (pl.parsed_arena) |*a| a.deinit(); + pl.parsed_arena = .init(p.gpa); + const rules = try rulesOf(p, pl.parsed_arena.?.allocator()); + pl.parsed = .{ .hash = hash, .rules = rules }; + return rules; +} + /// The rules in force as plumb/rules reads them, the defaults printed /// (in the step's scratch) while none were written. pub fn inForce(p: *Pardes) []const u8 { @@ -839,7 +863,7 @@ pub const e_declined = "no matching plumb rule"; /// the step's scratch. pub fn send(p: *Pardes, from: ?usize, msg: Msg, seen: ?Seen) Sent { const arena = p.scratch.allocator(); - var s: Send = .{ .p = p, .from = from, .arena = arena, .rules = rulesOf(p, arena) catch return .{ .refused = "plumb/rules: the rules in force do not read" } }; + var s: Send = .{ .p = p, .from = from, .arena = arena, .rules = inForceRules(p) catch return .{ .refused = "plumb/rules: the rules in force do not read" } }; if (seen) |look_seen| { s.looked = msg.data; s.memo_name = look_seen.name; @@ -960,6 +984,13 @@ fn deliver(s: *Send, id: usize, port: Port, m: *const Msg) Sent { if (m.data.len > 256) p.reportFailure(id, "plumb: web: a URL is at most 256 bytes") else p.emit(.{ .open_link = .from(m.data) }); }, .edit => { + // acme's plumbshow, for `plumb -i`: the data shown, not a name. + var action_buf: [4096]u8 = undefined; + const action = attrValue(m.attr, "action", &action_buf) orelse ""; + if (std.mem.eql(u8, action, "showdata") or std.mem.eql(u8, action, "showdiff")) { + show(s, id, m, std.mem.eql(u8, action, "showdiff")); + return .done; + } var buf: [4096]u8 = undefined; const addr = attrValue(m.attr, "addr", &buf) orelse ""; // A look's own name, as it sent it: its own look opens it. @@ -983,6 +1014,57 @@ fn deliver(s: *Send, id: usize, port: Port, m: *const Msg) Sent { return .done; } +/// The edit port's `showdata` and `showdiff` (acme's plumbshow): the data +/// in the pane `filename` names, the pane that sent it first (a +Pager's +/// own text), else a new one so named (`+Plumbed` with none), clean; +/// `showdiff` then stacks it in a +Diff in that pane's place (hunks.paged), +/// as a diff paged always was. +fn show(s: *Send, id: usize, m: *const Msg, diff: bool) void { + const p = s.p; + var buf: [4096]u8 = undefined; + const filename = attrValue(m.attr, "filename", &buf) orelse ""; + const named = std.mem.eql(u8, filename, if (p.panes[id]) |pn| (if (pn.file) |f| f.path else "") else ""); + const shown: usize = if (named) id else shown: { + const name = if (filename.len > 0) filename else "+Plumbed"; + const free = p.freeSlot() orelse return p.reportError(id, "plumb", error.NoPaneSlots); + pardes.panes.newScratchFor(p, id, .scratch); + const made = p.panes[free] orelse return; + @import("ninep/pane.zig").nameBuffer(p, free, name, false) catch |err| return p.reportError(id, "plumb", err); + const f = &made.file.?; + _ = pardes.panes.File.replace(p, f, 0, f.content.len, m.data); + f.saved_revision = f.revision; + break :shown free; + }; + if (diff) @import("hunks.zig").paged(p, shown); +} + +/// A +Pager filled (ninep/ctl.zig fillPager) with a diff (File.looksLikeDiff: +/// `git show`, a mailed patch): sent to `edit`, as acme's `plumb -i` +/// sends what it shows, `type text/x-diff`, `action=showdata` and the +/// +Pager's name; the default rules make it `showdiff`, stacked in a +Diff +/// in the +Pager's place. Other text paged is not sent: the look rules +/// would read it whole (a `matches` over a MiB of output, up to the +/// regular expressions' budget, at each page). +pub fn paged(p: *Pardes, id: usize) void { + const pane = p.panes[id] orelse return; + const f = pane.file orelse return; + if (!f.pager_diff) return; + var attr: std.Io.Writer.Allocating = .init(p.scratch.allocator()); + attr.writer.writeAll("action=showdata filename=") catch return; + // mesg.c's quote, for a name with a blank, a quote, `=` or a tab. + const quote = std.mem.indexOfAny(u8, f.path, " '=\t") != null; + if (quote) attr.writer.writeByte('\'') catch return; + for (f.path) |c| { + if (c == '\'') attr.writer.writeByte('\'') catch return; + attr.writer.writeByte(c) catch return; + } + if (quote) attr.writer.writeByte('\'') catch return; + switch (send(p, id, .{ .src = "pager", .dst = "edit", .wdir = Pardes.paneDir(pane), .type = "text/x-diff", .attr = attr.written(), .data = f.content }, null)) { + .done, .look => {}, + .refused => |why| p.reportFailure(id, why), + } +} + /// A message to a loaded kind's port: its data, a file, opened in that kind /// (or its pane given the keyboard, when one shows it). fn openInKind(s: *Send, id: usize, k: u8, m: *const Msg) void { @@ -1436,3 +1518,49 @@ test "plumb: the Plumb word opens the plumbing file, include basic until one is defer q.deinit(); try testing.expectEqualStrings("type\tis\ttext\ndata\tmatches\t'y.*'\nplumb\tto\tweb\n\n", inForce(q)); } + +test "plumb: a diff paged is sent to edit, stacked in a +Diff by the default rule; without it the +Pager stays, and a reader of plumb/edit gets it" { + const p = try Pardes.init(testing.allocator, .{ .tty_only = true }); + defer p.deinit(); + const page = "commit 0123456789abcdef0123456789abcdef01234567\ndiff --git a/x b/x\n--- a/x\n+++ b/x\n@@ -1 +1 @@\n-old\n+new\n"; + const Page = struct { + fn fill(pp: *Pardes, text: []const u8) !usize { + const id = try pardes.panes.Output.openPager(pp, 0, "/pardes-plumb-page"); + var held: std.ArrayList(u8) = .empty; + defer held.deinit(pp.gpa); + try held.appendSlice(pp.gpa, text); + try @import("ninep/ctl.zig").fillPager(pp, pp.panes[id].?.serial, &held); + return id; + } + fn diffs(pp: *Pardes) usize { + var n: usize = 0; + for (pp.panes) |slot| if (slot) |pn| if (pn.file) |f| { + n += @intFromBool(std.mem.indexOf(u8, f.path, "/+Diff") != null); + }; + return n; + } + }; + // The defaults: the +Pager stacked into a +Diff, and gone. + const stacked = try Page.fill(p, page); + try testing.expect(p.panes[stacked] == null or !std.mem.endsWith(u8, p.panes[stacked].?.file.?.path, "+Pager")); + try testing.expectEqual(@as(usize, 1), Page.diffs(p)); + // Text that is no diff is not sent: its +Pager stays, as ever. + const plain = try Page.fill(p, "just text\n"); + try testing.expect(std.mem.endsWith(u8, p.panes[plain].?.file.?.path, "+Pager")); + // The paged-diff rule taken out: the edit port shows the data where + // it is, and the +Pager stays as text. + try testing.expect(apply(p, false, "plumb/rules", "type is text\narg isfile $data\nplumb to edit\n") == null); + const kept = try Page.fill(p, page); + try testing.expect(std.mem.endsWith(u8, p.panes[kept].?.file.?.path, "+Pager")); + try testing.expectEqualStrings(page, p.panes[kept].?.file.?.content); + try testing.expectEqual(@as(usize, 1), Page.diffs(p)); + // From another sender: shown in a pane of its filename's, clean, and + // with showdiff stacked. + try testing.expect(send(p, 0, .{ .src = "test", .dst = "edit", .type = "text", .attr = "action=showdata filename=/elsewhere/notes", .data = "a note\n" }, null) == .done); + const shown = p.panes[p.view().active].?; + try testing.expectEqualStrings("/elsewhere/notes", shown.file.?.path); + try testing.expectEqualStrings("a note\n", shown.file.?.content); + try testing.expectEqual(shown.file.?.revision, shown.file.?.saved_revision); + try testing.expect(send(p, 0, .{ .src = "test", .dst = "edit", .type = "text/x-diff", .attr = "action=showdiff filename=/elsewhere/change", .data = page }, null) == .done); + try testing.expectEqual(@as(usize, 2), Page.diffs(p)); +} |
