From 3ace8b385e02a54830decb6aa48d1fa28d076a18 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 26 Nov 2026 00:04:32 -0300 Subject: Trail, Pick, Prompt, Jump: Ctrl-o pops the walk one word at a time (UX DESIGN L'1, binding; the user: "I did spc f f then did /something but I didn't know how to get back to the previous one", "the pickers are kinda shown as a stack right? I felt like I could pop the current one"): the trail's words, left to right, are the stack, and Ctrl-o steps one word left of where the pane is, Ctrl-i one right; onto a place as before (Trail.go); onto a list's word, that kept picker unrolls again in this pane's tag as it was left (query, cursor, marks), previewing here (Pick.reopen with `walking`, its step): Esc gives rest exactly as before the Ctrl-o, Enter lands as that word's (a step from it, the steps after it dropped, its word saying where it is now), Ctrl-o, Ctrl-i and Backspace on its empty field (after its refinements) roll it up unlanded and walk on (Pick.walkOn; past the last word the jump list's step alone, Jump.listBy); Ctrl-i in it is Tab, a mark, where the terminal cannot tell them apart; onto a search's word, the caret back where the search started and its places lit (Prompt.light, Look's without the select): `Step.search` is a `Search` that holds its start, path, line and column, recorded as `/` commits (Prompt.word from Places.origin) and by `*`; the pane is then at that word (`cur` on it: Trail.backs writes it before the path, n and N walk it, keep keeps it, a step from it keeps it, go and replaceStep never write a place over it); a held pane walks its own words, a search's and a recipe's; a list no longer kept is said (`walk: that list is gone: the last 8 are kept`) and passed by; the owner is no word to walk; Jumps new-pane walks nothing, as before; D8's fold takes places only: each run of places older than the newest `whole` back steps (or past the first two forward) is one `+N` where it starts, a list's word folds only where the tag keeps its newest step alone and never the newest list's, a search's or a recipe's never (Trail.folds; draw.zig dims every run's `+N`); after a landing (Pick.listed) and a search's Enter (Prompt.land) the notice row says, as an echo never logged, what the next Ctrl-o reaches, `C-o back to Find words 1/1` (Trail.sayBack); THE CAUSE of the user's trouble (UX L1): Trail.walkBy skipped every word but a place (`if (t.steps[i] != .place) continue;`), so Ctrl-o after the search went to main.zig:1, past both the search and the list, and nothing said which key would pop; RUN (TEST FIRST): Jump.zig "Ctrl-o pops the walk one word at a time (UX L'1) ..." (W2's test extended) types the user's keys in an 84x20 session of main.zig (SPC f f words, Enter, /in_word, Enter, then Ctrl-o x3 and Ctrl-i x3) and asks after each key the file, the trail's four steps and where the pane is among them, the caret's line, Find words unrolled or not and the search lit or not, then Esc from a reopened list (as before the Ctrl-o), its Enter (the search's word and the place after it dropped, `Find words 1/1` kept) and Backspace on its empty field (walks on to main.zig): fails on the base at Jump.zig:565 on the first Ctrl-o (expected words.zig, found main.zig), passes now; Trail.zig "the fold takes places alone (UX L'1) ..." (runs of places each a `+N`, a click on each its newest, two lists with the newest step alone, a search's word never) and "a held pane walks its own words (UX L'1) ..." (Ctrl-o puts the caret at 20:3 where `/line 9` began, its places lit; past it the jump list's); the notice-row test of walk UI G 2e now reads the echo, then the caret's place once it is retired; a user hit it on every Ctrl-o after a picker's Enter or a search; goldens re-recorded: find and fsearch (the notice row after a picker's Enter and a search's Enter reads the echo); ttyhelp is the merge's placeholder (M1 re-records it); the perf cell walk-pop (test/perf.zig, run only when named: Ctrl-o onto a list word reopening it in the key's own frame, then Esc), 326 us a pair on small in ReleaseFast; core-test 1124/1298 (173 skipped; the G 2e notice-row test, failing on the echo, updated and passing alone), shell-test 1346/1349 (3 skipped), behaviour-test 280/280, pane-test 183/184, process-test 3 of 147 failed before the two goldens moved (find, fsearch: the echo; ttyhelp the placeholder); load 2.2 to 7.2 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE --- core/Trail.zig | 295 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 242 insertions(+), 53 deletions(-) (limited to 'core/Trail.zig') diff --git a/core/Trail.zig b/core/Trail.zig index 2a37d68a..cd8eb924 100644 --- a/core/Trail.zig +++ b/core/Trail.zig @@ -57,15 +57,17 @@ pub const Step = union(enum) { /// 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). Made by a search - /// prompt (Prompt.word). - search: 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). @@ -157,8 +159,11 @@ fn push(t: *Trail, s: Step) void { 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 (here(pane)) |was| if (samePlace(was, path, at.line)) return show(p, id, path, at); + 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; @@ -166,7 +171,7 @@ pub fn step(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { if (here(pane)) |was| { if (t.n > t.cur) { t.n = t.cur + 1; - t.steps[t.cur] = was; + if (!word) t.steps[t.cur] = was; } else push(t, was); } else t.n = t.cur; push(t, to); @@ -175,19 +180,27 @@ pub fn step(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { } /// Go to step `i` of reading pane `id`'s trail, back or forward (§6.4): -/// where it is now becomes the step it leaves, the ones between stay. +/// 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]; - if (t.n > t.cur) if (here(pane)) |was| { - t.steps[t.cur] = was; - }; + 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 { @@ -210,7 +223,7 @@ pub fn through(t: *Trail, s: Step) void { 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) t.steps[t.cur] = to; + if (t.cur < t.n and t.steps[t.cur] == .place) t.steps[t.cur] = to; return show(p, id, path, at); } @@ -327,7 +340,7 @@ pub fn keep(pane: *Pane) void { if (t.held) return; t.held = true; var w: u8 = 0; - for (t.steps[0..t.cur]) |s| if (s == .search or s == .recipe) { + for (t.steps[0..t.backs()]) |s| if (s == .search or s == .recipe) { t.steps[w] = s; w += 1; }; @@ -350,23 +363,96 @@ pub fn edited(p: *Pardes, f: *const panes.File.State) void { } -/// Back (`delta` < 0) or forward one place in reading pane `id`'s walk, -/// as Ctrl-o and Ctrl-i go first (pick-ui SPEC §6.4): true when it had -/// one that way, false to leave the step to the jump list (Jump.jumpBy). +/// 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; - if (!reading(p, pane)) 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 i: usize = t.cur; - while (if (delta < 0) i > 0 else i + 1 < t.n) { - i = if (delta < 0) i - 1 else i + 1; - if (t.steps[i] != .place) continue; - _ = go(p, id, @intCast(i)) catch |err| p.reportError(.at(id), "walk", err); + 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 ---- /// `newest`: out of rest (UX D1), the owner's word and the newest back @@ -387,7 +473,8 @@ fn wordOf(t: *const Trail, i: usize, buf: *[max_text + 16]u8) []const u8 { 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(), + .list, .recipe => |*w| w.text.slice(), + .search => |*s| s.text.slice(), }; } @@ -406,10 +493,17 @@ fn shared(t: *const Trail, i: usize, end: []const u8) bool { 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, .newest => .{ 0, t.cur }, + .back, .newest => .{ 0, t.backs() }, .forward => .{ @min(t.n, t.cur + 1), t.n }, }; } @@ -417,20 +511,34 @@ fn range(t: *const Trail, part: Part) [2]usize { /// 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 { +/// 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) { - .back => back: { - const lo: usize = @intFromBool(t.cur > 0 and t.steps[0] == .owner); - break :back .{ lo, @max(lo, t.cur -| whole) }; + .newest => false, + .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, }, - .forward => .{ @min(t.n, t.cur + 1 + shown_ahead), t.n }, - .newest => .{ 0, 0 }, }; } +/// 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; +} + /// 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; @@ -440,9 +548,13 @@ const newest_cells = 24; /// 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; + if (part == .newest and t.steps[i] != .owner and i + 1 != t.backs()) return null; + 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 + } 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 @@ -455,7 +567,7 @@ fn wordIn(t: *const Trail, part: Part, whole: usize, i: usize, buf: *[max_text + /// 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 folded ones as one `+N` (`foldOf`); out of rest (`newest`) the +/// each run of folded ones as one `+N` (`folds`); 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; @@ -482,7 +594,7 @@ pub fn bytes(t: *const Trail, part: Part, whole: usize) usize { /// 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; + var i: usize = t.backs(); while (i > 0) { i -= 1; if (t.steps[i] == .list or t.steps[i] == .search) return @intCast(i); @@ -503,21 +615,18 @@ pub fn outOfRest(p: *const Pardes, pane: *const Pane) bool { 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; + const at = if (folds(t, .back, whole, i)) runStart(t, .back, whole, i) else i; var x: usize = 0; - for (0..i) |j| if (wordIn(t, .back, whole, j, &buf, &cut)) |word| { + for (0..at) |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 ""; + const word: []const u8 = wordIn(t, .back, whole, at, &buf, &cut) orelse ""; return .{ x, x + word.len }; } -/// Whether back step `i` is in the fold's `+N`. +/// Whether back step `i` is in a 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]; + return folds(t, .back, whole, i); } /// The step whose word in `part` is at byte `off` of what `write` wrote @@ -527,12 +636,17 @@ pub fn stepAt(t: *const Trail, part: Part, whole: usize, off: usize) ?u8 { 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; // 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); + 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; } @@ -550,7 +664,7 @@ pub fn lookStep(p: *Pardes, id: usize, i: u8) void { .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), + .list => |w| pardes.Pick.reopen(p, id, w.index, null), .search => pardes.Prompt.lookWord(p, id), .recipe => pardes.Prompt.lookRecipe(p, id), } @@ -867,14 +981,18 @@ test "the walk drawn with a picker's list and the notice row (walk UI G 2e, SPEC var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); // The reader's tag in the reading tint, the held one's not; the notice - // row, nothing said, where the reader's caret is. + // 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.chromeTheme().tag_reading_bg }, try tagGroundOf(p, &arena, rd)); try std.testing.expect(!std.meta.eql(pardes.Color{ .rgb = p.chromeTheme().tag_reading_bg }, try tagGroundOf(p, &arena, f.held))); - 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, try std.fmt.bufPrint(&want, "{s}/place.zig:8", .{f.dir})) != null); + 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(); @@ -1320,3 +1438,74 @@ test "a walk step never closes a file with unsaved changes: a reading pane chang 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)); +} -- cgit v1.3 From 280306ed779368d7e20e9678eca45e4b81e0ccbf Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 26 Nov 2026 03:03:20 -0300 Subject: Pick, Kind, look, draw, mouse: a picker previews a file an addon's kind takes through that kind in the pane it lands in, a binary no kind takes as one line naming it, and Enter on such a row lands as a Look does, never the file's bytes as text (the user: "the file preview wasn't working for images (maybe pdfs too? Didn't test)", "It should be a generic logic for addons btw, and I saw the bytes rendered as ascii"; the coordinator's W7 under SPEC principle 2); REPRODUCED with the installed build (install74, the S kit, 26 px): SPC f f from a reading main.zig, the cursor on photo.png left main.zig in the body (the previous row's file), on paper.pdf showed `%PDF-1.7 %âãÏÓ 1 0 obj …` as text (a PDF has no NUL in its first 8000 bytes, so Pick.preview's binary guard let it through), and Enter on photo.png put `�PNG … IHDR …` in the reading pane: THE BYTES-AS-TEXT PATH the user saw: Pick.land → look.open → walk → Trail.step → Trail.show → File.open, which reads any path as a text (look.resolve sends a typed or clicked `photo.png` to Kind.openFile, but look.open, every jump's way, had no kind route); THE FIX: look.open routes a path a kind takes (`kinds.forPath`, the routing resolve uses) to openKind, lookAt's kind branch made a function (the pane of it focused, else opened and placed as a Look places it), so a picker's Enter, the Walk queue and a held pane's step land as a Look; Find's row of a kind's file is the file whole (a place in it is its kind's address: line 1 made the image refuse it); Pick.preview: a file a kind takes is shown by a slot of that kind over the landing pane's text (Kind.newSlot, split from Kind.open; Pick.showKind), which stays under it unseen until the next row or the roll-up closes the slot (putBack, restore: Esc exact, nothing left), no per-kind code; any other file with a NUL is one read-only line, `blob.bin · binary · 3.0 kB` (Pick.nameLine); until the kind draws (its paint places nothing yet) the body shows `photo.png · image · 24.8 kB` (draw.zig; Kind.draw says whether it painted); a refused one is that line with its kind's why; the kind does its own work as for its pane (the image decoded on a worker); only the row the cursor rests on at a frame opens (the preview's existing tick rule), and a draw of a row passed by is dropped when it lands (Kind.Slot.drawn_from: this process's draw count when the slot was made; Kind.deliver drops older answers, which went to the next slot of the same kind on the same pane); a previewed kind says, looks at, runs and lists nothing (Kind.Call.end returns before them while Pick.previewing) and takes no mouse (mouse.kindMouse); RUN (TEST FIRST): test/behaviour/addons.zig, four tests through 9P with the fake addon (addons/fake: no kind code in Pick) and the image addon, and addons_pdf.zig one with the pdf addon: a `.fake` file previews as the fake's paint (`count 7`) and not its bytes, Esc puts the screen and /index back, Enter opens the fake's pane with the reading pane's text kept; blob.bin's one line, never other.txt's text after it, a kind's paint after the binary's; three pictures, two rows down with no frame between: one draw held per rested row (2, not 3), and the rested picture shown after the passed one's answer came last; Enter on a picture opens the image pane, the reading pane `the notes`; a PDF's pages as pictures in the landing pane, none after Esc; Session.tick (a host frame) added, as previews wait for the frame after the key; on the base they fail (the bytes `1234567` shown, no line, no image pane, `%PDF` on the screen): core-test 1128/1301 (173 skipped; the kept_out list names Kind.Slot's drawn_from and size), shell-test 1349/1352 (3 skipped), pane-test 183/184, behaviour-test 285/285 (+5) and --replay 285/285, process-test 0 of 147 failed (3 skipped), term-test 17/17, kitty-test 45/45; the gui unit-test on the stack's top (W8); no dump change (the two Slot fields are local, out of the digest and the dump); the S kit with this change's Debug gui at 26 px: the PNG and 9p.pdf previewing in the landing pane, blob.bin's line, Enter on photo.png an `img` pane with main.zig kept (.scratch/walkui/W78/png a02-a05, before r03 r05); no frame or perf gate (the user's rule of 2026-11-26); load 3.7 to 10 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE --- core/Kind.zig | 55 ++++++++++++---- core/Pick.zig | 132 +++++++++++++++++++++++++++++--------- core/Trail.zig | 4 +- core/draw.zig | 9 ++- core/look.zig | 54 ++++++++++------ core/mouse.zig | 3 +- core/root.zig | 1 + test/behaviour/addons.zig | 146 ++++++++++++++++++++++++++++++++++++++++++ test/behaviour/addons_pdf.zig | 28 ++++++++ test/session.zig | 8 +++ 10 files changed, 373 insertions(+), 67 deletions(-) (limited to 'core/Trail.zig') diff --git a/core/Kind.zig b/core/Kind.zig index 4e1aa469..e220ead7 100644 --- a/core/Kind.zig +++ b/core/Kind.zig @@ -137,7 +137,7 @@ pub const Slot = struct { // jobs' inputs, its file's hash, a placeholder's record), and what // a frame makes (the thumb, the pointer, whether one was drawn: a // view's, never a step's). - .local = .{ "ops", "st", "gpa", "arena", "plain", "file", "opened", "reloads", "jobs", "record", "thumb_at", "thumb_span", "pointer", "painted", "over", "used" }, + .local = .{ "ops", "st", "gpa", "arena", "plain", "file", "opened", "reloads", "jobs", "record", "thumb_at", "thumb_span", "pointer", "painted", "over", "used", "drawn_from", "size" }, }; /// Its kind's code here; null in a placeholder (`live` false: its kind /// not in the registry, its record kept for the day it comes) or a @@ -215,6 +215,13 @@ pub const Slot = struct { /// A client's join: its plain kind's file, until the pane is opened /// from it (dump.plainHere). file: ?[]u8 = null, + /// This process's draws made before it (`drawn` then): an answer to + /// one of them is another slot's, of a pane that showed something + /// else (a picker's preview of the row before), and is dropped. + drawn_from: u32 = 0, + /// Its file's bytes, as a picker's line naming it says them until its + /// kind's picture is drawn (Pick.preview). + size: u64 = 0, /// Debug: the kind's allocations, which paint must leave as they are. /// The slot's allocator's, as the slot is: a client's rollback puts /// the list back with the slot that holds it (on the page allocator, @@ -259,16 +266,12 @@ fn caps(p: *const Pardes, s: *const Slot) Caps { /// word opened) from `bytes`: made whatever the kind says, with what it /// refused in `slot.refused` for the caller to say. pub fn open(p: *Pardes, id: usize, k: u8, path: ?[]const u8, bytes: ?[]const u8) !*Pane { - const e = &p.kinds.kinds[k]; - // A plain kind's slot and block are the core's (a client's epoch). - const gpa = if (e.caps.plain) p.gpa else p.image_gpa; - const s = try gpa.create(Slot); - s.* = .{ .ops = p.kinds.ops[k], .live = true, .gpa = gpa, .arena = .init(gpa), .version = e.version, .gen = e.gen, .name = e.name }; + const s = try newSlot(p, k, path orelse ""); errdefer { + const gpa = s.gpa; s.free(); gpa.destroy(s); } - s.path = try gpa.dupe(u8, path orelse ""); const pane = try p.newDocPane(id); pane.addon = s; _ = reopen(p, id, s, bytes); @@ -278,6 +281,23 @@ pub fn open(p: *Pardes, id: usize, k: u8, path: ?[]const u8, bytes: ?[]const u8) return pane; } +/// A slot of kind `k` for `path`, its kind's state not made yet (`reopen` +/// makes it): a pane's, or a picker's preview's in the pane it lands in +/// (Pick.preview), that pane's own text set aside meanwhile. +pub fn newSlot(p: *Pardes, k: u8, path: []const u8) !*Slot { + const e = &p.kinds.kinds[k]; + // A plain kind's slot and block are the core's (a client's epoch). + const gpa = if (e.caps.plain) p.gpa else p.image_gpa; + const s = try gpa.create(Slot); + s.* = .{ .ops = p.kinds.ops[k], .live = true, .gpa = gpa, .arena = .init(gpa), .version = e.version, .gen = e.gen, .name = e.name, .drawn_from = drawn }; + errdefer { + s.free(); + gpa.destroy(s); + } + s.path = try gpa.dupe(u8, path); + return s; +} + /// `open` of the file at `path`, read here (a file that cannot be read /// is no bytes: its kind may refuse it). The slot stays ours across the /// read: another request may make a pane meanwhile. @@ -418,9 +438,10 @@ pub fn close(p: *Pardes, pane: *Pane) void { /// The pane's body painted over the `cols` x `rows` cells at (x, y): /// its cells into `s`, its placements after the frame's others. A pane /// whose kind has no code here, or a placeholder, says why in a line. -pub fn draw(p: *Pardes, s: *surface.Surface, arena: std.mem.Allocator, pane: *Pane, id: usize, x: u16, y: u16, cols: u16, rows: u16) void { - const slot = pane.addon orelse return; - if (cols == 0 or rows == 0) return; +/// False when its kind painted nothing (its picture not drawn yet). +pub fn draw(p: *Pardes, s: *surface.Surface, arena: std.mem.Allocator, pane: *Pane, id: usize, x: u16, y: u16, cols: u16, rows: u16) bool { + const slot = pane.addon orelse return true; + if (cols == 0 or rows == 0) return true; const th = p.theme(); const fg: surface.Color = if (th.fg) |f| .{ .rgb = f } else .default; const bg: surface.Color = if (th.bg) |b| .{ .rgb = b } else .default; @@ -431,17 +452,17 @@ pub fn draw(p: *Pardes, s: *surface.Surface, arena: std.mem.Allocator, pane: *Pa const why = if (caps(p, slot).plain) "it is not in its addons nor the session's library at its path here, or it broke its promise here" else "its state is not plain (PA_PLAIN), so it runs in the session alone"; var line: [200]u8 = undefined; _ = s.print(x, y, cols, std.fmt.bufPrint(&line, "addon {s} {d} does not run in this client: {s}", .{ slot.nameOf(), slot.version, why }) catch &line, .{ .fg = fg, .bg = bg }); - return; + return true; } else if (slot.ops == null) { var line: [160]u8 = undefined; const why = if (slot.refused.len > 0) slot.refused.slice() else "Addon load "; // Its kind here, it waits for its file (` is not here`). const text = if (p.kinds.byName(slot.nameOf()) != null) why else std.fmt.bufPrint(&line, "addon {s} {d} is not loaded: {s}", .{ slot.nameOf(), slot.version, why }) catch &line; _ = s.print(x, y, cols, text, .{ .fg = fg, .bg = bg }); - return; + return true; } else for (0..8) |_| { if (ready.items.len > 0) deliver(p, slot, pane, id); - frame.cells = (paint(p, slot, id, arena, cols, rows, &frame) orelse return); + frame.cells = (paint(p, slot, id, arena, cols, rows, &frame) orelse return true); // No workers here (a test's, a bench's core): a draw that paint // asked was done at once, and is this frame's, painted again. if (drawer != null) break; @@ -450,9 +471,11 @@ pub fn draw(p: *Pardes, s: *surface.Surface, arena: std.mem.Allocator, pane: *Pa } else break; } if (frame.gpu.len != 0) s.gpu_frames[id] = .{ .bytes = frame.gpu, .serial = pane.serial, .dst = .{ .x = x, .y = y, .w = cols, .h = rows } }; + var painted = frame.gpu.len != 0 or frame.n > 0; for (0..@min(rows, frame.rows)) |r| for (0..@min(cols, frame.cols)) |col| { const cell = &frame.cells[r * frame.cols + col]; if (cell.flags & c.PA_PAINTED == 0) continue; + painted = true; const len = @min(cell.len, 7); s.set(x + @as(u16, @intCast(col)), y + @as(u16, @intCast(r)), if (len == 0) " " else cell.text[0..len], styleOf(cell, fg, bg)); }; @@ -471,6 +494,7 @@ pub fn draw(p: *Pardes, s: *surface.Surface, arena: std.mem.Allocator, pane: *Pa .art = slot.art, }); } + return painted; } /// A frame of a kind's pane: its cells, `cols` a row, and its placements. @@ -1012,6 +1036,9 @@ const Call = struct { return o; }; const p = cl.p orelse return o; + // A picker's preview shows its file and does nothing more: what its + // kind says, looks at or runs meanwhile is dropped (Pick.showKind). + if (pardes.Pick.previewing(p, cl.id)) return o; if (o.said.len > 0) if (o.err) p.reportFailure(cl.id, o.said) else p.setMessage(.at(cl.id), o.said); if (o.pane_name.len > 0) textPane(p, cl.id, cl.slot, o.pane_name, o.pane_text); if (o.look.len > 0) @import("look.zig").lookAt(p, cl.id, o.look); @@ -1289,7 +1316,7 @@ fn deliver(p: *const Pardes, slot: *Slot, pane: *const Pane, id: usize) void { } _ = ready.orderedRemove(i); defer j.drop(); - if (pane.serial != j.serial or slot.gen != j.gen or slot.ops != j.ops) continue; + if (pane.serial != j.serial or slot.gen != j.gen or slot.ops != j.ops or j.token & ~c.PA_DRAWN <= slot.drawn_from) continue; var cl: Call = .{ .p = null, .id = id, .slot = slot, .quiet = true, .core = p }; const prev = cl.begin("job_done"); const before = if (checked) usedHash(slot); diff --git a/core/Pick.zig b/core/Pick.zig index 1d332f25..f13278af 100644 --- a/core/Pick.zig +++ b/core/Pick.zig @@ -639,7 +639,10 @@ fn rowAt(p: *Pardes, pk: *const Pick, arena: std.mem.Allocator, i: usize) std.me var namebuf: [4096]u8 = undefined; const name = if (text.len > 1 and text[0] == '"' and text[text.len - 1] == '"') look.unquoted(&namebuf, text[1 .. text.len - 1]) else text; row.faint = @intCast(if (std.mem.lastIndexOfScalar(u8, text, '/')) |k| k + 1 else 0); - row.target = .{ .place = .{ .path = try std.Io.Dir.path.join(arena, &.{ dir, name }), .at = .{ .line = 1, .col = 1 } } }; + // A text at its first line; a kind's file whole, as a Look of + // its name opens it (a place in it is its kind's address). + const at: look.Spot = if (p.kinds.forPath(name) != null) .{} else .{ .line = 1, .col = 1 }; + row.target = .{ .place = .{ .path = try std.Io.Dir.path.join(arena, &.{ dir, name }), .at = at } }; }, .panes => { row.pre = cand.key -| 1; @@ -925,7 +928,11 @@ pub fn sentence(p: *const Pardes, w: *std.Io.Writer) ?[2]usize { /// The landing pane's body showing the current row's place (SPEC §5.3): /// a file at its line, the query's matches lit for a Grep. Its own text is /// set aside the first time, put back when the picker rolls up. A file an -/// open pane shows is taken from it; any other is read. +/// open pane shows is taken from it; any other is read. A file a kind +/// takes (an addon's: a picture, a PDF), as a Look would open it +/// (look.resolve), shows through that kind (showKind); any other that is +/// not text (a NUL in its first 8000 bytes) as one line naming it, +/// `blob.bin · binary · 3.0 kB`, never its bytes. fn preview(p: *Pardes, pk: *Pick) void { const s = &p.view().tag_state; // No row yet: none shown, so the first to come is (a Find's listing @@ -944,40 +951,22 @@ fn preview(p: *Pardes, pk: *Pick) void { const pane = p.panes[id].?; const f = if (pane.file) |*file| file else return; if (f.twin != 0) return; - if (!std.mem.eql(u8, f.path, place.path)) { + const showing = if (pane.addon) |k| k.path else f.path; + if (!std.mem.eql(u8, showing, place.path)) { if (exec.kindOf(p.tape, place.path)) |kind| if (kind != .file) return; - const bytes = openText(p, place.path) orelse (p.tape.site(.preview, p.gpa, filesystem.read, .{ p, place.path }) catch return); - if (std.mem.indexOfScalar(u8, bytes[0..@min(bytes.len, 8000)], 0) != null) { - p.gpa.free(bytes); - return; + const read = openText(p, place.path) orelse (p.tape.site(.preview, p.gpa, filesystem.read, .{ p, place.path }) catch return); + if (pane.addon != null) Kind.close(p, pane); + if (p.kinds.forPath(place.path)) |k| { + defer p.gpa.free(read); + return showKind(p, pk, id, k, place.path, read); } - const path = p.gpa.dupe(u8, place.path) catch { - p.gpa.free(bytes); - return; + const bytes = if (std.mem.indexOfScalar(u8, read[0..@min(read.len, 8000)], 0) == null) read else named: { + defer p.gpa.free(read); + break :named nameLine(p.gpa, place.path, "binary", read.len, "") catch return; }; - if (pk.aside == null and !pk.made) { - const history = File.History.create(p.gpa) catch { - p.gpa.free(bytes); - p.gpa.free(path); - return; - }; - pk.aside = pane.file; - pane.file = .{ .path = path, .content = bytes, .output = .{ .from = .preview }, .history = history }; - } else { - const pf = &pane.file.?; - p.gpa.free(pf.path); - pf.path = path; - File.setContent(p, pf, bytes); - pf.saved_revision = pf.revision; - pf.scroll = 0; - pf.scroll_row = 0; - pf.syntax_dirty = true; - pane.places.clear(p.gpa); - } - pane.hscroll = 0; - pk.previewed = true; - p.sync(); + showText(p, pk, id, place.path, bytes); } + if (pane.addon != null) return; // The body under the band from the first place it shows (previewing): // laid out again before the place is put in its rows. if (!pk.previewed) { @@ -1001,6 +990,81 @@ fn preview(p: *Pardes, pk: *Pick) void { if (pk.source == .grep) pane.places.search(p, pane, .list, pk.id, std.mem.trim(u8, s.field.text(), " "), true, null, false); } +/// Landing pane `id`'s text `bytes` (taken) as `path`: its own text set +/// aside the first time, a preview's text after that changed in place. +fn showText(p: *Pardes, pk: *Pick, id: usize, path_: []const u8, bytes: []u8) void { + const pane = p.panes[id].?; + const path = p.gpa.dupe(u8, path_) catch { + p.gpa.free(bytes); + return; + }; + if (pk.aside == null and !pk.made) { + const history = File.History.create(p.gpa) catch { + p.gpa.free(bytes); + p.gpa.free(path); + return; + }; + pk.aside = pane.file; + pane.file = .{ .path = path, .content = bytes, .output = .{ .from = .preview }, .history = history }; + } else { + const pf = &pane.file.?; + p.gpa.free(pf.path); + pf.path = path; + File.setContent(p, pf, bytes); + pf.saved_revision = pf.revision; + pf.scroll = 0; + pf.scroll_row = 0; + pf.syntax_dirty = true; + pane.places.clear(p.gpa); + } + pane.hscroll = 0; + pk.previewed = true; + p.sync(); +} + +/// Landing pane `id` showing the file at `path` through kind `k`, as a +/// Look would open it in a pane of its own: a slot of that kind over the +/// pane's text, which stays under it, unseen, until the next row or the +/// roll-up takes the slot away (Kind.close). Its kind does the work it +/// does for its own pane (an image decoded on a worker; a draw of a row +/// passed by dropped, Kind.Slot.drawn_from), and nothing more: what it +/// says, looks at or runs is not done while it previews (Kind.Call.end). +/// Until its picture is drawn, the line naming it (draw.zig); one its kind +/// refuses is that line as text, saying why. +fn showKind(p: *Pardes, pk: *Pick, id: usize, k: u8, path: []const u8, bytes: []const u8) void { + const pane = p.panes[id].?; + const slot = Kind.newSlot(p, k, path) catch |err| return p.reportError(.at(id), "pick", err); + slot.size = bytes.len; + pane.addon = slot; + pk.previewed = true; + if (!Kind.reopen(p, id, slot, bytes) and slot.refused.len > 0) { + const line = nameLine(p.gpa, path, slot.nameOf(), bytes.len, slot.refused.slice()) catch return Kind.close(p, pane); + Kind.close(p, pane); + return showText(p, pk, id, path, line); + } + p.sync(); +} + +/// The one line a file shows while a picker previews it and none of its +/// text is shown: its name, what it is (`binary`, a kind's name), its size +/// and why when it says why: `photo.png · image · 1.2 MB`. +pub fn nameLine(gpa: std.mem.Allocator, path: []const u8, what: []const u8, size: u64, why: []const u8) ![]u8 { + var out: std.Io.Writer.Allocating = .init(gpa); + errdefer out.deinit(); + const w = &out.writer; + try w.print("{s} · {s} · ", .{ std.Io.Dir.path.basename(path), what }); + if (size < 1000) try w.print("{d} B", .{size}) else { + const units = [_][]const u8{ "kB", "MB", "GB", "TB", "PB", "EB" }; + var unit: u64 = 1000; + var u: usize = 0; + while (size / unit >= 1000 and u + 1 < units.len) : (u += 1) unit *= 1000; + const tenths = size / (unit / 10); + try w.print("{d}.{d} {s}", .{ tenths / 10, tenths % 10, units[u] }); + } + if (why.len > 0) try w.print(" · {s}", .{why}); + return out.toOwnedSlice(); +} + /// The text of `path` as an open pane has it, copied: no read for it. fn openText(p: *Pardes, path: []const u8) ?[]u8 { for (p.panes) |slot| { @@ -1019,6 +1083,9 @@ fn restore(p: *Pardes, pk: *Pick) void { const id = p.paneBySerial(pk.landing) orelse return; for (p.panes) |slot| if (slot) |tp| if (tp.places.from == pk.id) tp.places.clear(p.gpa); if (pk.made) { + // A kind's file shown: its slot closed first, as a pane's text + // closing is no file closed (no Recent row). + if (p.panes[id].?.addon != null) Kind.close(p, p.panes[id].?); pk.made = false; p.removePane(id, p.paneBySerial(pk.owner)) catch |err| p.reportError(.at(id), "pick", err); return; @@ -1030,6 +1097,7 @@ fn restore(p: *Pardes, pk: *Pick) void { fn putBack(p: *Pardes, pk: *Pick) void { const id = p.paneBySerial(pk.landing) orelse return; const pane = p.panes[id].?; + if (pane.addon != null) Kind.close(p, pane); if (pk.made) return; const own = pk.aside orelse { // Its own file previewed: its caret and top as they were. diff --git a/core/Trail.zig b/core/Trail.zig index cd8eb924..9b0d64ea 100644 --- a/core/Trail.zig +++ b/core/Trail.zig @@ -250,7 +250,9 @@ fn show(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { .opened => |free| free, .shown, .none => return p.view().active, }; - if (at.line > panes.File.nlines(p.gpa, &p.panes[free].?.file.?)) { + // (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; diff --git a/core/draw.zig b/core/draw.zig index b666fc15..4d47f895 100644 --- a/core/draw.zig +++ b/core/draw.zig @@ -533,7 +533,14 @@ fn renderPane(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: // painted out. Text gets the overlay; a kind's pane gives up the rows. const shown = pane.notices.span(); const body_h = (r.h -| pane.tag_rows) -| shown; - pardes.Kind.draw(p, s, arena, pane, id, tx, body_y + shown, tw, body_h); + // A picker's preview through a kind (Pick.showKind): its file named + // until its kind has drawn it. + if (!pardes.Kind.draw(p, s, arena, pane, id, tx, body_y + shown, tw, body_h) and body_h > 0 and pardes.Pick.previewing(p, id)) { + const k = pane.addon.?; + if (pardes.Pick.nameLine(arena, k.path, k.nameOf(), k.size, "")) |line| { + _ = s.print(tx, body_y + shown, tw, line, .{ .fg = if (th.fg) |c| .{ .rgb = c } else .default, .bg = pane_bg }); + } else |_| {} + } // The same one column as the real scrollbar below, running past // the notice bands so the gutter has no notch in it; its thumb // where this frame's paint said its window is in its whole (a diff --git a/core/look.zig b/core/look.zig index 41c59cf3..c7e0feb9 100644 --- a/core/look.zig +++ b/core/look.zig @@ -1522,6 +1522,9 @@ pub const Landed = union(enum) { none, shown, opened: usize }; /// `Jumps new-pane` as before the walk: a pane showing the file focused, /// else a new one. pub fn open(p: *Pardes, from: usize, path: []const u8, at: Spot, how: How) Landed { + // A file a kind takes (an addon's: a picture, a PDF) is its kind's + // pane, as a Look of it opens (resolve), never its bytes as a text. + if (p.kinds.forPath(path)) |k| return openKind(p, from, path, k, at); if (how == .replace and p.settings.jumps == .walk and path.len <= Trail.max_text and p.panes[from] != null) return walk(p, from, path, at); if (how == .replace and focusPaneByPath(p, path, at)) return .shown; return if (openNew(p, from, path, at, .doc)) |free| .{ .opened = free } else .none; @@ -1557,6 +1560,38 @@ fn walk(p: *Pardes, from: usize, path: []const u8, at: Spot) Landed { return .shown; } +/// The file at `path`, of kind `kind`, looked at from pane `id`: the pane +/// of it there is focused, else one of its kind opened as a document is +/// placed (layout.placeNew), at `at` when it says a place. +fn openKind(p: *Pardes, id: usize, path: []const u8, kind: u8, at: Spot) Landed { + var place: [41]u8 = undefined; + if (focusPaneByPath(p, path, .{})) { + if (at.line > 0) lookKind(p, id, p.view().active, spotText(at, &place), false); + return .shown; + } + const free = p.freeSlot() orelse { + p.reportError(.at(id), "look", error.NoPaneSlots); + return .none; + }; + const made = Kind.openFile(p, free, kind, path) catch |err| { + lookOpenFailed(p, id, path, err); + return .none; + }; + // One its kind refuses (an image corrupt, truncated, not what its name + // says) is said, a failure, rather than shown blank. + if (made.addon.?.refused.len > 0) { + made.vweight = 0; + p.unplaced.set(free); + var said: Kind.Buf(miss_path_cap + 160) = .{}; + var cut: [miss_path_cap + "…".len]u8 = undefined; + p.reportFailure(id, said.print("look: {s}: {s}", .{ missText(path, &cut), made.addon.?.refused.slice() })); + return .none; + } + layout.placeNew(p, id, id, free, .doc); + if (at.line > 0) lookKind(p, id, free, spotText(at, &place), true); + return .{ .opened = free }; +} + /// `path` opened at `at` in a new pane placed as `kind` places one from /// `from`, its line half its body's rows down; null when it is not /// (refused, said). @@ -1808,24 +1843,7 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void { }; return missed(p, id, "{s}: the pane its list is of is gone", .{target.path}); } - if (focusPaneByPath(p, target.path, .{})) { - if (target.at.line > 0) lookKind(p, id, v.active, spotText(target.at, &place), false); - return; - } - const free = p.freeSlot() orelse return p.reportError(.at(id), "look", error.NoPaneSlots); - const made = Kind.openFile(p, free, target.kind, target.path) catch |err| - return lookOpenFailed(p, id, target.path, err); - // One its kind refuses (an image corrupt, truncated, not what - // its name says) is said, a failure, rather than shown blank. - if (made.addon.?.refused.len > 0) { - made.vweight = 0; - p.unplaced.set(free); - var said: Kind.Buf(miss_path_cap + 160) = .{}; - var cut: [miss_path_cap + "…".len]u8 = undefined; - return p.reportFailure(id, said.print("look: {s}: {s}", .{ missText(target.path, &cut), made.addon.?.refused.slice() })); - } - layout.placeNew(p, id, id, free, .doc); - if (target.at.line > 0) lookKind(p, id, free, spotText(target.at, &place), true); + _ = openKind(p, id, target.path, target.kind, target.at); }, } } diff --git a/core/mouse.zig b/core/mouse.zig index b62cec7e..37c3a97f 100644 --- a/core/mouse.zig +++ b/core/mouse.zig @@ -235,7 +235,8 @@ fn kindMouse(p: *Pardes, m: Mouse) bool { for (p.panes, 0..) |slot, id| { const pane = slot orelse continue; const k = pane.addon orelse continue; - if (!pardes.Kind.takesMouse(p, k) or pane.collapsed) continue; + // A picker's preview through a kind is shown, not handled. + if (!pardes.Kind.takesMouse(p, k) or pane.collapsed or pardes.Pick.previewing(p, id)) continue; const r = v.rects[id]; const top = p.bodyTop(pane, r) + pane.notices.span(); const x0 = r.x + config.GUTTER; diff --git a/core/root.zig b/core/root.zig index 11cb5058..8d5f8c40 100644 --- a/core/root.zig +++ b/core/root.zig @@ -791,6 +791,7 @@ const kept_out = [_]struct { []const u8, []const u8, []const u8 }{ .{ "Text.zig", "room", "a buffer's capacity" }, .{ "Kind.zig", "ops st gpa arena plain file opened reloads jobs record", "the kind's code and memory here: a plain kind's block is in the copy, and its state hash is checked at every call (Kind.Call.end)" }, .{ "Kind.zig", "thumb_at thumb_span pointer painted over used", "a frame's: written by a paint; `painted` is read by a tick of a kind that is not plain, whose calls are the authority's alone" }, + .{ "Kind.zig", "drawn_from size", "a picker's preview through a kind (Pick.showKind): this process's draw count when the slot was made, which drops the answers of draws made before it, and the file's size for the line naming it until its kind draws; both read by a frame alone" }, .{ "Places.zig", "ticks ticks_gen re", "the rail's ticks, made again when the places move (`gen`), and the compiled pattern, of the pattern" }, .{ "Plumb.zig", "parsed parsed_arena", "the rules parsed, of their text" }, .{ "Stream.zig", "parse_ns paint paint_retired paint_count ahead ahead_for painted_ahead painted_on_loop ahead_seen ahead_ask", "the workers' paints and timings: how a text looks, and numbers" }, diff --git a/test/behaviour/addons.zig b/test/behaviour/addons.zig index ce3c7b02..25b1e523 100644 --- a/test/behaviour/addons.zig +++ b/test/behaviour/addons.zig @@ -371,3 +371,149 @@ test "addons: an image pane whose file changes on disk shows the new picture, un try std.testing.expectEqual(n, try s.focused()); try std.testing.expect(has(try s.read("/screen"), "\"src\":[0,0,3,1]")); } + +/// A tmp directory's real path, kept in the session's arena. +fn dirOf(s: *Session, tmp: *std.testing.TmpDir) ![]const u8 { + var buf: [4096]u8 = undefined; + return s.arena.allocator().dupe(u8, buf[0..try tmp.dir.realPath(std.testing.io, &buf)]); +} + +test "addons: a picker shows a file of an addon's kind through its kind in the pane it lands in, Esc puts that pane back as it was, and Enter lands as a Look does" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "the notes\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "seven.fake", .data = "1234567" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try load(&s, fake.ok); + defer unload(&s, "fake"); + const dir = try dirOf(&s, &tmp); + const pane = try s.lookFile(s.at("{s}/notes.txt", .{dir})); + const body = s.at("/pane/{d}/body", .{pane}); + const a = s.arena.allocator(); + const screen = try a.dupe(u8, try s.screen()); + const index = try a.dupe(u8, try s.read("/index")); + // Its kind's paint in the reading pane's body (the fake counts the + // file's 7 bytes), not the pane's own text, nor the file's bytes. + try s.keys(" ffseven"); + s.tick(); + const shown = try s.screen(); + try std.testing.expect(has(shown, "count 7")); + try std.testing.expect(!has(shown, "the notes") and !has(shown, "1234567")); + // Esc: the pane exactly as it was, and no pane more or less. + try s.keys("\x1b"); + s.tick(); + // (The notice row says the Find ran: the session's, not the pane's.) + const after = try s.screen(); + try std.testing.expectEqualStrings(screen[0..std.mem.lastIndexOfScalar(u8, screen, '\n').?], after[0..std.mem.lastIndexOfScalar(u8, after, '\n').?]); + try s.expect(body, "the notes\n"); + try std.testing.expectEqualStrings(index, try s.read("/index")); + // Enter: the kind's pane where a Look puts it; the reading pane keeps + // its text. + try s.keys(" ffseven\n"); + s.tick(); + const opened = try paneOf(&s, "fake"); + try std.testing.expect(opened != pane); + try s.expect(body, "the notes\n"); + try std.testing.expect(has(try s.screen(), "count 7")); +} + +test "addons: a picker's row of a file no kind takes and not text shows one line naming it, and the row before's file never stays in the body" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "the notes\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "other.txt", .data = "other text\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "seven.fake", .data = "1234567" }); + var blob: [3000]u8 = undefined; + for (&blob, 0..) |*b, i| b.* = @truncate(i *% 7); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "blob.bin", .data = &blob }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try load(&s, fake.ok); + defer unload(&s, "fake"); + const dir = try dirOf(&s, &tmp); + const pane = try s.lookFile(s.at("{s}/notes.txt", .{dir})); + try s.keys(" ffother"); + s.tick(); + try std.testing.expect(has(try s.screen(), "other text")); + // Onto the binary: its one line, never its bytes, nor other.txt. + try s.keys("\x08\x08\x08\x08\x08blob"); + s.tick(); + var shown = try s.screen(); + try std.testing.expect(has(shown, "blob.bin · binary · 3.0 kB")); + try std.testing.expect(!has(shown, "other text")); + // Onto a kind's file: its kind's paint, not the binary's line. + try s.keys("\x08\x08\x08\x08seven"); + s.tick(); + shown = try s.screen(); + try std.testing.expect(has(shown, "count 7")); + try std.testing.expect(!has(shown, "blob.bin · binary")); + // Back onto text, then Esc: the pane's own. + try s.keys("\x08\x08\x08\x08\x08other"); + s.tick(); + shown = try s.screen(); + try std.testing.expect(has(shown, "other text") and !has(shown, "count 7")); + try s.keys("\x1b"); + s.tick(); + try s.expect(s.at("/pane/{d}/body", .{pane}), "the notes\n"); + try std.testing.expect(has(try s.screen(), "the notes")); +} + +test "addons: moving the cursor over pictures in a picker draws only the one it rests on, and a draw of one passed by is dropped whenever it comes back" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const png = "\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\x0cIDATx\x9cc\xf8\xcf\xc0\x00\x00\x03\x01\x01\x00\xc9\xfe\x92\xef\x00\x00\x00\x00IEND\xaeB`\x82"; + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "the notes\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic1.png", .data = png }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic2.png", .data = png }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic3.ppm", .data = "P6\n2 1\n255\n\x0d\x4d\xe7\x0d\x4d\xe7" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + s.graphics(); + try load(&s, Session.image_addon); + defer unload(&s, "image"); + const dir = try dirOf(&s, &tmp); + const pane = try s.lookFile(s.at("{s}/notes.txt", .{dir})); + s.holdJobs(); + try s.keys(" ffpic"); + s.tick(); + // Until its picture is drawn, one line names it. + const first = try s.screen(); + try std.testing.expect(has(first, " · image · ")); + try std.testing.expectEqual(1, s.held.items.len); + // Two rows down with no frame between: only the row it rests on draws. + try s.keys("\u{F800}\u{F800}"); + s.tick(); + _ = try s.screen(); + try std.testing.expectEqual(2, s.held.items.len); + const rested = if (has(try s.screen(), "▶ pic3.ppm")) "\"src\":[0,0,2,1]" else "\"src\":[0,0,1,1]"; + const passed = if (std.mem.eql(u8, rested, "\"src\":[0,0,2,1]")) "\"src\":[0,0,1,1]" else "\"src\":[0,0,2,1]"; + // The rested one's answer first, the passed one's after: dropped. + std.mem.reverse(@TypeOf(s.held.items[0]), s.held.items); + s.releaseJobs(); + const screen = try s.read("/screen"); + try std.testing.expect(has(screen, s.at("\"serial\":{d},", .{pane})) and has(screen, rested)); + try std.testing.expect(!has(screen, passed)); +} + +test "addons: Enter on a picture in a picker opens its kind's pane, never its bytes as text in the reading pane (the user's report)" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "the notes\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic.ppm", .data = "P6\n2 1\n255\n\x0d\x4d\xe7\x0d\x4d\xe7" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + s.graphics(); + try load(&s, Session.image_addon); + defer unload(&s, "image"); + const dir = try dirOf(&s, &tmp); + const pane = try s.lookFile(s.at("{s}/notes.txt", .{dir})); + try s.keys(" ffpic\n"); + s.tick(); + try s.expect(s.at("/pane/{d}/body", .{pane}), "the notes\n"); + const image = try paneOf(&s, "image"); + try std.testing.expect(image != pane); + const screen = try s.read("/screen"); + try std.testing.expect(has(screen, s.at("\"serial\":{d},", .{image})) and has(screen, "\"src\":[0,0,2,1]")); + try std.testing.expect(!has(try s.screen(), "1 P6")); +} diff --git a/test/behaviour/addons_pdf.zig b/test/behaviour/addons_pdf.zig index 0c7982d1..d773f4e2 100644 --- a/test/behaviour/addons_pdf.zig +++ b/test/behaviour/addons_pdf.zig @@ -299,3 +299,31 @@ test "addons: a PDF whose file changes on disk while a page of it renders: the p try s.keys("0"); try std.testing.expect((try shown(&s, n)).len >= 1); } + +test "addons: a PDF under a picker's cursor shows through the pdf addon in the pane it lands in, never its bytes as text, and Esc puts the pane's own text back" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + s.graphics(); + try s.loadPdf(); + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const pdf = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "docs/design.pdf", s.arena.allocator(), .limited(4 << 20)); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "paper.pdf", .data = pdf }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "the notes\n" }); + var buf: [4096]u8 = undefined; + const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; + const pane = try s.lookFile(s.at("{s}/notes.txt", .{dir})); + try s.keys(" ffpaper"); + // (Its kind told its view at the frame after the one that opened it.) + s.tick(); + s.tick(); + const screen = try s.screen(); + try std.testing.expect(!has(screen, "%PDF") and !has(screen, "the notes")); + // Its pages pictures in that pane's body, as a PDF's pane shows them. + try std.testing.expect((try shown(&s, pane)).len > 0); + try s.keys("\x1b"); + s.tick(); + try s.expect(s.at("/pane/{d}/body", .{pane}), "the notes\n"); + try std.testing.expectEqual(0, (try shown(&s, pane)).len); + try std.testing.expect(has(try s.screen(), "the notes")); +} diff --git a/test/session.zig b/test/session.zig index d437ecf2..13798753 100644 --- a/test/session.zig +++ b/test/session.zig @@ -316,6 +316,14 @@ pub const Session = struct { return true; } + /// A frame of the host's loop (its tick): what waits for the frame + /// after the keys that asked it is done (a picker's preview of the row + /// its cursor rests on). + pub fn tick(s: *Session) void { + pardesOf(s).update(.tick); + s.drain(); + } + /// Kinds' jobs and draws held from now on (`hold_jobs`), until /// `releaseJobs`. pub fn holdJobs(s: *Session) void { -- cgit v1.3