diff options
Diffstat (limited to 'core/Trail.zig')
| -rw-r--r-- | core/Trail.zig | 117 |
1 files changed, 98 insertions, 19 deletions
diff --git a/core/Trail.zig b/core/Trail.zig index b6d6341a..9223d1d3 100644 --- a/core/Trail.zig +++ b/core/Trail.zig @@ -18,6 +18,7 @@ const look = @import("look.zig"); const Pardes = pardes.Pardes; const Pane = panes.Pane; const Spot = look.Spot; +const TagState = @import("TagState.zig"); const Trail = @This(); @@ -38,8 +39,10 @@ cur: u8 = 0, /// Kept, or edited since it was shown: it no longer walks (§6.5). held: bool = false, +/// `cur` is at most `n`: a held pane's (keep, a search's or a recipe's +/// word alone) has no current place, past its steps. pub fn lastCur(t: *const Trail) usize { - return t.n -| 1; + return t.n; } /// One step of a walk, as its word says it (§2 "trail word"). @@ -232,6 +235,31 @@ fn lift(p: *Pardes, id: usize) void { layout.restoreMinimaFrom(p, &now, &floor); } +/// `s`, a search's or a recipe's word, the pane's one of its kind (§7.3, +/// §7.5): the one it had goes, and it is the newest back step. +pub fn setWord(t: *Trail, s: Step) void { + var w: u8 = 0; + for (t.steps[0..t.n], 0..) |old, i| { + if (std.meta.activeTag(old) == std.meta.activeTag(s)) { + if (i < t.cur) t.cur -= 1; + continue; + } + t.steps[w] = old; + w += 1; + } + t.n = w; + if (t.n == max_steps) { + const first: usize = @intFromBool(t.steps[0] == .owner); + std.mem.copyForwards(Step, t.steps[first .. max_steps - 1], t.steps[first + 1 ..]); + t.n -= 1; + t.cur -|= 1; + } + std.mem.copyBackwards(Step, t.steps[t.cur + 1 .. t.n + 1], t.steps[t.cur..t.n]); + t.steps[t.cur] = s; + t.n += 1; + t.cur += 1; +} + /// Pane `id` held (§6.5): Keep, or an edit. It no longer walks, so the /// walk leaves its trail (places, lists, its owner, forward steps); words /// about its own text (a search, a recipe) stay. The next jump from its @@ -287,11 +315,11 @@ 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 "·"; - // The step's own bytes, not a copy's (a copy is gone at the return). - return switch (t.steps[i]) { +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 "·"; + // The step itself, not a copy: a word's text is the step's own bytes. + 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(), @@ -309,34 +337,52 @@ fn range(t: *const Trail, part: Part) [2]usize { } /// 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 { +/// path), or the forward steps', each with one before it (after the path): +/// the newest `whole` back steps whole, older ones `·` (`folded`). +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; 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)}), + .back => try w.print("{s}" ++ sep, .{wordOf(t, i, whole, &buf)}), + .forward => try w.print(sep ++ "{s}", .{wordOf(t, i, whole, &buf)}), }; } /// The bytes `write` writes for `part`. -pub fn bytes(t: *const Trail, part: Part) usize { +pub fn bytes(t: *const Trail, part: Part, whole: usize) 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; + for (r[0]..r[1]) |i| n += wordOf(t, i, whole, &buf).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; +} + +/// Where back step `i`'s word 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 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 }; +} + /// 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 { +pub fn stepAt(t: *const Trail, part: Part, whole: usize, 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; + 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; @@ -346,8 +392,8 @@ pub fn stepAt(t: *const Trail, part: Part, off: usize) ?u8 { /// Look on step `i` of pane `id`'s trail (§6.3): a place is gone back or /// forward to, the owner focused, a list unrolled again as it was -/// (Pick.reopen); a search or a recipe is the prompts' to take (not yet: -/// said). +/// (Pick.reopen), a search lit again, a recipe's selections put back +/// (Prompt.zig). pub fn lookStep(p: *Pardes, id: usize, i: u8) void { const pane = p.panes[id] orelse return; switch (pane.trail.steps[i]) { @@ -356,18 +402,20 @@ pub fn lookStep(p: *Pardes, id: usize, i: u8) void { p.view().active = owner; } else p.reportFailure(id, "walk: its owner is closed"), .list => |w| pardes.Pick.reopen(p, id, w.index), - .search, .recipe => p.setMessage(.at(id), "walk: a search or a recipe is not walked yet"), + .search => pardes.Prompt.lookWord(p, id), + .recipe => pardes.Prompt.lookRecipe(p, id), } } /// 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). +/// source again, a search searches again, a recipe replays. 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 => |w| pardes.Pick.rerun(p, id, w.index), - .search, .recipe => p.setMessage(.at(id), "walk: a search or a recipe is not run again yet"), + .search => pardes.Prompt.execWord(p, id), + .recipe => pardes.Prompt.execRecipe(p, id), } } @@ -779,3 +827,34 @@ test "Jumps new-pane jumps as before the walk: a new pane for a file no pane sho }; try std.testing.expectEqual(open + 2, now); } + +test "Enter on a word of the walk, the keyboard in the tag, goes back to its step as a click does; out of rest the walk folds to its newest word" { + 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 }) |line| f.look(f.held, "rank.zig", line); + const r = p.view().active; + const pane = p.panes[r].?; + var buf: [4096 + 128]u8 = undefined; + 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. + p.update(.{ .key = .{ .cp = ' ' } }); + 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 }))); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + 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. + pane.focus = .tag; + pane.tag.cur_row = 0; + pane.tag.cur_col = @intCast(std.mem.indexOf(u8, rest, "rank.zig:10").? + 2); + p.update(.{ .key = .{ .cp = pardes.Key.enter } }); + p.sync(); + try std.testing.expectEqualStrings("rank.zig:10", f.at(&at)); + try std.testing.expect(pane.focus == .body); +} |
