summaryrefslogtreecommitdiff
path: root/core/Trail.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-24 19:24:44 -0300
committerGabriel Schneider <[email protected]>2026-11-24 19:24:44 -0300
commit1c4aea3b0298f6f59ffec9b49f4d5e5f38c8af0f (patch)
tree09aa46200e9b005852a533da932c46b09a077388 /core/Trail.zig
parent08524143c84c069a80861895fafd0b7411a201f5 (diff)
downloadpardes-1c4aea3b0298f6f59ffec9b49f4d5e5f38c8af0f.tar.gz
pardes-1c4aea3b0298f6f59ffec9b49f4d5e5f38c8af0f.zip
walk (pick-ui SPEC §6.3, §6.4, item 3 of the Walk UI line B): a reader's trail is words of its tag: its back steps before its path (`‹@p3 › rank.zig:14 › `, the owner's word written so that its Look is `@p3`, the newest shown_steps 3 whole and older ones folded to `·`, still their steps), its forward steps after it, dimmed in the theme's lineno ink (draw.zig inkTag, the name's tint loop made one with it), then Keep; the prefix is made in the room Trail.bytes measures (tagline.pathPrefix, no allocation past it), and the name's place after the words is tagline.nameAt (onName, typedIntoName, tagTintRange, the drafted name's caret); a click on a step's word is that step's Look (B1 with no drag, the one place it does more than select, and B3) or Exec (B2): a place gone back to (the later steps kept as forward steps) or forward to, the owner focused; a list, a search or a recipe word says it is not walked yet (their makers are D's and E's: Trail.lookStep, Trail.execStep); TEST FIRST: Trail.zig "the session of pick-ui §6.7: four jumps across three files, back by a click on a trail word, the later steps kept dimmed after the path, forward again, Keep, and a new reader on the next jump" and "a trail keeps its newest three back steps whole, older ones fold to ·, and a click on a · goes back to its step"; core-test 998/1164 (166 skipped), pane-test 183/184, behaviour-test 277/277
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'core/Trail.zig')
-rw-r--r--core/Trail.zig170
1 files changed, 170 insertions, 0 deletions
diff --git a/core/Trail.zig b/core/Trail.zig
index 63a57c2c..a198cae5 100644
--- a/core/Trail.zig
+++ b/core/Trail.zig
@@ -222,6 +222,92 @@ pub fn edited(p: *Pardes, f: *const panes.File.State) void {
}
+// ---- the words ----
+
+pub const Part = enum { back, forward };
+
+/// Step `i`'s word: `‹@p3`, `rank.zig:14`, a list's, search's or recipe's
+/// own text; `·` when it is folded.
+fn wordOf(t: *const Trail, i: usize, buf: *[max_text + 16]u8) []const u8 {
+ const s = t.steps[i];
+ if (s != .owner and i < t.cur and t.cur - i > shown_steps) return "·";
+ return switch (s) {
+ .owner => |serial| std.fmt.bufPrint(buf, "‹" ++ config.pane_addr ++ "{d}", .{serial}) catch unreachable, // a u32 fits
+ .place => |at| std.fmt.bufPrint(buf, "{s}:{d}", .{ std.Io.Dir.path.basename(at.path.slice()), at.line }) catch unreachable, // a basename and a u32 fit
+ .list, .search, .recipe => |w| w.text.slice(),
+ };
+}
+
+const sep = " › ";
+
+/// The steps a part writes.
+fn range(t: *const Trail, part: Part) [2]usize {
+ return switch (part) {
+ .back => .{ 0, t.cur },
+ .forward => .{ @min(t.n, t.cur + 1), t.n },
+ };
+}
+
+/// The back steps' words, each with the separator after it (before the
+/// path), or the forward steps', each with one before it (after the path).
+pub fn write(t: *const Trail, part: Part, w: *std.Io.Writer) std.Io.Writer.Error!void {
+ var buf: [max_text + 16]u8 = undefined;
+ const r = range(t, part);
+ for (r[0]..r[1]) |i| switch (part) {
+ .back => try w.print("{s}" ++ sep, .{wordOf(t, i, &buf)}),
+ .forward => try w.print(sep ++ "{s}", .{wordOf(t, i, &buf)}),
+ };
+}
+
+/// The bytes `write` writes for `part`.
+pub fn bytes(t: *const Trail, part: Part) usize {
+ var buf: [max_text + 16]u8 = undefined;
+ var n: usize = 0;
+ const r = range(t, part);
+ for (r[0]..r[1]) |i| n += wordOf(t, i, &buf).len + sep.len;
+ return n;
+}
+
+/// The step whose word in `part` is at byte `off` of what `write` wrote
+/// for it, null on a separator or past them.
+pub fn stepAt(t: *const Trail, part: Part, off: usize) ?u8 {
+ var buf: [max_text + 16]u8 = undefined;
+ var x: usize = 0;
+ const r = range(t, part);
+ for (r[0]..r[1]) |i| {
+ if (part == .forward) x += sep.len;
+ const n = wordOf(t, i, &buf).len;
+ if (off >= x and off < x + n) return @intCast(i);
+ x += n;
+ if (part == .back) x += sep.len;
+ }
+ return null;
+}
+
+/// Look on step `i` of pane `id`'s trail (§6.3): a place is gone back or
+/// forward to, the owner focused; a list, a search or a recipe is the
+/// pickers' and the prompts' to take (not yet: said).
+pub fn lookStep(p: *Pardes, id: usize, i: u8) void {
+ const pane = p.panes[id] orelse return;
+ switch (pane.trail.steps[i]) {
+ .place => _ = go(p, id, i) catch |err| p.reportError(.at(id), "walk", err),
+ .owner => |serial| if (p.paneBySerial(serial)) |owner| {
+ p.view().active = owner;
+ } else p.reportFailure(id, "walk: its owner is closed"),
+ .list, .search, .recipe => p.setMessage(.at(id), "walk: a list, a search or a recipe is not walked yet"),
+ }
+}
+
+/// Exec on step `i` (§6.3): a place and the owner as Look; a list runs its
+/// source again, a search searches again, a recipe replays (not yet).
+pub fn execStep(p: *Pardes, id: usize, i: u8) void {
+ const pane = p.panes[id] orelse return;
+ switch (pane.trail.steps[i]) {
+ .place, .owner => lookStep(p, id, i),
+ .list, .search, .recipe => p.setMessage(.at(id), "walk: a list, a search or a recipe is not run again yet"),
+ }
+}
+
fn typeKeys(p: *Pardes, keys: []const u8) void {
for (keys) |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } });
}
@@ -416,3 +502,87 @@ test "the reader of a held pane too short to halve goes to the bottom of the nex
const reader_id = f.p.view().active;
try std.testing.expectEqual(col + 1, layout.findPane(f.p, reader_id).?.col);
}
+
+/// Where `word` starts in pane `id`'s tag, on the screen.
+fn tagCell(p: *Pardes, id: usize, word: []const u8) ![2]u16 {
+ const pane = p.panes[id].?;
+ const tag = try tagOf(p, pane);
+ const i = std.mem.indexOf(u8, tag, word) orelse return error.NotInTag;
+ const r = p.view().rects[id];
+ return .{ r.x + pardes.TAG_TEXT_INSET + @as(u16, @intCast(panes.File.displayWidth(p.settings.tab, tag[0..i]))), p.tagTop(pane, r) };
+}
+
+/// A click with `button` on `word` in pane `id`'s tag: a press and a
+/// release on its first cell.
+fn clickTag(p: *Pardes, id: usize, word: []const u8, button: pardes.Mouse.Button) !void {
+ const at = try tagCell(p, id, word);
+ for ([_]pardes.Mouse.Kind{ .press, .release }) |kind| p.update(.{ .mouse = .{ .button = button, .kind = kind, .col = at[0], .row = at[1] } });
+ p.sync();
+}
+
+test "the session of pick-ui §6.7: four jumps across three files, back by a click on a trail word, the later steps kept dimmed after the path, forward again, Keep, and a new reader on the next jump" {
+ var f: Fixture = .{ .p = undefined, .tmp = undefined };
+ try f.init(60);
+ defer f.deinit();
+ const p = f.p;
+ const owner = p.panes[f.held].?.serial;
+ var buf: [4096 + 128]u8 = undefined;
+ var at: [64]u8 = undefined;
+ // 1 · g d on rank from the held pane: its reader, made below it.
+ f.look(f.held, "rank.zig", 12);
+ // 2 · Look at Place.of from line 14 of rank.zig: the reader walks.
+ typeKeys(p, "2j");
+ f.look(p.view().active, "place.zig", 8);
+ // 3 · g d on Span from line 9: one more step in the same file.
+ typeKeys(p, "j");
+ f.look(p.view().active, "place.zig", 40);
+ var r = p.view().active;
+ try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[r].?), try std.fmt.bufPrint(&buf, "‹@p{d} › rank.zig:14 › place.zig:9 › {s}/place.zig Keep ", .{ owner, f.dir })));
+ // 4 · a click on rank.zig:14: back, the later steps after the path,
+ // dimmed.
+ try clickTag(p, r, "rank.zig:14", .left);
+ r = p.view().active;
+ try std.testing.expectEqualStrings("rank.zig:14", f.at(&at));
+ try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[r].?), try std.fmt.bufPrint(&buf, "‹@p{d} › {s}/rank.zig › place.zig:9 › place.zig:40 Keep ", .{ owner, f.dir })));
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const s = try p.render(arena.allocator());
+ const dim = try tagCell(p, r, "place.zig:40");
+ try std.testing.expectEqual(pardes.Color{ .rgb = p.chromeTheme().lineno }, s.at(dim[0], dim[1]).style.fg);
+ // A click on a forward step goes forward to it.
+ try clickTag(p, r, "place.zig:9", .left);
+ r = p.view().active;
+ try std.testing.expectEqualStrings("place.zig:9", f.at(&at));
+ try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[r].?), try std.fmt.bufPrint(&buf, "‹@p{d} › rank.zig:14 › {s}/place.zig › place.zig:40 Keep ", .{ owner, f.dir })));
+ // Exec (B2) on the owner's word: the owner has the keyboard.
+ try clickTag(p, r, "‹@p", .middle);
+ try std.testing.expectEqual(f.held, p.view().active);
+ // A new step from the reader drops the forward steps.
+ f.look(r, "rank.zig", 50);
+ r = p.view().active;
+ try std.testing.expectEqual(p.panes[r].?.trail.cur + 1, p.panes[r].?.trail.n);
+ // 5 · Keep: the walk leaves its tag; the next jump from the owner
+ // makes a new reader.
+ pardes.exec.runBuiltin(p, .Keep, r, "Keep", null);
+ p.sync();
+ try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[r].?), try std.fmt.bufPrint(&buf, "{s}/rank.zig Save", .{f.dir})));
+ f.look(f.held, "place.zig", 20);
+ const fresh = p.view().active;
+ try std.testing.expect(fresh != r and fresh != f.held and p.panes[r] != null);
+ try std.testing.expectEqual(owner, p.panes[fresh].?.trail.steps[0].owner);
+}
+
+test "a trail keeps its newest three back steps whole, older ones fold to ·, and a click on a · goes back to its step" {
+ var f: Fixture = .{ .p = undefined, .tmp = undefined };
+ try f.init(60);
+ defer f.deinit();
+ const p = f.p;
+ const owner = p.panes[f.held].?.serial;
+ for ([_]usize{ 10, 20, 30, 40, 50 }) |line| f.look(f.held, "rank.zig", line);
+ const r = p.view().active;
+ var buf: [4096 + 128]u8 = undefined;
+ var at: [64]u8 = undefined;
+ try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[r].?), try std.fmt.bufPrint(&buf, "‹@p{d} › · › rank.zig:20 › rank.zig:30 › rank.zig:40 › {s}/rank.zig Keep ", .{ owner, f.dir })));
+ try clickTag(p, r, "·", .right);
+ try std.testing.expectEqualStrings("rank.zig:10", f.at(&at));
+}