summaryrefslogtreecommitdiff
path: root/src/look.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/look.zig')
-rw-r--r--src/look.zig393
1 files changed, 185 insertions, 208 deletions
diff --git a/src/look.zig b/src/look.zig
index 082565ed..136a7a9c 100644
--- a/src/look.zig
+++ b/src/look.zig
@@ -4,6 +4,7 @@ const builtin = @import("builtin");
const libc = std.c;
const pardes = @import("pardes.zig");
const exec = @import("exec.zig");
+const Plumb = @import("Plumb.zig");
const config = @import("config.zig");
const diff = @import("diff.zig");
const platform_has_fs = filesystem.platform_has_fs;
@@ -152,56 +153,6 @@ pub fn lineSpan(p: ?*pardes.Pardes, line: []const u8, cwd: []const u8, realbuf:
return null;
}
-/// 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: `<test>
-/// [word ...] to <port>`, 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: <test> [word ...] to <port>";
- 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 <port>`";
-}
-
-/// 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);
@@ -224,42 +175,6 @@ 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 };
}
@@ -872,25 +787,59 @@ pub fn clearNavigationSelection(p: *Pardes, pane: *Pane) void {
if (v.drag == .select and p.panes[v.drag.select.id] == pane) v.drag = .none;
}
+/// The directories a relative name is looked for in, in order, as acme's
+/// look tries its window's and then the other windows' (Incl's come
+/// after, the caller's, as includename's): `first` (the pane's own, a
+/// plumb message's wdir), then the directory of each pane of the jump list
+/// but `from`, newest first, each once.
+pub const Dirs = struct {
+ p: *Pardes,
+ from: ?usize,
+ seen: [MAX_PANES + 1][]const u8 = undefined,
+ nseen: usize = 0,
+ /// The next of the jump list's, counted from its newest.
+ n: usize = 1,
+
+ pub fn init(p: *Pardes, from: ?usize, first: ?[]const u8) Dirs {
+ var d: Dirs = .{ .p = p, .from = from };
+ if (first) |dir| {
+ d.seen[0] = dir;
+ d.nseen = 1;
+ d.n = 0;
+ }
+ return d;
+ }
+
+ pub fn next(d: *Dirs) ?[]const u8 {
+ if (d.n == 0) {
+ d.n = 1;
+ return d.seen[0];
+ }
+ const id = d.from orelse return null;
+ const jumps = &d.p.view().jumps;
+ cand: while (d.n <= jumps.n) {
+ const k = jumps.list[jumps.n - d.n].pane;
+ d.n += 1;
+ if (k == id) continue;
+ const dir = Pardes.paneDir(d.p.panes[k] orelse continue);
+ for (d.seen[0..d.nseen]) |s| if (std.mem.eql(u8, s, dir)) continue :cand;
+ d.seen[d.nseen] = dir;
+ d.nseen += 1;
+ return dir;
+ }
+ return null;
+ }
+};
+
/// Resolve without changing focus or falling back to search. PDF links use
/// the same location rules as an ordinary Look before offering a choice.
fn resolveLookTarget(p: *Pardes, id: usize, txt: []const u8, realbuf: *[4096]u8) Target {
- const v = p.view();
const trimmed = std.mem.trim(u8, txt, " \t\r\n");
const pl = parsePathLine(trimmed);
if (pl.path.len > 0) if (openPaneTarget(p, pl.path, pl.at)) |target| return target;
var found: Target = .none;
- var seen: [MAX_PANES][]const u8 = undefined;
- var nseen: usize = 0;
- var n: usize = 0;
- cand: while (n <= v.jumps.n) : (n += 1) {
- const k: usize = if (n == 0) id else v.jumps.list[v.jumps.n - n].pane;
- if (n > 0 and k == id) continue; // tried first, skip the rerun
- const op = p.panes[k] orelse continue;
- const dir = Pardes.paneDir(op);
- for (seen[0..nseen]) |s| if (std.mem.eql(u8, s, dir)) continue :cand;
- seen[nseen] = dir;
- nseen += 1;
+ var dirs: Dirs = .init(p, id, if (p.panes[id]) |op| Pardes.paneDir(op) else null);
+ while (dirs.next()) |dir| {
if (pl.path.len > 0 and pl.path[0] != '/') {
var joinbuf: [2048]u8 = undefined;
if (std.fmt.bufPrint(&joinbuf, "{s}/{s}", .{ dir, pl.path }) catch null) |j|
@@ -1089,7 +1038,6 @@ fn lookUnresolved(p: *Pardes, id: usize, pane: *Pane, trimmed: []const u8, text:
// 9P is no keyboard input, and searches whatever the key mode.
if (pane.addon != null or (pane.body.mode == .tty and !p.fs.serving)) return;
if (text.len == 0) return;
- if (lookCommit(p, id, pane, trimmed)) return;
if (pane.file != null) return lookWord(p, id, pane, text);
p.word_look = true;
defer p.word_look = false;
@@ -1110,38 +1058,6 @@ fn lookUnresolved(p: *Pardes, id: usize, pane: *Pane, trimmed: []const u8, text:
}
}
-/// A commit hash (7 to 40 hex digits, a letter and a digit among them)
-/// looked at inside a git repository: `git show` it, paged into a +Pager,
-/// which stacks its hunks in a +Diff (ctl.fillPager); the command pane
-/// goes when it succeeds (Pane.commandDone).
-// ponytail: a hex word that names no commit leaves git's error in a command pane, not a search; check the object first if that bites.
-fn lookCommit(p: *Pardes, id: usize, pane: *Pane, word: []const u8) bool {
- if (!isCommitHash(word)) return false;
- var dir = filesystem.localPath(Pardes.paneDir(pane)) orelse return false;
- while (true) {
- var buf: [4096]u8 = undefined;
- const dot_git = std.fmt.bufPrint(&buf, "{s}/.git", .{dir}) catch return false;
- if (exec.kindOf(dot_git) != null) break;
- dir = std.Io.Dir.path.dirname(dir) orelse return false;
- }
- var line: [64]u8 = undefined;
- const dst = exec.execute(p, id, std.fmt.bufPrint(&line, "git show {s}", .{word}) catch return false) orelse return true;
- if (p.panes[dst]) |c| c.command_pages = true;
- return true;
-}
-
-fn isCommitHash(word: []const u8) bool {
- if (word.len < 7 or word.len > 40) return false;
- var digit = false;
- var letter = false;
- for (word) |c| switch (c) {
- '0'...'9' => digit = true,
- 'a'...'f' => letter = true,
- else => return false,
- };
- return digit and letter;
-}
-
/// Opens (or finds) `path` as Look would, `id`'s own pane when it is empty,
/// and selects what `expr` addresses there from its dot. An address that
/// does not evaluate moves nothing and says why (acme: no jump).
@@ -1369,7 +1285,7 @@ pub fn isHistoryList(pane: *const Pane) bool {
// ---- a look on a diff line ----
/// The lines of a pane showing a diff, a .diff or .patch file or a
-/// finished command pane whose output is one (File.DiffOutput); null for
+/// finished command pane whose output is one (ansi.DiffOutput); null for
/// any other pane. Good until the next step.
pub fn diffLines(p: *Pardes, pane: *Pane) ?[]const []const u8 {
if (pane.file) |*f| {
@@ -1390,18 +1306,32 @@ pub fn diffAddress(pane: *const Pane, lines: []const []const u8, row: usize, buf
return target.address(buf);
}
-/// What a click at byte `col` of line `row` of a diff pane takes, the one
-/// answer for every way a click arrives (a mouse click, a 9P look of the
-/// whole line, an empty look or exec written back to `event`): a file or
-/// hunk header anywhere, or a hunk line on its `+`/`-`/` ` prefix, is the
-/// address the line names (diffAddress); a hunk line's code is words, from
-/// `word_from` on, the prefix never one of them. Null when the pane shows
-/// no diff or the line is no part of one.
-pub const DiffClick = struct { address: ?[]const u8 = null, word_from: usize = 0 };
+/// What a click at byte `col` of line `row` of a diff pane takes: a file
+/// or hunk header anywhere, or a hunk line on its `+`/`-`/` ` prefix, is
+/// the address the line names (diffAddress); a hunk line's code is words,
+/// from `word_from` on, the prefix never one of them. Null when the line is
+/// no part of a diff.
+const DiffClick = struct { address: ?[]const u8 = null, word_from: usize = 0 };
-pub fn diffClick(p: *Pardes, pane: *Pane, row: usize, col: usize, buf: []u8) ?DiffClick {
+/// What a click at byte `col` of row `row` takes, the one answer for every
+/// way a click arrives (the mouse, exec.operandAt; an empty look written
+/// back to `event`, events.expandAt; a whole line written to `look`): the
+/// address a row's producer gave it (a query pane's row on its address,
+/// Query.rowAddress), else the one the pane's language reads in the line (a
+/// diff's: diffClickIn); or where its words start, the address no part of
+/// them. `line` is the row's text it was read from, and the address's text
+/// in it ends at `end`. Null for a row that names no address.
+pub const RowAddress = struct { address: ?[]const u8 = null, word_from: usize = 0, line: []const u8, end: usize };
+
+pub fn rowAddressAt(p: *Pardes, pane: *Pane, row: usize, col: usize, buf: []u8) ?RowAddress {
+ if (panes.Query.rowAddress(pane, row, col, buf)) |address| {
+ const f = &pane.file.?;
+ return .{ .address = address, .line = modal.lineSlice(f.content, row), .end = f.location_rows[row].location_end };
+ }
const lines = diffLines(p, pane) orelse return null;
- return diffClickIn(pane, lines, row, col, buf);
+ if (row >= lines.len) return null;
+ const click = diffClickIn(pane, lines, row, col, buf) orelse return null;
+ return .{ .address = click.address, .word_from = click.word_from, .line = lines[row], .end = lines[row].len };
}
fn diffClickIn(pane: *const Pane, lines: []const []const u8, row: usize, col: usize, buf: []u8) ?DiffClick {
@@ -1417,18 +1347,19 @@ fn diffClickIn(pane: *const Pane, lines: []const []const u8, row: usize, col: us
return .{ .address = address };
}
-/// A look written over 9P of a whole line of a diff pane (the cursor's line
-/// first, then the first after it): what a click on its first column takes.
+/// A look written over 9P of a whole line of a pane whose rows name
+/// addresses (a diff's, a query pane's: rowAddressAt), the cursor's line
+/// first, then the first after it: what a click on its first column takes.
/// A blank context line (` `) is a line too.
-fn diffLineAddress(p: *Pardes, pane: *Pane, operand: []const u8, buf: []u8) ?[]const u8 {
+fn lineAddress(p: *Pardes, pane: *Pane, operand: []const u8, buf: []u8) ?[]const u8 {
const text = std.mem.trimEnd(u8, operand, "\r\n");
if (text.len == 0) return null;
- const lines = diffLines(p, pane) orelse return null;
+ const lines = (if (pane.query != null) panes.File.cursorLines(p.scratch.allocator(), pane, &pane.file.?) catch null else diffLines(p, pane)) orelse return null;
const cur: usize = @intCast(@max(0, pane.body.cur_row));
for (0..lines.len) |k| {
const i = (cur + k) % lines.len;
if (std.mem.eql(u8, std.mem.trimEnd(u8, lines[i], "\r"), text))
- return (diffClickIn(pane, lines, i, 0, buf) orelse return null).address;
+ return (rowAddressAt(p, pane, i, 0, buf) orelse return null).address;
}
return null;
}
@@ -1524,7 +1455,7 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void {
// A whole line of a diff written over 9P (a context line ` Del`
// included) is a look at the address it names, never the word run.
var diff_buf: [4096 + 32]u8 = undefined;
- const looked: []const u8 = diffLineAddress(p, pane, operand_raw, &diff_buf) orelse looked: {
+ const looked: []const u8 = lineAddress(p, pane, operand_raw, &diff_buf) orelse looked: {
if (config.commandWord(command)) |cmd| {
if (exec.execute(p, id, cmd)) |dst| v.active = dst;
return;
@@ -1574,6 +1505,36 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void {
for (0..pane.sel.len) |slot| pane.clearPointerSelection(slot);
pane.sel = @splat(.{});
}
+ // The plumber, asked before Look's own look as acme asks it (look.c's
+ // look3): what is looked at, sent from this pane. A name already open
+ // in a pane is focused first, never sent; nor is a look that opens
+ // as text (look_text), or the edit port's open of what was. A message
+ // the rules give back to `edit` as sent is Look's own to open, on what
+ // it found; one nothing takes, Look's too.
+ const shown = switch (found) {
+ .pane => true,
+ .file => |f| openPaneTarget(p, f.path, f.at) != null,
+ .kind => |k| openPaneTarget(p, k.path, .{}) != null,
+ .none, .dir, .url => false,
+ };
+ if (!shown and !p.look_text) {
+ const msg: Plumb.Msg = .{
+ .src = if (pane.addon) |k| k.nameOf() else if (pane.body.mode == .tty and !p.fs.serving) "term" else "pardes",
+ .wdir = Pardes.paneDir(pane),
+ .type = "text",
+ .data = trimmed,
+ };
+ const seen: Plumb.Seen = .{ .name = pl.path, .found = switch (found) {
+ .file => |f| .{ .path = f.path },
+ .kind => |k| .{ .path = k.path },
+ .dir => |d| .{ .path = d, .dir = true },
+ .none, .url, .pane => null,
+ } };
+ switch (Plumb.send(p, id, msg, seen)) {
+ .done => return,
+ .look, .refused => {},
+ }
+ }
switch (found) {
.none => lookUnresolved(p, id, pane, trimmed, std.mem.trimEnd(u8, txt, "\r\n")),
.pane => |t| {
@@ -1598,7 +1559,8 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void {
focusPaneLine(p, t.id, t.at, .center);
settleLine(p, id, trimmed, t.at, before, null);
},
- .url => |u| if (u.len <= 256) p.emit(.{ .open_link = .from(u) }),
+ // acme's own look opens no URL: the plumber's web port does.
+ .url => lookUnresolved(p, id, pane, trimmed, std.mem.trimEnd(u8, txt, "\r\n")),
.dir => |dir| {
// A directory that is not there (the pane's own, gone): said,
// ENOENT, no terminal made in it.
@@ -1619,18 +1581,6 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void {
const before = v.active;
var opened: ?usize = null;
if (!focusPaneByPath(p, target.path, target.at)) {
- // A .diff or .patch looked at bare: its hunks stacked in a
- // +Diff, which takes the keyboard as the file would; one
- // with none in it opens as text.
- if (!p.look_text and target.at.line == 0 and
- (std.mem.endsWith(u8, target.path, ".diff") or std.mem.endsWith(u8, target.path, ".patch")))
- if (filesystem.read(p, target.path)) |bytes| {
- defer p.gpa.free(bytes);
- if (panes.Output.openDiff(p, id, target.path, bytes)) |hid| {
- v.active = hid;
- return;
- } else |_| {}
- } else |_| {};
const free = p.freeSlot() orelse return p.reportError(id, "look", error.NoPaneSlots);
const nt = panes.File.open(p, free, target.path, target.at.line) catch |err|
return lookOpenFailed(p, id, target.path, err);
@@ -1900,17 +1850,9 @@ test "a zero column after a path is found, in every spelling a look takes" {
for ([_][]const u8{ "", ":3", ":3:1", ":3:2-5", ":3:2-5:7", ":30:10" }) |rest| try std.testing.expect(!zeroColumn(rest));
}
-test "a commit hash is 7 to 40 lowercase hex digits with a letter and a digit" {
- try std.testing.expect(isCommitHash("9bffa7041c"));
- try std.testing.expect(isCommitHash("9bffa7041cd2e8a0b6b1f7c3e4d5a6b7c8d9e0f1"));
- try std.testing.expect(!isCommitHash("9bffa7"));
- try std.testing.expect(!isCommitHash("defaced"));
- try std.testing.expect(!isCommitHash("1234567"));
- try std.testing.expect(!isCommitHash("0x9bffa7041c"));
- try std.testing.expect(!isCommitHash("9BFFA7041C"));
-}
test "Look hover waits without mutating the pane and input cancels it" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
const delay = config.look_preview_delay_frames;
const gpa = std.testing.allocator;
@@ -2021,6 +1963,7 @@ test "Look hover waits without mutating the pane and input cancels it" {
}
test "cwd changes cancel only the affected pane's Look hover" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // the classic boot's three shells
const gpa = std.testing.allocator;
const p = try Pardes.init(gpa, .{ .shells = 3 });
defer p.deinit();
@@ -2331,7 +2274,7 @@ test "a click, a 9P whole-line look and an empty event write-back take one expan
if (at.whole_line) {
const lines = diffLines(p, pane).?;
var buf: [4096 + 32]u8 = undefined;
- const whole = diffLineAddress(p, pane, lines[at.row], &buf) orelse return error.NoWholeLineAddress;
+ const whole = lineAddress(p, pane, lines[at.row], &buf) orelse return error.NoWholeLineAddress;
try std.testing.expectEqualStrings(at.want, whole);
}
}
@@ -2540,6 +2483,7 @@ test "Incl's directories find a name found nowhere else, <name> too; bare Incl l
}
test "LookNext in a command pane walks its own file:line places, whoever walked last" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a command pane
if (!platform_has_fs) return;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
@@ -2685,51 +2629,84 @@ test "Look on a diff line is the look of the address it names, selected by hand:
try std.testing.expectEqual(@as(i32, 1), seen[0].row);
}
-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" {
+test "Plumb: a look sends what it looks at, the rules written first: a rule's word or port takes it, a name open in a pane is focused unsent, one nothing takes is Look's own" {
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 = "<svg/>" });
- 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)];
+ var pic_buf: [4200]u8 = undefined;
+ const pic = try std.fmt.bufPrint(&pic_buf, "{s}/pic.svg", .{root});
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);
+ const pane = try p.setTestFile("see https://ziglang.org here\n");
+ // The defaults: a URL to the web port, the host's opener; the hover
+ // asks resolve, as before.
+ lookAt(p, 0, "https://ziglang.org");
+ var linked = false;
+ while (p.nextEffect()) |e| if (e == .open_link) {
+ try std.testing.expectEqualStrings("https://ziglang.org", e.open_link.slice());
+ linked = true;
+ };
+ try std.testing.expect(linked);
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);
+ // A rule set written first takes what it matches: its word started in
+ // place, from the looking pane, and no pane opened.
+ try std.testing.expect(Plumb.apply(p, false, "plumb/rules", "type is text\ndata matches '.*\\.svg'\narg isfile $data\nplumb start Msg svg $file\n\ninclude basic\n") == null);
+ lookAt(p, 0, pic);
+ try std.testing.expectEqual(@as(usize, 0), p.view().active);
+ var said_buf: [4300]u8 = undefined;
+ try std.testing.expectEqualStrings(try std.fmt.bufPrint(&said_buf, "svg {s}", .{pic}), pane.msg[0..pane.msg_len]);
+ // A name open in a pane is focused, never sent.
+ p.look_text = true;
+ lookAt(p, 0, pic);
+ p.look_text = false;
+ const opened = p.view().active;
+ try std.testing.expect(opened != 0);
+ p.view().active = 0;
+ p.setMessage(0, "before");
+ lookAt(p, 0, pic);
+ try std.testing.expectEqual(opened, p.view().active);
+ try std.testing.expectEqualStrings("before", pane.msg[0..pane.msg_len]);
+ // With no web rule, a URL is a word Look searches for.
+ p.view().active = 0;
+ try std.testing.expect(Plumb.apply(p, false, "plumb/rules", "type is text\narg isfile $data\nplumb to edit\n") == null);
+ while (p.nextEffect()) |_| {}
+ lookAt(p, 0, "https://ziglang.org");
+ while (p.nextEffect()) |e| try std.testing.expect(e != .open_link);
+ const dot = @import("ninep/pane.zig").dotOf(pane);
+ try std.testing.expectEqualStrings("https://ziglang.org", pane.file.?.content[dot.q0..dot.q1]);
+}
+
+test "Plumb: a commit hash looked at in a git work tree is git show's, paged; outside one Diff declines, and the word is searched for" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a command pane
+ if (!platform_has_fs) return;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.createDir(std.testing.io, ".git", .default_dir);
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "fixed in 3f2a9c1\n" });
+ 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();
+ // /test.txt: its directory, /, is in no work tree.
+ const pane = try p.setTestFile("fixed in 3f2a9c1\n");
+ lookAt(p, 0, "3f2a9c1");
+ const dot = @import("ninep/pane.zig").dotOf(pane);
+ try std.testing.expectEqualStrings("3f2a9c1", pane.file.?.content[dot.q0..dot.q1]);
+ for (p.panes) |slot| if (slot) |pn| try std.testing.expect(pn.command == null);
+ var notes_buf: [4200]u8 = undefined;
+ lookAt(p, 0, try std.fmt.bufPrint(&notes_buf, "{s}/notes.txt", .{root}));
+ const notes = p.view().active;
+ try std.testing.expect(notes != 0);
+ lookAt(p, notes, "3f2a9c1");
+ var paged = false;
+ for (p.panes) |slot| if (slot) |pn| if (pn.command) |cmd| {
+ try std.testing.expect(std.mem.endsWith(u8, cmd, "git show 3f2a9c1"));
+ try std.testing.expect(pn.command_pages);
+ paged = true;
+ };
+ try std.testing.expect(paged);
}