summaryrefslogtreecommitdiff
path: root/core/Trail.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/Trail.zig')
-rw-r--r--core/Trail.zig260
1 files changed, 224 insertions, 36 deletions
diff --git a/core/Trail.zig b/core/Trail.zig
index 1cd14091..c2c1b0c3 100644
--- a/core/Trail.zig
+++ b/core/Trail.zig
@@ -15,6 +15,7 @@ const config = @import("config.zig");
const layout = @import("layout.zig");
const panes = @import("panes.zig");
const look = @import("look.zig");
+const modal = @import("modal.zig");
const Pardes = pardes.Pardes;
const Pane = panes.Pane;
const Spot = look.Spot;
@@ -27,7 +28,8 @@ pub const max_steps = 16;
/// The bytes of a step's path or word. A jump to a longer path is no step:
/// it opens a pane as `Jumps new-pane` does (look.zig open).
pub const max_text = 256;
-/// The newest back steps written whole; older ones fold to `·` (§6.3).
+/// The newest back steps written whole at rest; older ones fold to `·`
+/// (§6.3). Out of rest the owner's and the newest alone (`.newest`).
pub const shown_steps = 3;
/// `cur` held to the steps there are (walk.zig `.place`).
@@ -326,81 +328,172 @@ pub fn walkBy(p: *Pardes, id: usize, delta: i32) bool {
// ---- the words ----
-pub const Part = enum { back, forward };
+/// `newest`: out of rest (UX D1), the owner's word and the newest back
+/// step's alone, each with the separator after it.
+pub const Part = enum { back, forward, newest };
/// 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, whole: usize, buf: *[max_text + 16]u8) []const u8 {
- const s = &t.steps[i];
- if (s.* != .owner and i < t.cur and t.cur - i > whole) return "·";
+/// own text. A place's file is named by its basename, or where another
+/// step's file has the same one, by the shortest end of its path the other
+/// does not share (UX D8: `core/x.zig:3` beside `dev/x.zig:9`).
+fn wordOf(t: *const Trail, i: usize, buf: *[max_text + 16]u8) []const u8 {
// The step itself, not a copy: a word's text is the step's own bytes.
- return switch (s.*) {
+ return switch (t.steps[i]) {
.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
+ .place => |*at| place: {
+ const path = at.path.slice();
+ var from = if (std.mem.lastIndexOfScalar(u8, path, '/')) |slash| slash + 1 else 0;
+ while (from > 0 and shared(t, i, path[from..])) from = if (std.mem.lastIndexOfScalar(u8, path[0 .. from - 1], '/')) |slash| slash + 1 else 0;
+ break :place std.fmt.bufPrint(buf, "{s}:{d}", .{ path[from..], at.line }) catch unreachable; // a path and a u32 fit
+ },
.list, .search, .recipe => |*w| w.text.slice(),
};
}
+/// Whether another step's file, not step `i`'s, has a path ending in `end`
+/// at a `/`.
+fn shared(t: *const Trail, i: usize, end: []const u8) bool {
+ const own = t.steps[i].place.path.slice();
+ for (t.steps[0..t.n], 0..) |*s, j| {
+ if (j == i or s.* != .place) continue;
+ const path = s.place.path.slice();
+ if (std.mem.eql(u8, path, own) or !std.mem.endsWith(u8, path, end)) continue;
+ if (path.len == end.len or path[path.len - end.len - 1] == '/') return true;
+ }
+ return false;
+}
+
const sep = " › ";
/// The steps a part writes.
fn range(t: *const Trail, part: Part) [2]usize {
return switch (part) {
- .back => .{ 0, t.cur },
+ .back, .newest => .{ 0, t.cur },
.forward => .{ @min(t.n, t.cur + 1), t.n },
};
}
+/// The forward steps written whole after the path at rest; the rest fold.
+pub const shown_ahead = 2;
+
+/// Steps [lo, hi) of `part` folded into one dim `+N` word where the first
+/// of them is (UX D8): at rest the back steps older than the newest
+/// `whole` (never the owner), the forward steps past `shown_ahead`.
+fn foldOf(t: *const Trail, part: Part, whole: usize) [2]usize {
+ return switch (part) {
+ .back => back: {
+ const lo: usize = @intFromBool(t.cur > 0 and t.steps[0] == .owner);
+ break :back .{ lo, @max(lo, t.cur -| whole) };
+ },
+ .forward => .{ @min(t.n, t.cur + 1 + shown_ahead), t.n },
+ .newest => .{ 0, 0 },
+ };
+}
+
+/// The cells of the newest step's word out of rest: a longer one is cut
+/// in its middle (UX D1: `Find pick…3/6`).
+const newest_cells = 24;
+
+/// Step `i`'s word as `part` writes it, null where it writes none: a
+/// folded step's is the fold's `+N` on its first, none on the others; out
+/// of rest the owner's and the newest step's alone, a word longer than
+/// `newest_cells` keeping its first and last cells around a `…`.
+fn wordIn(t: *const Trail, part: Part, whole: usize, i: usize, buf: *[max_text + 16]u8, cut: *[max_text + 16]u8) ?[]const u8 {
+ if (part == .newest and t.steps[i] != .owner and i + 1 != t.cur) return null;
+ const fold = foldOf(t, part, whole);
+ if (i >= fold[0] and i < fold[1]) return if (i == fold[0]) std.fmt.bufPrint(buf, "+{d}", .{fold[1] - fold[0]}) catch unreachable else null;
+ const word = wordOf(t, i, buf);
+ if (part != .newest or word.len <= newest_cells) return word;
+ const tab = 8; // a step's word holds no tab
+ if (panes.File.displayWidth(tab, word) <= newest_cells) return word;
+ const head = panes.File.byteAtDisplay(tab, word, newest_cells / 2);
+ var tail = word.len;
+ while (tail > head and panes.File.displayWidth(tab, word[tail..]) < newest_cells / 2 - 1) tail = modal.prevGrapheme(word, tail);
+ return std.fmt.bufPrint(cut, "{s}…{s}", .{ word[0..head], word[tail..] }) catch word;
+}
+
/// 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):
-/// the newest `whole` back steps whole, older ones `·` (`folded`).
+/// path), or the forward steps', each with one before it (after the path),
+/// the folded ones as one `+N` (`foldOf`); out of rest (`newest`) the
+/// owner's and the newest's.
pub fn write(t: *const Trail, part: Part, whole: usize, w: *std.Io.Writer) std.Io.Writer.Error!void {
var buf: [max_text + 16]u8 = undefined;
+ var cut: [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, whole, &buf)}),
- .forward => try w.print(sep ++ "{s}", .{wordOf(t, i, whole, &buf)}),
+ for (r[0]..r[1]) |i| if (wordIn(t, part, whole, i, &buf, &cut)) |word| switch (part) {
+ .back, .newest => try w.print("{s}" ++ sep, .{word}),
+ .forward => try w.print(sep ++ "{s}", .{word}),
};
}
/// The bytes `write` writes for `part`.
pub fn bytes(t: *const Trail, part: Part, whole: usize) usize {
var buf: [max_text + 16]u8 = undefined;
+ var cut: [max_text + 16]u8 = undefined;
var n: usize = 0;
const r = range(t, part);
- for (r[0]..r[1]) |i| n += wordOf(t, i, whole, &buf).len + sep.len;
+ for (r[0]..r[1]) |i| if (wordIn(t, part, whole, i, &buf, &cut)) |word| {
+ n += word.len + sep.len;
+ };
return n;
}
-/// The back steps pane `pane`'s tag writes whole: `shown_steps` at rest,
-/// the newest alone while its tag is out of rest (SPEC §3.4.3), so a
-/// state's head and the band's spines keep their room.
-pub fn folded(p: *const Pardes, pane: *const Pane) usize {
- const host = TagState.hostOf(p) orelse return shown_steps;
- return if (p.panes[host] == pane) 1 else shown_steps;
+/// The back step `n` and N walk (Pick.walkList, Prompt): the newest list
+/// or search; its word is underlined (UX D8).
+pub fn walked(t: *const Trail) ?u8 {
+ var i: usize = t.cur;
+ while (i > 0) {
+ i -= 1;
+ if (t.steps[i] == .list or t.steps[i] == .search) return @intCast(i);
+ }
+ return null;
+}
+
+/// Whether pane `pane`'s tag is out of rest in the live view (UX D1: its
+/// path, its forward steps and its own words hidden, the owner and the
+/// newest step before the head). Asking is rest.
+pub fn outOfRest(p: *const Pardes, pane: *const Pane) bool {
+ const host = TagState.hostOf(p) orelse return false;
+ return p.panes[host] == pane;
}
-/// Where back step `i`'s word is in what `write` writes for the back
-/// steps, the newest `whole` whole.
+/// Where back step `i`'s word (the fold's `+N` for a folded one) is in
+/// what `write` writes for the back steps, the newest `whole` whole.
pub fn spanOf(t: *const Trail, i: usize, whole: usize) [2]usize {
var buf: [max_text + 16]u8 = undefined;
+ var cut: [max_text + 16]u8 = undefined;
var x: usize = 0;
- for (0..i) |j| x += wordOf(t, j, whole, &buf).len + sep.len;
- return .{ x, x + wordOf(t, i, whole, &buf).len };
+ for (0..i) |j| if (wordIn(t, .back, whole, j, &buf, &cut)) |word| {
+ x += word.len + sep.len;
+ };
+ const fold = foldOf(t, .back, whole);
+ const at = if (i >= fold[0] and i < fold[1]) fold[0] else i;
+ if (at < i) return spanOf(t, at, whole);
+ const word: []const u8 = wordIn(t, .back, whole, i, &buf, &cut) orelse "";
+ return .{ x, x + word.len };
+}
+
+/// Whether back step `i` is in the fold's `+N`.
+pub fn isFolded(t: *const Trail, i: usize, whole: usize) bool {
+ const fold = foldOf(t, .back, whole);
+ return i >= fold[0] and i < fold[1];
}
/// 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, whole: usize, off: usize) ?u8 {
var buf: [max_text + 16]u8 = undefined;
+ var cut: [max_text + 16]u8 = undefined;
var x: usize = 0;
const r = range(t, part);
+ const fold = foldOf(t, part, whole);
for (r[0]..r[1]) |i| {
+ const word = wordIn(t, part, whole, i, &buf, &cut) orelse continue;
if (part == .forward) x += sep.len;
- const n = wordOf(t, i, whole, &buf).len;
- if (off >= x and off < x + n) return @intCast(i);
- x += n;
- if (part == .back) x += sep.len;
+ // A fold's `+N` is the step nearest the path of those it holds.
+ if (off >= x and off < x + word.len) return @intCast(if (i == fold[0] and fold[0] < fold[1]) (if (part == .back) fold[1] - 1 else fold[0]) else i);
+ x += word.len;
+ if (part != .forward) x += sep.len;
}
return null;
}
@@ -744,19 +837,88 @@ test "the walk drawn with a picker's list and the notice row (walk UI G 2e, SPEC
try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, p.panes[rd].?), "› Refs 2/2 ›") != null);
}
-test "a trail keeps its newest three back steps whole, older ones fold to ·, and a click on a · goes back to its step" {
+test "a trail keeps its newest three back steps whole, older ones fold into one dim `+N` after the owner, and a click on it goes back to the newest of them; forward steps past two fold the same way" {
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);
+ for ([_]usize{ 10, 20, 30, 40, 50, 60 }) |line| f.look(f.held, "rank.zig", line);
const r = p.view().active;
+ const pane = p.panes[r].?;
+ // Wide enough for the three newest whole on its one row.
+ p.update(.{ .resize = .{ .cols = 400, .rows = 60 } });
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.expect(std.mem.startsWith(u8, try tagOf(p, pane), try std.fmt.bufPrint(&buf, "‹@p{d} › +2 › rank.zig:30 › rank.zig:40 › rank.zig:50 › {s}/rank.zig Keep ", .{ owner, f.dir })));
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const s = try p.render(arena.allocator());
+ const fold = try tagCell(p, r, "+2");
+ // In the dim ink of the reading tag's ground (UX D4).
+ try std.testing.expectEqual(pardes.Color{ .rgb = p.chromeTheme().roles.on(.reading_focused).dim }, s.at(fold[0], fold[1]).style.fg);
+ try clickTag(p, r, "+2", .right);
+ try std.testing.expectEqualStrings("rank.zig:20", f.at(&at));
+ // Back at the first: five forward steps, the two nearest whole.
+ try clickTag(p, r, "rank.zig:10", .right);
try std.testing.expectEqualStrings("rank.zig:10", f.at(&at));
+ try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, pane), try std.fmt.bufPrint(&buf, "{s}/rank.zig › rank.zig:20 › rank.zig:30 › +3 Keep ", .{f.dir})) != null);
+}
+
+test "the walk never adds a row to a tag at rest: its steps fold further, down to the newest alone, where they would" {
+ var f: Fixture = .{ .p = undefined, .tmp = undefined };
+ try f.init(60);
+ defer f.deinit();
+ const p = f.p;
+ f.look(f.held, "rank.zig", 10);
+ for ([_]usize{ 20, 30, 40, 50, 60, 70 }) |line| {
+ f.look(p.view().active, "place.zig", line);
+ f.look(p.view().active, "rank.zig", line);
+ }
+ const r = p.view().active;
+ const pane = p.panes[r].?;
+ try std.testing.expect(pane.trail.cur > 4);
+ // A width 35 cells past what the tag takes with no walk: room on its
+ // row for the owner, a fold and the newest, not for three.
+ const walk = pane.trail;
+ pane.trail.n = 0;
+ pane.trail.cur = 0;
+ const bare = panes.File.displayWidth(p.settings.tab, try tagOf(p, pane));
+ pane.trail = walk;
+ const extra = p.view().screen_w - pardes.tagline.tagWidth(p, r);
+ p.update(.{ .resize = .{ .cols = @intCast(bare + 35 + extra), .rows = 60 } });
+ p.sync();
+ try std.testing.expectEqual(@as(u16, 1), pardes.layout.wantedTagRows(p, r));
+ try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, pane), " › +") != null);
+}
+
+test "two steps whose files have the same name are told apart by the shortest path they do not share; the word n walks is underlined" {
+ var f: Fixture = .{ .p = undefined, .tmp = undefined };
+ try f.init(60);
+ defer f.deinit();
+ const p = f.p;
+ try f.tmp.dir.createDirPath(std.testing.io, "core");
+ try f.tmp.dir.writeFile(std.testing.io, .{ .sub_path = "core/rank.zig", .data = "a\nb\nc\n" });
+ f.look(f.held, "rank.zig", 12);
+ f.look(p.view().active, "core/rank.zig", 2);
+ f.look(p.view().active, "place.zig", 3);
+ const r = p.view().active;
+ var buf: [4096 + 128]u8 = undefined;
+ const tag = try tagOf(p, p.panes[r].?);
+ try std.testing.expect(std.mem.indexOf(u8, tag, try std.fmt.bufPrint(&buf, " › {s}/rank.zig:12 › core/rank.zig:2 › ", .{std.Io.Dir.path.basename(f.dir)})) != null);
+ // A list word: the one n walks, underlined.
+ p.panes[r].?.trail.steps[1] = .{ .list = .{} };
+ p.panes[r].?.trail.steps[1].list.text.set("Find x 1/2");
+ p.sync();
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const s = try p.render(arena.allocator());
+ const word = try tagCell(p, r, "Find x 1/2");
+ try std.testing.expect(s.at(word[0], word[1]).style.ul == .single);
+ // In the key ink (UX D8, D4).
+ try std.testing.expectEqual(pardes.Color{ .rgb = p.chromeTheme().roles.key }, s.at(word[0], word[1]).style.fg);
+ const other = try tagCell(p, r, "place.zig");
+ try std.testing.expect(s.at(other[0], other[1]).style.ul != .single);
}
test "Ctrl-o goes back one place in a reader's walk and Ctrl-i forward again; past its first place, or from a held pane, the jump list's" {
@@ -857,12 +1019,38 @@ test "Enter on a word of the walk, the keyboard in the tag, goes back to its ste
var at: [64]u8 = undefined;
const rest = try std.fmt.bufPrint(&buf, "‹@p{d} › rank.zig:10 › rank.zig:20 › {s}/rank.zig Keep", .{ owner, f.dir });
try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, pane), rest));
- // SPC: out of rest, the older steps are `·`, the owner and the newest
- // whole; back at rest, as they were.
+ // SPC, its level shown: out of rest the tag is the owner, the newest
+ // step and the head (UX D1): no older step, no path, no Keep, none of
+ // its own words; back at rest, as they were.
+ const whole_rest = try p.gpa.dupe(u8, try tagOf(p, pane));
+ defer p.gpa.free(whole_rest);
p.update(.{ .key = .{ .cp = ' ' } });
+ p.advance(p.clock.now_ns);
+ p.clock.now_ns = p.view().tag_state.due_ns;
+ p.sync();
var folded_buf: [4096 + 128]u8 = undefined;
- try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, pane), try std.fmt.bufPrint(&folded_buf, "‹@p{d} › · › rank.zig:20 › {s}/rank.zig", .{ owner, f.dir })));
+ const out = try tagOf(p, pane);
+ try std.testing.expect(std.mem.startsWith(u8, out, try std.fmt.bufPrint(&folded_buf, "‹@p{d} › rank.zig:20 › ␣ ▸ ", .{owner})));
+ try std.testing.expect(std.mem.indexOf(u8, out, "rank.zig:10") == null and std.mem.indexOf(u8, out, f.dir) == null and std.mem.indexOf(u8, out, "Keep") == null);
+ // Look on the newest step's word is that step.
+ try std.testing.expectEqual(@as(?u8, pane.trail.cur - 1), pardes.tagline.trailAt(p, pane, std.mem.indexOf(u8, out, "rank.zig:20").? + 3));
+ try std.testing.expectEqual(@as(?u8, 0), pardes.tagline.trailAt(p, pane, 1));
+ p.update(.{ .key = .{ .cp = pardes.Key.escape } });
+ try std.testing.expectEqualStrings(whole_rest, try tagOf(p, pane));
+ // Back a step (a forward step after the path at rest): out of rest the
+ // forward step is hidden as the path is.
+ pardes.Trail.lookStep(p, r, pane.trail.cur - 1);
+ p.sync();
+ try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, pane), "rank.zig:30") != null);
+ p.update(.{ .key = .{ .cp = ' ' } });
+ p.advance(p.clock.now_ns);
+ p.clock.now_ns = p.view().tag_state.due_ns;
+ p.sync();
+ try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, pane), try std.fmt.bufPrint(&folded_buf, "‹@p{d} › rank.zig:10 › ␣ ▸ ", .{owner})));
+ try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, pane), "rank.zig:30") == null);
p.update(.{ .key = .{ .cp = pardes.Key.escape } });
+ pardes.Trail.lookStep(p, r, pane.trail.cur + 1);
+ p.sync();
try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, pane), rest));
// The keyboard in the tag, its cursor on `rank.zig:10`: Enter goes back
// to that step, the keyboard in the body again.