//! 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 Pardes = pardes.Pardes; const Pane = panes.Pane; const Spot = look.Spot; 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, pub fn lastCur(t: *const Trail) usize { return t.n -| 1; } /// 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 by the pickers (not yet). list: Word, /// A search made in the pane (`/dirty/ 3/7`, §7.3). Made by the /// prompts (not yet). search: Word, /// A chain of selection prompts as an Edit line (§7.5). Made by the /// prompts (not yet). 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 }; /// 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. pub fn reading(p: *const Pardes, pane: *const Pane) bool { return p.settings.jumps == .walk and isFile(pane) and !pane.trail.held; } /// 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.cur]) |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). pub fn edited(p: *Pardes, f: *const panes.File.State) void { 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); } } 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, its tag in the reading tint with Keep after its path; 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.chromeTheme().tag_reading_bg }, try tagGroundOf(p, &arena, 0)); // 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 Save Term Collapse Follow Del", try tagOf(p, pane)); try std.testing.expectEqual(pardes.Color{ .rgb = p.chromeTheme().tag_active_bg }, try tagGroundOf(p, &arena, 0)); 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.chromeTheme().tag_active_bg }, try tagGroundOf(p, &arena, 0)); } 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)); }