summaryrefslogtreecommitdiff
path: root/core/Pick.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-26 03:03:20 -0300
committerGabriel Schneider <[email protected]>2026-11-26 03:03:25 -0300
commit280306ed779368d7e20e9678eca45e4b81e0ccbf (patch)
tree9278534f32a3724b289761594ca658cb6549b227 /core/Pick.zig
parent1ce3bcb6178b720058518a12e45f25769be7d39c (diff)
downloadpardes-280306ed779368d7e20e9678eca45e4b81e0ccbf.tar.gz
pardes-280306ed779368d7e20e9678eca45e4b81e0ccbf.zip
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
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE
Diffstat (limited to 'core/Pick.zig')
-rw-r--r--core/Pick.zig132
1 files changed, 100 insertions, 32 deletions
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.