//! A pane's walk (pick-ui SPEC §6): the places a reading pane stepped //! through, written as words in its tag before its path, and whether it //! is held. A held pane (edited, kept, or not a file) never walks: a jump //! from it steps its one reader, a reading pane below it whose trail //! starts with it (`‹@p3`). A reading pane steps in place. //! //! The steps are one array, oldest first: `cur` is where the pane is (its //! place as it was at its last step: the caret is the truth between //! steps), the steps before it the back steps, the ones after it the //! forward steps a step back left, drawn dimmed after the path. const std = @import("std"); const pardes = @import("root.zig"); const Kind = @import("Kind.zig"); 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; const TagState = @import("TagState.zig"); const Trail = @This(); /// The steps a trail keeps; past it the oldest go, the owner kept first. 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). pub const shown_steps = 3; /// `cur` held to the steps there are (walk.zig `.place`). pub const replica = .{ .len = .{.{ "steps", "n" }}, .place = .{.{ "cur", "lastCur" }} }; steps: [max_steps]Step = undefined, n: u8 = 0, 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; } /// One step of a walk, as its word says it (§2 "trail word"). pub const Step = union(enum) { /// A place the pane was at: `rank.zig:14`. place: Place, /// The held pane this is the reader of, by its serial: `‹@p3`, always /// the first step. owner: u32, /// A list the pane came through (`Find pick 3/6 •2`, §5.6): its word /// and the pickers' index for it. Made as a picker lands (Pick.listed). list: Word, /// A search made in the pane (`/dirty/ 3/7`, §7.3) and where it /// started: a walk back onto it puts the caret there (UX L'1). Made by /// a search prompt (Prompt.word). search: Search, /// A chain of selection prompts as an Edit line (§7.5). Made by the sam /// prompts (Prompt.link). recipe: Word, }; pub const Place = struct { path: Kind.Buf(max_text) = .{}, line: u32 = 0, col: u32 = 0 }; pub const Word = struct { text: Kind.Buf(max_text) = .{}, index: u32 = 0 }; pub const Search = struct { text: Kind.Buf(max_text) = .{}, index: u32 = 0, from: Place = .{} }; /// A file the walk can show: a text on its path, not an output, a /// listing, a pager, a query or a kind (§2: what is not a file is held). pub fn isFile(pane: *const Pane) bool { const f = pane.file orelse return false; return f.output == null and f.listing == null and !f.pager and f.mini == null and pane.query == null and pane.addon == null; } /// A pane that walks: a file not held, in a session whose jumps walk. A /// picker's preview in its body changes nothing: its own file, set aside /// under the preview, is the one asked of (tagline.named: its tag and its /// tint stay as at rest, UX DESIGN M1). pub fn reading(p: *const Pardes, pane: *const Pane) bool { if (p.settings.jumps != .walk or pane.trail.held) return false; if (pardes.Pick.landingOf(p, pane) == null) return isFile(pane); const f = pardes.tagline.named(p, pane) orelse return false; return f.output == null and f.listing == null and !f.pager and f.mini == null and pane.query == null; } /// An untouched pane (UX J1): a `+New` never typed into (no undo /// history), its body empty, still so named, not kept, not folded, and no /// 9P open holding it: a look or exec open answered with it (exec.leased), /// or an open of one of its own files (a script's look or exec at it, an /// event helper's event), so that a script's pane/new stays its own. It is /// free space: the next pane made takes its place (layout.freeSpace), a /// jump from it among them. pub fn untouched(p: *const Pardes, pane: *const Pane) bool { const f = pane.file orelse return false; const o = f.output orelse return false; if (o.from != .cmd or o.from.cmd != .New or f.pager) return false; if (f.content.len != 0 or f.history.journal.steps.items.len != 0 or pane.trail.held or pane.collapsed) return false; if (!std.mem.eql(u8, std.Io.Dir.path.basename(f.path), config.scratch_buffer) or @import("exec.zig").leased(p, pane.serial)) return false; for (p.fs.opens[0..p.fs.opens_len]) |open| if (open.node != 0) if (pardes.ctlfs.Node.target(open.node)) |t| { if (t == .pane and t.pane.serial == pane.serial) return false; }; return true; } /// The reading pane that is held pane `owner`'s reader, if it has one. pub fn readerOf(p: *const Pardes, owner: *const Pane) ?usize { for (p.panes, 0..) |slot, id| { const r = slot orelse continue; if (r == owner or !isFile(r) or r.trail.held or r.trail.n == 0) continue; if (r.trail.steps[0] == .owner and r.trail.steps[0].owner == owner.serial) return id; } return null; } /// A new reader's trail: its owner, then where it opened. With no owner, /// a jump's from an untouched pane, whose place it takes (UX J1): its /// trail starts at the file. pub fn reader(owner: ?u32, path: []const u8, at: Spot) Trail { var t: Trail = .{}; if (owner) |o| push(&t, .{ .owner = o }); if (placeOf(path, at.line, at.col)) |s| push(&t, s); t.cur = t.n -| 1; return t; } /// A place as a step, null when its path does not fit one. fn placeOf(path: []const u8, line: usize, col: usize) ?Step { if (path.len > max_text) return null; var at: Place = .{ .line = std.math.lossyCast(u32, line), .col = std.math.lossyCast(u32, col) }; at.path.set(path); return .{ .place = at }; } /// Where `pane` is now, as a step. fn here(pane: *const Pane) ?Step { const f = pane.file orelse return null; return placeOf(f.path, @as(usize, @intCast(@max(0, pane.body.cur_row))) + 1, @as(usize, @intCast(@max(0, pane.body.cur_col))) + 1); } fn samePlace(s: Step, path: []const u8, line: usize) bool { return s == .place and s.place.line == line and std.mem.eql(u8, s.place.path.slice(), path); } /// `s` as the newest step, the oldest that is not the owner dropped when /// the trail is full. fn push(t: *Trail, s: Step) void { 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; } t.steps[t.n] = s; t.n += 1; } /// Step reading pane `id` to `path` at `at` (§6.2): a place already on its /// trail is gone back or forward to, never held twice; anything else is a /// new step, which drops the forward steps. The pane's slot after it: a /// step to another file is a new pane in its place (`replace`). pub fn step(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { const pane = p.panes[id].?; const t = &pane.trail; // At a word (a walk onto a search's, a list's Enter, UX L'1): a step // from it keeps the word, and is one wherever the pane is. const word = t.cur < t.n and t.steps[t.cur] != .place; // Where it is already: no step. if (!word) if (here(pane)) |was| if (samePlace(was, path, at.line)) return show(p, id, path, at); for (t.steps[0..t.cur], 0..) |s, i| if (samePlace(s, path, at.line)) return go(p, id, @intCast(i)); if (t.n > t.cur + 1) for (t.steps[t.cur + 1 .. t.n], t.cur + 1..) |s, j| if (samePlace(s, path, at.line)) return go(p, id, @intCast(j)); const to = placeOf(path, at.line, at.col) orelse return error.NameTooLong; // Where it was: the current step's place, or a first one. if (here(pane)) |was| { if (t.n > t.cur) { t.n = t.cur + 1; if (!word) t.steps[t.cur] = was; } else push(t, was); } else t.n = t.cur; push(t, to); t.cur = t.n - 1; return show(p, id, path, at); } /// Go to step `i` of reading pane `id`'s trail, back or forward (§6.4): /// where it is now becomes the step it leaves (a word it is at stays the /// word), the ones between stay. pub fn go(p: *Pardes, id: usize, i: u8) !usize { const pane = p.panes[id].?; const t = &pane.trail; if (i >= t.n or t.steps[i] != .place) return id; const s = t.steps[i]; leave(pane); t.cur = i; return show(p, id, s.place.path.slice(), .{ .line = s.place.line, .col = s.place.col }); } /// Where pane `pane` is now kept as the step it leaves, when that is a /// place: a word it is at stays the word. fn leave(pane: *Pane) void { const t = &pane.trail; if (t.cur < t.n and t.steps[t.cur] == .place) if (here(pane)) |was| { t.steps[t.cur] = was; }; } /// A list the pane's newest step came through (§6.2: a picker's Enter, /// Pick.zig): its word before the step's place. pub fn through(t: *Trail, s: Step) void { if (t.cur >= t.n or t.steps[t.cur] != .place) return; if (t.n == max_steps) { const first: usize = @intFromBool(t.steps[0] == .owner); if (t.cur <= first) return; 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; } /// Reading pane `id`'s current step replaced by `path` at `at`, no step /// pushed (a list's `n`, §5.6); its slot after it, as `step`'s. pub fn replaceStep(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { const to = placeOf(path, at.line, at.col) orelse return error.NameTooLong; const t = &p.panes[id].?.trail; if (t.cur < t.n and t.steps[t.cur] == .place) t.steps[t.cur] = to; return show(p, id, path, at); } /// Pane `id` showing `path` at `at`, the keyboard on it: its caret moved /// in the file it shows, or `path` opened in its place as the same pane, as /// acme's Get keeps a window's id: its walk, its serial (`@pN`, a script's /// `/pane/N`), the 9P state its scripts hold and its /log record (a rename), /// the file it left closed as any (Recent keeps its place). A line past the /// new file's end is a miss that changes nothing. A file with changes it /// has not saved is never closed by a step, whatever changed it (§6.5): its /// pane is held, as Keep holds it, and the step opens where a held pane's /// go, in its reader. fn show(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { // A step in the pane a picker asks from or previews in ends the picker // first, as Esc does (UX DESIGN O3): the step is from its own text. pardes.Pick.yieldFor(p, id); const pane = p.panes[id].?; if (pane.file) |f| if (std.mem.eql(u8, f.path, path)) { look.clearNavigationSelection(p, pane); look.focusPaneLine(p, id, at, .center); lift(p, id); return id; }; if (pardes.tagline.dirty(pane)) { keep(pane); const free = switch (look.open(p, id, path, at, .replace)) { .opened => |free| free, .shown, .none => return p.view().active, }; // (A kind's pane, look.open's for a file a kind takes, has no lines.) const nf = if (p.panes[free].?.file) |*f| f else return free; if (at.line > panes.File.nlines(p.gpa, nf)) { try p.removePane(free, null); p.view().active = id; return error.LineMissing; } return free; } const free = p.freeSlot() orelse return error.NoPaneSlots; const nt = try panes.File.open(p, free, path, at.line); // Refused: closed at the step's end, never placed (Pardes.unplaced). errdefer { p.unplaced.set(free); nt.vweight = 0; } if (at.line > panes.File.nlines(p.gpa, &nt.file.?)) return error.LineMissing; nt.trail = pane.trail; // What jj answered for the file it leaves goes with that file; its // text a revision on from the one it leaves, so nothing a worker was // asked of that text (a parse, a paint, both by serial and revision) // lands on this one. @import("jj.zig").forget(p, pane.serial); nt.serial = pane.serial; nt.file.?.revision = if (pane.file) |f| f.revision +% 1 else 0; nt.file.?.saved_revision = nt.file.?.revision; // Its 9P state: the one never announced leaves with no record. std.mem.swap(@TypeOf(pane.fs), &nt.fs, &pane.fs); // Before the layout gives its place away: its file is told closed // while it is still in its slot. try panes.deinitPane(p, pane); layout.replacePane(p, id, free); p.panes[id] = null; pardes.ctlfs.events.noteLog(p, .rename, nt); if (pardes.Pardes.onDisk(nt)) p.recent.opened(p.recentGpa(), path); lift(p, free); look.focusPaneLine(p, free, at, .keep); // Its line half the body's rows down, as a look's new pane has it. const rows = @max(1, p.view().rects[free].h -| pardes.BOX_H); panes.File.setTop(nt, panes.File.walkRows(p.settings.tab, nt, .{ .line = nt.file.?.scroll }, -@as(i64, rows / 2)).to); return free; } /// Pane `id`'s tag after a step may take more rows than it had (its walk's /// words, a longer name): it is given them from its column, as a step that /// wraps tags gives them (layout.restoreMinimaFrom), the others keeping /// their own minimum: a row at a time while its tag is folded to its newest /// step or its path cut for want of them (tagline.folded). What the column /// cannot give, the step never takes: the tag stays cut. fn lift(p: *Pardes, id: usize) void { const now = p.view().rects; var floor = layout.minima(p); floor[id] = 0; layout.restoreMinimaFrom(p, &now, &floor); const pane = p.panes[id] orelse return; while (pardes.tagline.folded(p, pane).squeezed and layout.giveRow(p, id)) {} } /// `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 /// old owner makes a new reader. pub fn keep(pane: *Pane) void { const t = &pane.trail; if (t.held) return; t.held = true; var w: u8 = 0; for (t.steps[0..t.backs()]) |s| if (s == .search or s == .recipe) { t.steps[w] = s; w += 1; }; t.n = w; t.cur = w; } /// An edit of file `f`: the panes showing it are held from now on (§6.5). /// Only where a pane could read: a session whose jumps walk, a file /// (isFile), so that an edit anywhere else (an output's, a 9P append's /// under `Jumps new-pane`) costs nothing past these checks. pub fn edited(p: *Pardes, f: *const panes.File.State) void { if (p.settings.jumps != .walk or f.output != null or f.listing != null or f.pager or f.mini != null) return; for (p.panes) |slot| { const pane = slot orelse continue; if (pane.trail.held) continue; const shown = if (pane.file) |*pf| pf else continue; if (shown == f or (f.twin != 0 and shown.twin == f.twin)) keep(pane); } } /// Back (`delta` < 0) or forward one word in pane `id`'s walk, as Ctrl-o /// and Ctrl-i go first (pick-ui SPEC §6.4, UX L'1): true when it had one /// that way, false to leave the step to the jump list (Jump.jumpBy). pub fn walkBy(p: *Pardes, id: usize, delta: i32) bool { const pane = p.panes[id] orelse return false; return walkFrom(p, id, pane.trail.cur, delta); } /// One word `delta`'s way from step `from` of pane `id`'s walk (UX L'1: /// the trail's words are the stack): onto a place, there (`go`); onto a /// list's word, that list unrolled again as it was left, previewing here, /// its Ctrl-o and Ctrl-i going on from its word (Pick.reopen); onto a /// search's, the caret where the search started and its places lit /// (Prompt.light); a held pane onto its own words, a search's and a /// recipe's (its selections back). A list no longer kept is said and /// passed by. pub fn walkFrom(p: *Pardes, id: usize, from: usize, delta: i32) bool { const pane = p.panes[id] orelse return false; const t = &pane.trail; var at = from; while (reach(p, pane, at, delta)) |i| { switch (t.steps[i]) { .owner => return false, // reach passes it by .place => _ = go(p, id, i) catch |err| p.reportError(.at(id), "walk", err), .list => |w| { if (pardes.Pick.byId(p, w.index) == null) { p.setMessage(.at(id), "walk: that list is gone: the last 8 are kept"); at = i; continue; } pardes.Pick.reopen(p, id, w.index, i); }, .search => |*s| { leave(pane); t.cur = i; // Its place copied: a step to another file frees this pane. const start = s.from; var to = id; if (start.path.len > 0 and !t.held) { to = show(p, id, start.path.slice(), .{ .line = start.line, .col = start.col }) catch |err| { p.reportError(.at(id), "walk", err); return true; }; } else if (start.path.len > 0 and std.mem.eql(u8, start.path.slice(), pane.file.?.path)) { look.focusPaneLine(p, id, .{ .line = start.line, .col = start.col }, .center); } pardes.Prompt.light(p, to); }, .recipe => { t.cur = i; pardes.Prompt.lookRecipe(p, id); }, } return true; } return false; } /// The step a walk from step `from` of pane `pane`'s trail reaches /// `delta`'s way (UX L'1): a place, a list's word or a search's of a /// reading pane; a held pane's own words, a search's and a recipe's; never /// the owner. Only where it walks: a file, in a session whose jumps walk. fn reach(p: *const Pardes, pane: *const Pane, from: usize, delta: i32) ?u8 { if (p.settings.jumps != .walk or !isFile(pane)) return null; const t = &pane.trail; var i = from; while (if (delta < 0) i > 0 else i + 1 < t.n) { i = if (delta < 0) i - 1 else i + 1; switch (t.steps[i]) { .owner => {}, .search => return @intCast(i), .place, .list => if (!t.held) return @intCast(i), .recipe => if (t.held) return @intCast(i), } } return null; } /// After a landing and a search's Enter in pane `id` (UX L'1): the word /// its next Ctrl-o reaches, said on the notice row as an echo, `C-o back /// to Find words 1/1`, never logged. pub fn sayBack(p: *Pardes, id: usize) void { const pane = p.panes[id] orelse return; const i = reach(p, pane, pane.trail.cur, -1) orelse return; var buf: [max_text + 16]u8 = undefined; var said: [max_text + 32]u8 = undefined; const text = std.fmt.bufPrint(&said, "C-o back to {s}", .{wordOf(&pane.trail, i, &buf)}) catch return; _ = pardes.Messages.keep(p, .echo, pane.serial, "", text); } // ---- 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. 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 (t.steps[i]) { .owner => |serial| std.fmt.bufPrint(buf, "‹" ++ config.pane_addr ++ "{d}", .{serial}) catch unreachable, // a u32 fits .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, .recipe => |*w| w.text.slice(), .search => |*s| s.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 end of the steps written before the path: the back steps, and the /// word the pane is at when a walk stepped onto one (UX L'1: a search's, /// its caret where the search started). pub fn backs(t: *const Trail) usize { return if (t.cur < t.n and t.steps[t.cur] != .place) t.cur + 1 else t.cur; } /// The steps a part writes. fn range(t: *const Trail, part: Part) [2]usize { return switch (part) { .back => .{ 0, t.backs() }, .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; /// Whether step `i` folds into a dim `+N` in `part`, one `+N` a run of them /// where its first is (UX D8, L'1: the fold takes places): at rest a place /// older than the newest `whole` back steps, or past the first /// `shown_ahead` forward steps; a list's word only where the tag keeps its /// newest step alone (`whole` 1), and never the newest list's; the owner's, /// a search's and a recipe's never. fn folds(t: *const Trail, part: Part, whole: usize, i: usize) bool { const end = t.backs(); return switch (part) { .forward => t.steps[i] == .place and i > t.cur + shown_ahead, .back => i + whole < end and switch (t.steps[i]) { .place => true, .list => whole == 1 and for (t.steps[i + 1 .. end]) |s| { if (s == .list) break true; } else false, .owner, .search, .recipe => false, }, }; } /// The first of step `i`'s run of folded steps in `part`. fn runStart(t: *const Trail, part: Part, whole: usize, i: usize) usize { var at = i; while (at > range(t, part)[0] and folds(t, part, whole, at - 1)) at -= 1; return at; } /// 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. fn wordIn(t: *const Trail, part: Part, whole: usize, i: usize, buf: *[max_text + 16]u8) ?[]const u8 { if (folds(t, part, whole, i)) { if (runStart(t, part, whole, i) != i) return null; var end = i; while (end < range(t, part)[1] and folds(t, part, whole, end)) end += 1; return std.fmt.bufPrint(buf, "+{d}", .{end - i}) catch unreachable; // a count fits } return wordOf(t, i, buf); } /// 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), /// each run of folded ones as one `+N` (`folds`). 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| if (wordIn(t, part, whole, i, &buf)) |word| switch (part) { .back => 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 n: usize = 0; const r = range(t, part); for (r[0]..r[1]) |i| if (wordIn(t, part, whole, i, &buf)) |word| { n += word.len + sep.len; }; return n; } /// 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.backs(); 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: its head and /// band hang under it, over its body (UX DESIGN I1, M2). 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 (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; const at = if (folds(t, .back, whole, i)) runStart(t, .back, whole, i) else i; var x: usize = 0; for (0..at) |j| if (wordIn(t, .back, whole, j, &buf)) |word| { x += word.len + sep.len; }; const word: []const u8 = wordIn(t, .back, whole, at, &buf) orelse ""; return .{ x, x + word.len }; } /// Whether back step `i` is in a fold's `+N`. pub fn isFolded(t: *const Trail, i: usize, whole: usize) bool { return folds(t, .back, whole, i); } /// 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 x: usize = 0; const r = range(t, part); for (r[0]..r[1]) |i| { const word = wordIn(t, part, whole, i, &buf) orelse continue; if (part == .forward) 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) { var near = i; if (part == .back and folds(t, part, whole, i)) while (near + 1 < r[1] and folds(t, part, whole, near + 1)) { near += 1; }; return @intCast(near); } x += word.len; if (part != .forward) 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 unrolled again as it was /// (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]) { .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 => |w| pardes.Pick.reopen(p, id, w.index, null), .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. 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 => pardes.Prompt.execWord(p, id), .recipe => pardes.Prompt.execRecipe(p, id), } } fn typeKeys(p: *Pardes, keys: []const u8) void { for (keys) |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); } fn tagOf(p: *Pardes, pane: *Pane) ![]const u8 { return pardes.tagline.tagText(p, p.scratch.allocator(), pane); } /// The ground of the cell under the first cell of pane `id`'s tag text. fn tagGroundOf(p: *Pardes, arena: *std.heap.ArenaAllocator, id: usize) !pardes.Color { p.sync(); _ = arena.reset(.retain_capacity); const s = try p.render(arena.allocator()); const r = p.view().rects[id]; return s.at(r.x + pardes.TAG_TEXT_INSET, p.tagTop(p.panes[id].?, r)).style.bg; } test "a file's pane is reading, Keep after its path and its grip in the mode's mark, its tag on its focus's ground (DESIGN.md G1'); typed into, it is held, and Jumps new-pane reads none" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); p.settings.jumps = .walk; const pane = try p.setTestFile("alpha\nbeta\n"); try std.testing.expect(reading(p, pane)); try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, pane), "/test.txt Keep ")); try std.testing.expectEqual(pardes.Color{ .rgb = p.chrome().on(.focus).ground }, try tagGroundOf(p, &arena, 0)); try std.testing.expect(p.surface.region(.grip, 0).?.reading); // Under Jumps new-pane nothing reads: the tag as it always was. p.settings.jumps = .@"new-pane"; try std.testing.expect(!reading(p, pane)); try std.testing.expectEqualStrings("/test.txt Del ", try tagOf(p, pane)); try std.testing.expectEqual(pardes.Color{ .rgb = p.chrome().on(.focus).ground }, try tagGroundOf(p, &arena, 0)); try std.testing.expect(!p.surface.region(.grip, 0).?.reading); p.settings.jumps = .walk; // Typed into: held from now on, though Undo takes the text back. typeKeys(p, "ix"); p.update(.{ .key = .{ .cp = pardes.Key.escape } }); typeKeys(p, "u"); try std.testing.expectEqualStrings("alpha\nbeta\n", pane.file.?.content); try std.testing.expect(!reading(p, pane)); try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, pane), "Keep") == null); try std.testing.expectEqual(pardes.Color{ .rgb = p.chrome().on(.focus).ground }, try tagGroundOf(p, &arena, 0)); try std.testing.expect(!p.surface.region(.grip, 0).?.reading); } test "Keep holds a reading pane; a scratch and a listing are never reading" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); p.settings.jumps = .walk; const pane = try p.setTestFile("alpha\n"); pardes.exec.runBuiltin(p, .Keep, 0, "Keep", null); try std.testing.expect(!reading(p, pane)); try std.testing.expect(pane.trail.held); pardes.exec.runBuiltin(p, .New, 0, "New", null); const scratch = p.panes[p.view().active].?; try std.testing.expect(scratch != pane and scratch.file != null and !reading(p, scratch)); var dir = std.testing.tmpDir(.{}); defer dir.cleanup(); var buf: [4096]u8 = undefined; const len = try dir.dir.realPath(std.testing.io, &buf); pardes.look.lookAt(p, 0, buf[0..len]); const listing = p.panes[p.view().active].?; try std.testing.expect(listing.file.?.listing != null and !reading(p, listing)); } /// A session over three files of 100 lines in a directory of its own, /// search.zig, rank.zig and place.zig; search.zig open and edited (held) /// in pane `held`, under the session's first pane. const Fixture = struct { p: *Pardes, tmp: std.testing.TmpDir, dir_buf: [4096]u8 = undefined, dir: []const u8 = "", held: usize = 0, fn init(f: *Fixture, rows: u16) !void { f.tmp = std.testing.tmpDir(.{}); errdefer f.tmp.cleanup(); var text: [100 * 9]u8 = undefined; var w: std.Io.Writer = .fixed(&text); for (1..101) |n| try w.print("line {d}\n", .{n}); for ([_][]const u8{ "search.zig", "rank.zig", "place.zig" }) |name| try f.tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = w.buffered() }); f.dir = f.dir_buf[0..try f.tmp.dir.realPath(std.testing.io, &f.dir_buf)]; f.p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 200, .rows = rows }); errdefer f.p.deinit(); _ = try f.p.setTestFile("scratch\n"); f.p.settings.jumps = .@"new-pane"; f.look(0, "search.zig", 88); f.p.settings.jumps = .walk; f.held = f.p.view().active; typeKeys(f.p, "ix"); f.p.update(.{ .key = .{ .cp = pardes.Key.escape } }); f.p.sync(); } fn deinit(f: *Fixture) void { f.p.deinit(); f.tmp.cleanup(); } /// A Look from pane `id` at `name:line` in the fixture's directory. fn look(f: *Fixture, id: usize, name: []const u8, line: usize) void { var buf: [4096 + 64]u8 = undefined; pardes.look.lookAt(f.p, id, std.fmt.bufPrint(&buf, "{s}/{s}:{d}", .{ f.dir, name, line }) catch unreachable); f.p.sync(); } fn active(f: *Fixture) *Pane { return f.p.panes[f.p.view().active].?; } /// The active pane's file's name and line, as `rank.zig:12`. fn at(f: *Fixture, buf: []u8) []const u8 { const pane = f.active(); return std.fmt.bufPrint(buf, "{s}:{d}", .{ std.Io.Dir.path.basename(pane.file.?.path), pane.body.cur_row + 1 }) catch unreachable; } }; test "a jump from a held pane steps its reader, made under it with half its rows; the reader walks in place, and a place on its trail is gone back to, never held twice" { var f: Fixture = .{ .p = undefined, .tmp = undefined }; try f.init(60); defer f.deinit(); const held = f.p.panes[f.held].?; try std.testing.expect(!reading(f.p, held)); const before = f.p.view().rects[f.held]; var buf: [64]u8 = undefined; // g d on rank: its reader, under it, half its rows. f.look(f.held, "rank.zig", 12); const r1 = f.p.view().active; try std.testing.expect(r1 != f.held); try std.testing.expectEqualStrings("rank.zig:12", f.at(&buf)); try std.testing.expectEqual(held.serial, f.active().trail.steps[0].owner); const col = layout.findPane(f.p, f.held).?; try std.testing.expectEqual(col.col, layout.findPane(f.p, r1).?.col); try std.testing.expectEqual(col.idx + 1, layout.findPane(f.p, r1).?.idx); try std.testing.expect(f.p.view().rects[f.held].h <= before.h / 2 + 1); // Look at place.zig:8 in the reader, from line 14: the reader walks. typeKeys(f.p, "2j"); f.look(r1, "place.zig", 8); const r2 = f.p.view().active; try std.testing.expect(f.p.panes[r1] == null or r2 == r1); try std.testing.expectEqualStrings("place.zig:8", f.at(&buf)); try std.testing.expectEqual(layout.findPane(f.p, f.held).?.idx + 1, layout.findPane(f.p, r2).?.idx); // g d on Span, from line 9: one more step in the same file. typeKeys(f.p, "j"); f.look(r2, "place.zig", 40); try std.testing.expectEqual(r2, f.p.view().active); try std.testing.expectEqualStrings("place.zig:40", f.at(&buf)); const t = &f.active().trail; try std.testing.expectEqual(@as(u8, 4), t.n); try std.testing.expectEqual(@as(u8, 3), t.cur); try std.testing.expectEqual(@as(u32, 14), t.steps[1].place.line); try std.testing.expectEqual(@as(u32, 9), t.steps[2].place.line); // The next jump from the held pane lands in the same reader. f.look(f.held, "rank.zig", 30); const r3 = f.p.view().active; try std.testing.expectEqualStrings("rank.zig:30", f.at(&buf)); try std.testing.expectEqual(held.serial, f.active().trail.steps[0].owner); var readers: usize = 0; for (f.p.panes) |slot| if (slot) |pane| { readers += @intFromBool(pane.trail.n > 0 and pane.trail.steps[0] == .owner); }; try std.testing.expectEqual(@as(usize, 1), readers); // A Look at a place on its trail goes back to it: nothing pushed. f.look(r3, "rank.zig", 14); try std.testing.expectEqualStrings("rank.zig:14", f.at(&buf)); try std.testing.expectEqual(@as(u8, 1), f.active().trail.cur); try std.testing.expectEqual(@as(u8, 5), f.active().trail.n); } test "a Look from inside the reader at a file its held pane shows focuses the held pane and replaces nothing" { var f: Fixture = .{ .p = undefined, .tmp = undefined }; try f.init(60); defer f.deinit(); f.look(f.held, "rank.zig", 12); const reader_id = f.p.view().active; const reader_pane = f.active(); f.look(reader_id, "search.zig", 89); try std.testing.expectEqual(f.held, f.p.view().active); try std.testing.expectEqual(@as(i32, 88), f.active().body.cur_row); try std.testing.expectEqual(reader_pane, f.p.panes[reader_id].?); try std.testing.expectEqualStrings("rank.zig", std.Io.Dir.path.basename(reader_pane.file.?.path)); } test "the reader of a held pane too short to halve goes to the bottom of the next column, of the previous one from the last" { var f: Fixture = .{ .p = undefined, .tmp = undefined }; try f.init(20); defer f.deinit(); const col = layout.findPane(f.p, f.held).?.col; try std.testing.expect(f.p.view().rects[f.held].h / 2 < f.p.panes[f.held].?.tag_rows + layout.reader_keeps); try std.testing.expect(layout.insertColumn(f.p, col, false) != null); f.look(f.held, "rank.zig", 12); 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: on whichever of /// its rows the tag wrapped it onto. 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 shown = try pardes.tagline.shownTag(p, p.scratch.allocator(), pane); const at = modal.positionAt(shown.text, shown.fromReal(i)); const col = panes.File.rawDisplayCol(p.settings.tab, modal.lineSlice(shown.text, at.row), at.col); const r = p.view().rects[id]; return .{ r.x + pardes.TAG_TEXT_INSET + @as(u16, @intCast(col)), p.tagTop(pane, r) + @as(u16, @intCast(at.row)) }; } /// 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()); // In the dim ink of the focused tag's ground (UX D4: 3:1 at least). const dim = try tagCell(p, r, "place.zig:40"); try std.testing.expectEqual(pardes.Color{ .rgb = p.chrome().on(.focus).dim }, 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 Del ", .{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 "the walk drawn with a picker's list and the notice row (walk UI G 2e, SPEC §3.7, §6.7): a list handed to a held pane lands in its reader, its word in the reader's trail before the place, the reader's tag in the reading tint with Keep, the held pane's not, n walking the list, the notice row at the foot saying where the caret is" { var f: Fixture = .{ .p = undefined, .tmp = undefined }; try f.init(40); defer f.deinit(); const p = f.p; p.messages.live_len = 0; const owner = p.panes[f.held].?.serial; var buf: [4096 + 128]u8 = undefined; var want: [4096 + 128]u8 = undefined; // g d from the held pane: its reader, made below it. f.look(f.held, "rank.zig", 12); const r = p.view().active; // A list asked from the held pane (SPC e's places, a server's answer): // hung under the held pane's tag (UX DESIGN O1), its Enter lands in the // reader, nothing made. const rows = try std.fmt.bufPrint(&buf, "{s}/place.zig:8:1 of\n{s}/place.zig:40:1 Span\n", .{ f.dir, f.dir }); pardes.Pick.open(p, f.held, .list, "Refs", "", rows); try std.testing.expectEqual(owner, p.view().tag_state.host); try std.testing.expectEqual(p.panes[r].?.serial, pardes.Pick.live(p).?.landing); pardes.Pick.landHere(p); p.sync(); // The reader stepped (to another file: a pane of its own, its owner's // word kept), no pane more. var n: usize = 0; for (p.panes) |slot| n += @intFromBool(slot != null); try std.testing.expectEqual(@as(usize, 3), n); const rd = p.view().active; try std.testing.expect(rd != f.held and p.panes[r] == null); try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[rd].?), try std.fmt.bufPrint(&want, "‹@p{d} › rank.zig:12 › Refs 1/2 › {s}/place.zig Keep ", .{ owner, f.dir }))); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); // The reader's tag on the focused ground, the held one's on the plain // (G1': the mode is on the reader's grip); the notice row says what // Ctrl-o goes back to (UX L'1), and with nothing said, where the // reader's caret is. try std.testing.expectEqual(pardes.Color{ .rgb = p.chrome().on(.focus).ground }, try tagGroundOf(p, &arena, rd)); try std.testing.expectEqual(pardes.Color{ .rgb = p.chrome().on(.plain).ground }, try tagGroundOf(p, &arena, f.held)); try std.testing.expect(p.surface.region(.grip, rd).?.reading and !p.surface.region(.grip, f.held).?.reading); for ([_][]const u8{ "C-o back to Refs 1/2", try std.fmt.bufPrint(&want, "{s}/place.zig:8", .{f.dir}) }) |said| { const s = try p.render(arena.allocator()); const foot = s.region(.notice_row, pardes.Region.none).?.rect; var row: std.ArrayList(u8) = .empty; for (0..s.cols) |x| try row.appendSlice(arena.allocator(), s.at(@intCast(x), foot.y).grapheme()); try std.testing.expect(std.mem.indexOf(u8, row.items, said) != null); _ = p.messages.retire(&.{.echo}); } // n: the list's next place, in place of the current step. typeKeys(p, "n"); p.sync(); try std.testing.expectEqual(@as(i32, 39), p.panes[rd].?.body.cur_row); 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 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, 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, 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 focused tag's ground (UX D4). try std.testing.expectEqual(pardes.Color{ .rgb = p.chrome().on(.focus).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 "a step whose tag would take a row its column cannot give folds to its newest step and cuts its path from the left, `…` and the basename whole, and takes no row; a column with a row to give grows its tag as before" { var f: Fixture = .{ .p = undefined, .tmp = undefined }; try f.init(60); defer f.deinit(); const p = f.p; const v = p.view(); // A path some 115 cells longer than rank.zig's, wider than a tag's row // here: its tag wraps onto a row more than rank.zig's. const deep = "a-directory-whose-name-runs-on-past-a-tag-row/" ++ "and-one-more-whose-name-runs-on-as-well/" ++ "and-a-third-for-good-measure/long.zig"; try f.tmp.dir.createDirPath(std.testing.io, std.Io.Dir.path.dirname(deep).?); try f.tmp.dir.writeFile(std.testing.io, .{ .sub_path = deep, .data = "a\nb\nc\nd\n" }); var full_buf: [4096 + 256]u8 = undefined; const full = try std.fmt.bufPrint(&full_buf, "{s}/{s}", .{ f.dir, deep }); f.look(f.held, "rank.zig", 12); var r = v.active; const c = layout.findPane(p, r).?.col; // Its column with no row to give: every pane at its minimum. var need: u16 = 0; for (v.cols[c].panes[0..v.cols[c].n]) |k| need += layout.minRows(p, k); p.update(.{ .resize = .{ .cols = 120, .rows = need + p.topBarHeight() + layout.columnBarHeight(p) + layout.noticeRowHeight(p) } }); try std.testing.expect(layout.shareColumn(p, c)); for (v.cols[c].panes[0..v.cols[c].n]) |k| try std.testing.expectEqual(layout.minRows(p, k), v.rects[k].h); const before = v.rects[r]; f.look(r, deep, 2); r = v.active; // In place, its rows as they were, every pane at its minimum. try std.testing.expectEqual(before, v.rects[r]); try std.testing.expect(layout.columnAtMinimums(p, c)); const pane = p.panes[r].?; try std.testing.expectEqualStrings(full, pane.file.?.path); // `‹@pN › rank.zig:12 › …` and the path's end, its basename whole. var tag = try tagOf(p, pane); var at: [64]u8 = undefined; const lead = try std.fmt.bufPrint(&at, "‹@p{d} › rank.zig:12 › …", .{p.panes[f.held].?.serial}); try std.testing.expect(std.mem.startsWith(u8, tag, lead)); var shown = tag[lead.len..std.mem.indexOf(u8, tag, " Keep ").?]; try std.testing.expect(shown.len < full.len and std.mem.endsWith(u8, full, shown) and std.mem.endsWith(u8, shown, "/long.zig")); // A step on to a short path and back: the cut again, its forward step // after it, dimmed where it is; the basename in the name's ink. f.look(r, "place.zig", 5); try clickTag(p, v.active, "long.zig:2", .right); r = v.active; try std.testing.expectEqual(before, v.rects[r]); try std.testing.expect(layout.columnAtMinimums(p, c)); tag = try tagOf(p, p.panes[r].?); try std.testing.expect(std.mem.startsWith(u8, tag, lead)); const ahead = pardes.tagline.forwardSpan(p, p.panes[r].?); try std.testing.expectEqualStrings(" › place.zig:5", tag[ahead[0]..ahead[1]]); shown = tag[lead.len..ahead[0]]; try std.testing.expect(std.mem.endsWith(u8, full, shown) and std.mem.endsWith(u8, shown, "/long.zig")); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); const s = try p.render(arena.allocator()); const base = try tagCell(p, r, "long.zig"); try std.testing.expectEqual(pardes.Color{ .rgb = p.chrome().name[1] }, s.at(base[0], base[1]).style.fg); // A click on the path drafts a name: the whole path, its caret where // the click was (on the first byte shown after the `…`). const after = v.rects[r]; const cell = after.x + pardes.TAG_TEXT_INSET + @as(u16, @intCast(panes.File.displayWidth(p.settings.tab, lead))); for ([_]pardes.Mouse.Kind{ .press, .release }) |kind| p.update(.{ .mouse = .{ .button = .left, .kind = kind, .col = cell, .row = p.tagTop(p.panes[r].?, after) } }); p.sync(); try std.testing.expect(p.panes[r].?.prompt == .name); try std.testing.expectEqualStrings(full, p.panes[r].?.input.own.?); try std.testing.expectEqual(@as(i32, @intCast(full.len - shown.len)), p.panes[r].?.input.cur_col); p.update(.{ .key = .{ .cp = pardes.Key.escape } }); // Back to rank.zig, a row to give in the column's first pane: the step // to the long path grows the tag as before, the whole path in it. try clickTag(p, r, "rank.zig:12", .right); r = v.active; p.update(.{ .resize = .{ .cols = 120, .rows = v.screen_h + 6 } }); for (v.cols[c].panes[0..v.cols[c].n]) |k| p.panes[k].?.vweight = @floatFromInt(layout.minRows(p, k)); p.panes[v.cols[c].panes[0]].?.vweight += 6; layout.compute(p); const short = v.rects[r]; f.look(r, deep, 2); r = v.active; try std.testing.expect(v.rects[r].h > short.h); try std.testing.expect(layout.columnAtMinimums(p, c)); tag = try tagOf(p, p.panes[r].?); try std.testing.expect(std.mem.indexOf(u8, tag, "…") == null and std.mem.indexOf(u8, tag, full) != null); } test "a band on a walked reading pane is whole: the layout's floor counts its tag at rest whole, as the sync after it keeps it (a pane the layout has given its width and not yet its height is never cut)" { var f: Fixture = .{ .p = undefined, .tmp = undefined }; try f.init(60); defer f.deinit(); const p = f.p; const v = p.view(); const deep = "a-directory-whose-name-runs-on-past-a-tag-row/" ++ "and-one-more-whose-name-runs-on-as-well/" ++ "and-a-third-for-good-measure/long.zig"; try f.tmp.dir.createDirPath(std.testing.io, std.Io.Dir.path.dirname(deep).?); try f.tmp.dir.writeFile(std.testing.io, .{ .sub_path = deep, .data = "a\nb\nc\nd\n" }); f.look(f.held, "rank.zig", 12); f.look(v.active, deep, 2); const r = v.active; const pane = p.panes[r].?; try std.testing.expect(reading(p, pane)); // Narrow: its tag at rest takes more rows than Help's head and band. p.update(.{ .resize = .{ .cols = 40, .rows = 60 } }); p.sync(); try std.testing.expect(pane.tag_rows > 5); const c = layout.findPane(p, r).?.col; const col = v.cols[c].panes[0..v.cols[c].n]; try std.testing.expect(col.len > 1); var rows: u16 = 0; for (col) |k| rows += v.rects[k].h; // Its share of the column at every height from 12 rows to 30 (its tag // at rest whole); Help with a query nothing matches: a 4-row band. var h: u16 = 12; while (h <= 30) : (h += 1) { for (col) |k| p.panes[k].?.vweight = if (k == r) @floatFromInt(h) else @as(f32, @floatFromInt(rows - h)) / @as(f32, @floatFromInt(col.len - 1)); layout.compute(p); p.sync(); for ([_]u21{ ' ', '?', 'q', 'q', 'x', 'q' }) |key| p.update(.{ .key = .{ .cp = key, .text = &[_]u8{@intCast(key)} } }); p.sync(); const at = pardes.Band.covered(p, pane, r); try std.testing.expect(at.band > 0); try std.testing.expectEqual(pardes.Band.takes(p, r), at.band); try std.testing.expectEqual(layout.wantedTagRows(p, r), pane.tag_rows); try std.testing.expect(v.rects[r].h >= at.tag + at.over); p.update(.{ .key = .{ .cp = pardes.Key.escape } }); p.sync(); try std.testing.expect(pardes.TagState.hostOf(p) == 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.chrome().on(.focus).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" { var f: Fixture = .{ .p = undefined, .tmp = undefined }; try f.init(60); defer f.deinit(); const p = f.p; var at: [64]u8 = undefined; f.look(f.held, "rank.zig", 12); f.look(p.view().active, "place.zig", 8); f.look(p.view().active, "place.zig", 40); const back: pardes.Key = .{ .cp = 'o', .ctrl = true }; const ahead: pardes.Key = .{ .cp = 'i', .ctrl = true }; p.update(.{ .key = back }); p.sync(); try std.testing.expectEqualStrings("place.zig:8", f.at(&at)); p.update(.{ .key = back }); p.sync(); try std.testing.expectEqualStrings("rank.zig:12", f.at(&at)); p.update(.{ .key = ahead }); p.sync(); try std.testing.expectEqualStrings("place.zig:8", f.at(&at)); p.update(.{ .key = back }); p.sync(); try std.testing.expectEqualStrings("rank.zig:12", f.at(&at)); // The owner's word is no place: past the first, the jump list's. try std.testing.expect(!walkBy(p, p.view().active, -1)); try std.testing.expect(!walkBy(p, f.held, -1)); } test "closing a held pane closes its reader; a kept reader stays; typed into, a reader is held and its walk leaves its tag" { 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; f.look(f.held, "rank.zig", 12); f.look(p.view().active, "place.zig", 8); const r = p.view().active; // Typed into: held, its walk gone from its tag, kept when its owner goes. typeKeys(p, "ix"); p.update(.{ .key = .{ .cp = pardes.Key.escape } }); p.sync(); try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[r].?), try std.fmt.bufPrint(&buf, "{s}/place.zig Save", .{f.dir}))); try std.testing.expectEqual(@as(u8, 0), p.panes[r].?.trail.n); // The next jump from its old owner makes a new reader. f.look(f.held, "rank.zig", 30); const fresh = p.view().active; try std.testing.expect(fresh != r); try std.testing.expectEqual(owner, p.panes[fresh].?.trail.steps[0].owner); // Del of the owner: its reader goes with it; the edited one stays. const fresh_serial = p.panes[fresh].?.serial; try p.removePane(f.held, null); p.sync(); try std.testing.expect(p.paneBySerial(fresh_serial) == null); try std.testing.expect(p.panes[r] != null); } test "Jumps new-pane jumps as before the walk: a new pane for a file no pane shows, under the asking one, the one showing it focused, nothing stepped" { var f: Fixture = .{ .p = undefined, .tmp = undefined }; try f.init(60); defer f.deinit(); const p = f.p; p.settings.jumps = .@"new-pane"; var at: [64]u8 = undefined; var open: usize = 0; for (p.panes) |slot| open += @intFromBool(slot != null); // From the held pane and from the reading one: a new pane each time. f.look(f.held, "rank.zig", 12); const rank = p.view().active; try std.testing.expectEqualStrings("rank.zig:12", f.at(&at)); f.look(rank, "place.zig", 8); try std.testing.expect(p.view().active != rank and p.panes[rank] != null); try std.testing.expectEqualStrings("place.zig:8", f.at(&at)); // A file a pane shows: that pane, focused; nothing new, no walk. f.look(p.view().active, "rank.zig", 30); try std.testing.expectEqual(rank, p.view().active); try std.testing.expectEqualStrings("rank.zig:30", f.at(&at)); var now: usize = 0; for (p.panes) |slot| if (slot) |pane| { now += 1; try std.testing.expectEqual(@as(u8, 0), pane.trail.n); }; 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 is as at rest, its words its steps (UX DESIGN M1)" { 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, its level shown: out of rest the tag is as at rest, every step, // the path and Keep, the head under it (UX DESIGN M1); each word its // step. 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(); try std.testing.expect(pardes.TagState.hostOf(p) == r); const out = try tagOf(p, pane); try std.testing.expectEqualStrings(whole_rest, out); 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: the forward step after the path, out of rest as at rest. pardes.Trail.lookStep(p, r, pane.trail.cur - 1); p.sync(); const back = try p.gpa.dupe(u8, try tagOf(p, pane)); defer p.gpa.free(back); try std.testing.expect(std.mem.indexOf(u8, back, "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.expectEqualStrings(back, try tagOf(p, pane)); 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. 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); } test "a walk step never closes a file with unsaved changes: a reading pane changed by any path, one that holds it or one that does not, is held as Keep holds it, and the step opens in its reader, the change kept (SPEC §6.5)" { const th = @import("ninep/harness.zig"); const tree = @import("ninep/tree.zig"); const Key = pardes.Key; const Path = enum { body, edit, pipe, lsp, paste, typed_unwalked, load }; var lost: usize = 0; for (std.enums.values(Path)) |path| { var f: Fixture = .{ .p = undefined, .tmp = undefined }; try f.init(60); defer f.deinit(); const p = f.p; // rank.zig in the held pane's reader, a reading pane. f.look(f.held, "rank.zig", 12); const r = p.view().active; const pane = p.panes[r].?; try std.testing.expect(reading(p, pane)); switch (path) { // A 9P write to its body. .body => try std.testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(pane.serial, .body), "changed\n").reply.status), // An Edit. .edit => try std.testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(pane.serial, .ctl), "Edit 3a/ changed/\n").reply.status), // A pipe of its line, answered. .pipe => { while (p.nextEffect()) |_| {} typeKeys(p, "x|"); p.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } }); p.update(.{ .key = .{ .cp = Key.enter } }); var id: ?u32 = null; while (p.nextEffect()) |e| switch (e) { .pipe => |asked| id = asked.id, else => {}, }; p.update(.{ .pipe_resp = .{ .id = id.?, .success = true, .outputs = &.{"CHANGED\n"} } }); }, // A language server's edit (a code action's records). .lsp => try std.testing.expect(@import("lsp/Ask.zig").applyAction(p, pane, pane.file.?.revision, "@put 0 0 changed%0A\n")), // A paste. .paste => p.update(.{ .paste = "changed\n" }), // Typed under `Jumps new-pane`, where an edit holds nothing, // the session then walking. .typed_unwalked => { p.settings.jumps = .@"new-pane"; typeKeys(p, "ichanged"); p.update(.{ .key = .{ .cp = Key.escape } }); p.settings.jumps = .walk; }, // A new text put in as a load (File.load holds no pane: the // Edit command's way before it said Trail.edited). .load => panes.File.load(p, &pane.file.?, try p.gpa.dupe(u8, "changed\n")), } const text = try std.testing.allocator.dupe(u8, pane.file.?.content); defer std.testing.allocator.free(text); try std.testing.expect(std.mem.indexOf(u8, text, "changed") != null or std.mem.indexOf(u8, text, "CHANGED") != null); try std.testing.expect(pardes.tagline.dirty(pane)); // A Look from it at another file. f.look(r, "place.zig", 8); var buf: [64]u8 = undefined; try std.testing.expectEqualStrings("place.zig:8", f.at(&buf)); const reader_pane = f.active(); // rank.zig still open with its change, held; place.zig in its reader. var kept: ?*Pane = null; for (p.panes) |slot| if (slot) |other| if (other.file) |of| if (std.mem.endsWith(u8, of.path, "/rank.zig")) { kept = other; }; if (kept == null) { std.debug.print("{t}: the changed rank.zig was closed by the step\n", .{path}); lost += 1; continue; } try std.testing.expectEqualStrings(text, kept.?.file.?.content); try std.testing.expect(kept.?.trail.held and kept.? != reader_pane); try std.testing.expectEqual(kept.?.serial, reader_pane.trail.steps[0].owner); } try std.testing.expectEqual(@as(usize, 0), lost); // Its owner closed: a reader with unsaved changes is held, never closed // with it (the tether, SPEC §11, closes a reader that only read). var f: Fixture = .{ .p = undefined, .tmp = undefined }; try f.init(60); defer f.deinit(); f.look(f.held, "rank.zig", 12); const r = f.p.view().active; panes.File.load(f.p, &f.p.panes[r].?.file.?, try f.p.gpa.dupe(u8, "changed\n")); try f.p.removePane(f.held, null); const kept = f.p.panes[r] orelse return error.TheChangedReaderWasClosed; try std.testing.expectEqualStrings("changed\n", kept.file.?.content); try std.testing.expect(kept.trail.held); } test "the fold takes places alone (UX L'1): a list's and a search's words are never in a `+N`, a run of places between them each its own; where the tag keeps its newest step alone, older lists fold with the places, the newest list never" { const Make = struct { fn place(line: u32) Step { var at: Place = .{ .line = line, .col = 1 }; at.path.set("/x/a.zig"); return .{ .place = at }; } fn list(text: []const u8) Step { var w: Word = .{}; w.text.set(text); return .{ .list = w }; } fn written(t: *const Trail, part: Part, whole: usize, out: []u8) ![]const u8 { var w: std.Io.Writer = .fixed(out); try t.write(part, whole, &w); return w.buffered(); } }; var out: [1024]u8 = undefined; var t: Trail = .{}; for ([_]Step{ Make.place(1), Make.place(2), Make.list("A 1/2"), Make.place(3), Make.place(4), Make.place(5), Make.place(6) }) |s| push(&t, s); t.cur = 6; try std.testing.expectEqualStrings("+2 › A 1/2 › a.zig:3 › a.zig:4 › a.zig:5 › ", try Make.written(&t, .back, 3, &out)); try std.testing.expectEqualStrings("+2 › A 1/2 › +2 › a.zig:5 › ", try Make.written(&t, .back, 1, &out)); // Each `+N` is its run's: a click on the second goes to its newest. try std.testing.expectEqual(@as(?u8, 1), t.stepAt(.back, 1, 0)); try std.testing.expectEqual(@as(?u8, 4), t.stepAt(.back, 1, "+2 › A 1/2 › ".len)); try std.testing.expectEqualDeep([2]usize{ "+2 › A 1/2 › ".len, "+2 › A 1/2 › +2".len }, t.spanOf(3, 1)); try std.testing.expect(t.isFolded(4, 1) and !t.isFolded(2, 1) and !t.isFolded(5, 1)); // Two lists: with the newest step alone the older folds, the newer not. t = .{}; for ([_]Step{ Make.place(1), Make.list("A 1/2"), Make.place(2), Make.list("B 1/1"), Make.place(3), Make.place(4) }) |s| push(&t, s); t.cur = 5; try std.testing.expectEqualStrings("+3 › B 1/1 › a.zig:3 › ", try Make.written(&t, .back, 1, &out)); try std.testing.expectEqualStrings("+1 › A 1/2 › a.zig:2 › B 1/1 › a.zig:3 › ", try Make.written(&t, .back, 3, &out)); // A search's word never folds; forward, the places past two alone. t = .{}; var s: Search = .{}; s.text.set("/s/ 1/1"); for ([_]Step{ Make.place(1), Make.place(2), .{ .search = s }, Make.place(3), Make.place(4), Make.place(5), Make.list("C 1/1"), Make.place(6), Make.place(7) }) |x| push(&t, x); t.cur = 4; try std.testing.expectEqualStrings("+2 › /s/ 1/1 › a.zig:3 › ", try Make.written(&t, .back, 1, &out)); try std.testing.expectEqualStrings(" › a.zig:5 › C 1/1 › +2", try Make.written(&t, .forward, 1, &out)); } test "a held pane walks its own words (UX L'1): Ctrl-o onto its search's word puts the caret where the search started, its places lit; past it, the jump list's" { var f: Fixture = .{ .p = undefined, .tmp = undefined }; try f.init(60); defer f.deinit(); const p = f.p; const held = p.panes[f.held].?; pardes.look.focusPaneLine(p, f.held, .{ .line = 20, .col = 3 }, .center); typeKeys(p, "/line 9"); p.update(.{ .key = .{ .cp = pardes.Key.enter } }); p.sync(); try std.testing.expectEqual(@as(i32, 89), held.body.cur_row); try std.testing.expect(held.trail.n == 1 and held.trail.cur == 1 and held.trail.steps[0] == .search); try std.testing.expectEqualStrings("C-o back to /line 9/ 2/11", p.messages.headline().?.words()); held.places.clear(p.gpa); p.update(.{ .key = .{ .cp = 'o', .ctrl = true } }); p.sync(); try std.testing.expectEqual(@as(usize, f.held), p.view().active); try std.testing.expectEqual(@as(i32, 19), held.body.cur_row); try std.testing.expectEqual(@as(i32, 2), held.body.cur_col); try std.testing.expectEqual(@as(u8, 0), held.trail.cur); try std.testing.expect(held.places.shows(held) and std.mem.eql(u8, held.places.pattern, "line 9")); // Its word still before its path, the search's. try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, held), "/line 9/ ")); try std.testing.expect(!walkBy(p, f.held, -1)); }