summaryrefslogtreecommitdiff
path: root/core/Pick.zig
diff options
context:
space:
mode:
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.