From c37a700474e8b84cc3c7db8bf550b7c5f18b1871 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 13:15:15 -0300 Subject: kinds #23, the plumber: Plumb, skqrvwks's rules ported (one a line in plumb(6)'s order, the session's and kept in a dump, tried before the built-in ladder, prefix, suffix, isdir and isfile), its ports url, dir, file or any loaded kind by its name from the registry, a rule naming a kind not loaded refused in words when written and passed over like any rule that cannot make its target when its kind has gone since, so `Plumb suffix .svg to image` in init sends a look to the image addon Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/look.zig | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) (limited to 'src/look.zig') diff --git a/src/look.zig b/src/look.zig index 7ac8798b..52674ecf 100644 --- a/src/look.zig +++ b/src/look.zig @@ -304,6 +304,56 @@ test "lookableLineSpan prefers the longest run, so a blank inside a path is one ); } +/// Where a `Plumb` rule sends what it matched: the targets the built-in +/// rules below make (url: the browser, through `xdg-open`), or a loaded +/// kind by its name (`image`, `pdf`). +pub const Port = enum { url, dir, file }; + +/// A rule as `Plumb` takes it, plumb(6)'s order on one line: ` +/// [word ...] to `, the test `prefix` (the text starts so), `suffix` +/// (an existing file's name ends so, any case), `isdir` or `isfile`; the +/// port `url`, `dir`, `file` or a kind `kinds` has. +/// Null when it is one; otherwise what is wrong with it, said. +pub fn plumbRuleError(kinds: *const Kind.Registry, rule: []const u8) ?[]const u8 { + var words = std.mem.tokenizeAny(u8, rule, " \t"); + const how = words.next() orelse return "a rule: [word ...] to "; + const takes = if (std.mem.eql(u8, how, "prefix") or std.mem.eql(u8, how, "suffix")) + true + else if (std.mem.eql(u8, how, "isdir") or std.mem.eql(u8, how, "isfile")) + false + else + return "invalid test: prefix, suffix, isdir or isfile"; + var n: usize = 0; + while (words.next()) |w| : (n += 1) { + if (!std.mem.eql(u8, w, "to")) continue; + if (takes and n == 0) return "invalid rule: prefix and suffix take a word"; + if (!takes and n > 0) return "invalid rule: isdir and isfile take no word"; + const port = words.next() orelse return "invalid rule: to names no port"; + if (std.meta.stringToEnum(Port, port) == null and kinds.byName(port) == null) + return "invalid port: url, dir, file or a loaded kind"; + if (words.next() != null) return "invalid rule: a word after the port"; + return null; + } + return "invalid rule: no `to `"; +} + +/// What a rule's port makes of what it matched; null when it cannot (no +/// such file, a directory to `file`, a kind gone since): the next rule tries. +fn plumbTarget(p: *pardes.Pardes, port: []const u8, text: []const u8, found: ?filesystem.Resolved, at: Spot) ?Target { + const to = std.meta.stringToEnum(Port, port); + if (to == .url) return .{ .url = if (found) |f| f.path else text }; + const f = found orelse return null; + if (to == null) { + const k = p.kinds.byName(port) orelse return null; + return if (f.dir) null else .{ .kind = .{ .path = f.path, .kind = k, .at = at } }; + } + return switch (to.?) { + .url => unreachable, + .dir => if (f.dir) .{ .dir = f.path } else null, + .file => if (f.dir) null else .{ .file = .{ .path = f.path, .at = at } }, + }; +} + pub fn resolve(p: ?*pardes.Pardes, word_raw: []const u8, cwd: []const u8, realbuf: *[4096]u8) Target { const trimmed = std.mem.trim(u8, word_raw, " \t\r\n"); const pl = parsePathLine(trimmed); @@ -326,6 +376,42 @@ pub fn resolve(p: ?*pardes.Pardes, word_raw: []const u8, cwd: []const u8, realbu } } + if (p) |pp| if (pp.plumb.items.len > 0) { + var found: ?filesystem.Resolved = null; + var looked = false; + for (pp.plumb.items) |rule| { + var words = std.mem.tokenizeAny(u8, rule, " \t"); + const how = words.next() orelse continue; + // The file is asked about once, and only by a rule that needs it. + const prefix = std.mem.eql(u8, how, "prefix"); + if (!looked and !prefix) { + found = filesystem.resolve(p, word, cwd, realbuf); + looked = true; + } + const file = if (found) |f| !f.dir else false; + var hit = std.mem.eql(u8, how, "isfile") and file or + std.mem.eql(u8, how, "isdir") and found != null and !file; + var port: []const u8 = ""; + while (words.next()) |w| { + if (std.mem.eql(u8, w, "to")) { + port = words.next() orelse ""; + break; + } + if (prefix and std.mem.startsWith(u8, trimmed, w)) hit = true; + // A suffix is a file's: plumb's basic pairs each with `arg isfile`. + if (std.mem.eql(u8, how, "suffix") and file and std.ascii.endsWithIgnoreCase(found.?.path, w)) hit = true; + } + if (!hit) continue; + const url = std.mem.eql(u8, port, "url"); + if (!looked and !url) { + found = filesystem.resolve(p, word, cwd, realbuf); + looked = true; + } + // A prefix rule's url is the text, never a file it also names. + if (plumbTarget(pp, port, trimmed, if (prefix and url) null else found, pl.at)) |t| return t; + } + }; + for (config.url_schemes) |scheme| { if (std.mem.startsWith(u8, trimmed, scheme)) return .{ .url = trimmed }; } @@ -2579,3 +2665,52 @@ test "Look on a diff line is the look of the address it names, selected by hand: /// The rows n/N's walk over look-able text reads before it stops (look.zig). pub const max_look_rows = 8192; + +test "Plumb: the user's rules come before the built-in ones, the first that matches wins, a bad rule is refused, a dump keeps them" { + if (!platform_has_fs) return; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic.svg", .data = "" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "shot.PNG", .data = "x" }); + var dir_buf: [4096]u8 = undefined; + const root = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + const p = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 30 }); + defer p.deinit(); + // A kind as a library brings it, its name and extension (no code: a + // look only names it). + try std.testing.expect(p.kinds.take(.{ .name = "image", .exts = ".png" }, "") == null); + var real: [4096]u8 = undefined; + // None by default: today's ladder. + try std.testing.expect(resolve(p, "pic.svg", root, &real) == .file); + try std.testing.expect(resolve(p, "shot.PNG", root, &real) == .kind); + try std.testing.expect(resolve(p, "https://ziglang.org", root, &real) == .url); + try std.testing.expect(p.executeBuiltinLine(0, "Plumb suffix .svg .SVGZ to image")); + try std.testing.expect(p.executeBuiltinLine(0, "Plumb suffix .png to file")); + try std.testing.expect(p.executeBuiltinLine(0, "Plumb prefix https:// to file")); + try std.testing.expect(p.executeBuiltinLine(0, "Plumb prefix gopher:// to url")); + try std.testing.expect(resolve(p, "pic.svg", root, &real) == .kind); + // Any case, as the kinds' extensions are. + try std.testing.expect(resolve(p, "shot.PNG", root, &real) == .file); + // A rule that cannot make its target (no such file) lets the next try. + try std.testing.expect(resolve(p, "https://ziglang.org", root, &real) == .url); + try std.testing.expectEqualStrings("gopher://x", resolve(p, "gopher://x", root, &real).url); + // A suffix is a file's: a word naming no file matches none. + try std.testing.expect(resolve(p, "nothere.svg", root, &real) == .none); + // Refused, in words, the list as it was: a kind not loaded too. + for ([_][]const u8{ "Plumb matches x to image", "Plumb suffix to image", "Plumb isdir .x to dir", "Plumb suffix .x to nowhere", "Plumb suffix .x to pdf", "Plumb suffix .x", "Plumb suffix .x to image extra" }) |bad| + try std.testing.expect(p.executeBuiltinLine(0, bad)); + try std.testing.expectEqual(@as(usize, 4), p.plumb.items.len); + try std.testing.expectEqualStrings("suffix .svg .SVGZ to image", p.plumb.items[0]); + try std.testing.expect(p.executeBuiltinLine(0, "Plumb")); + const pane = p.panes[0].?; + try std.testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "; suffix .png to file") != null); + try pardes.dump.dumpState(p); + const restored = try pardes.dump.restore(p, p.dump_out.?, "/tmp/pardes.dump.zon"); + defer restored.deinit(); + try std.testing.expectEqual(@as(usize, 4), restored.plumb.items.len); + // The kind gone, its rule is tried like any that cannot: the ladder's. + p.kinds.drop(p.kinds.byName("image").?); + try std.testing.expect(resolve(p, "pic.svg", root, &real) == .file); + try std.testing.expect(p.executeBuiltinLine(0, "Plumb -")); + try std.testing.expectEqual(@as(usize, 0), p.plumb.items.len); +} -- cgit v1.3