diff options
| -rw-r--r-- | core/Kind.zig | 55 | ||||
| -rw-r--r-- | core/Pick.zig | 132 | ||||
| -rw-r--r-- | core/Trail.zig | 4 | ||||
| -rw-r--r-- | core/draw.zig | 9 | ||||
| -rw-r--r-- | core/look.zig | 54 | ||||
| -rw-r--r-- | core/mouse.zig | 3 | ||||
| -rw-r--r-- | core/root.zig | 1 | ||||
| -rw-r--r-- | test/behaviour/addons.zig | 146 | ||||
| -rw-r--r-- | test/behaviour/addons_pdf.zig | 28 | ||||
| -rw-r--r-- | test/session.zig | 8 |
10 files changed, 373 insertions, 67 deletions
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 <path>"; // Its kind here, it waits for its file (`<path> 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 { |
