summaryrefslogtreecommitdiff
path: root/src/look.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/look.zig')
-rw-r--r--src/look.zig40
1 files changed, 18 insertions, 22 deletions
diff --git a/src/look.zig b/src/look.zig
index ab1407ee..870209a3 100644
--- a/src/look.zig
+++ b/src/look.zig
@@ -1,4 +1,5 @@
const std = @import("std");
+const Kind = @import("Kind.zig");
const builtin = @import("builtin");
const libc = std.c;
const pardes = @import("pardes.zig");
@@ -185,18 +186,12 @@ pub const Target = union(enum) {
none,
dir: []const u8, // resolved absolute path, in caller's buf
file: FileTarget,
- image: struct { path: []const u8 },
+ /// A file a kind opens (Kind.forPath): a picture.
+ kind: struct { path: []const u8, ops: *const Kind.Ops },
url: []const u8,
pane: struct { id: usize, at: Spot },
};
-pub fn isImagePath(path: []const u8) bool {
- for (config.image_exts) |ext| {
- if (std.ascii.endsWithIgnoreCase(path, ext)) return true;
- }
- return false;
-}
-
pub fn isPdfPath(path: []const u8) bool {
if (comptime !pdf_enabled) return false;
return std.ascii.endsWithIgnoreCase(path, ".pdf");
@@ -279,7 +274,7 @@ pub fn lineSpan(p: ?*pardes.Pardes, line: []const u8, cwd: []const u8, realbuf:
while (b > a and std.mem.indexOfScalar(u8, trail_trim, line[b - 1]) != null) b -= 1;
const cand = line[a..b];
if (cand.len > 0 and parsePathLine(cand).end == cand.len) switch (resolve(p, cand, cwd, realbuf)) {
- .dir, .file, .image => return .{ .start = a, .end = b },
+ .dir, .file, .kind => return .{ .start = a, .end = b },
.url, .pane => if (std.mem.indexOfAny(u8, cand, " \t") == null)
return .{ .start = a, .end = b },
.none => {},
@@ -376,7 +371,7 @@ pub fn resolve(p: ?*pardes.Pardes, word_raw: []const u8, cwd: []const u8, realbu
.at = pl.at,
.kind = .pdf,
} };
- if (isImagePath(found.path)) return .{ .image = .{ .path = found.path } };
+ if (Kind.forPath(found.path)) |ops| return .{ .kind = .{ .path = found.path, .ops = ops } };
return .{ .file = .{ .path = found.path, .at = pl.at } };
}
@@ -1038,7 +1033,7 @@ fn selectSpan(p: *Pardes, pane: *Pane, at: Spot) void {
fn openPaneTarget(p: *const Pardes, path: []const u8, at: Spot) ?Target {
for (p.panes, 0..) |slot, i| {
const tt = slot orelse continue;
- if (tt.image) |iv| if (std.mem.eql(u8, iv.path, path)) {
+ if (tt.addon) |k| if (std.mem.eql(u8, k.path, path)) {
return .{ .pane = .{ .id = i, .at = .{} } };
};
if (comptime pdf_enabled) if (tt.pdf) |pv| if (std.mem.eql(u8, pv.path, path)) {
@@ -1137,14 +1132,14 @@ fn canonicalLookLocation(p: *Pardes, id: usize, text: []const u8) ?[]const u8 {
return p.scratch.allocator().dupe(u8, url) catch null;
},
.file => |file| return locationText(p, file.path, file.at),
- .image => |iv| return locationText(p, iv.path, .{}),
+ .kind => |k| return locationText(p, k.path, .{}),
.dir => |dir| return locationText(p, dir, .{}),
.pane => |target| {
if (target.id >= p.panes.len) return null;
const pane = p.panes[target.id] orelse return null;
if (pane.file) |file| return locationText(p, file.path, target.at);
if (comptime pdf_enabled) if (pane.pdf) |doc| return locationText(p, doc.path, target.at);
- if (pane.image) |iv| return locationText(p, iv.path, target.at);
+ if (pane.addon) |k| if (k.path.len > 0) return locationText(p, k.path, target.at);
var path: [32]u8 = undefined;
return locationText(p, std.fmt.bufPrint(&path, config.pane_addr ++ "{d}", .{pane.serial}) catch return null, target.at);
},
@@ -1380,7 +1375,7 @@ fn lookUnresolved(p: *Pardes, id: usize, pane: *Pane, trimmed: []const u8, text:
}
// A raw terminal's clicks are its program's; a look written over
// 9P is no keyboard input, and searches whatever the key mode.
- if (pane.image != null or (pane.body.mode == .tty and !p.fs.serving)) return;
+ if (pane.addon != null or (pane.body.mode == .tty and !p.fs.serving)) return;
if (text.len == 0) return;
if (lookCommit(p, id, pane, trimmed)) return;
if (pane.file != null) return lookWord(p, id, pane, text);
@@ -1485,7 +1480,7 @@ fn lookAddress(p: *Pardes, id: usize, path: []const u8, expr: []const u8) void {
if ((by_serial or path.len == 0) and pane.isTerminal()) return lookTerminalAddress(p, id, target, expr);
// Named as the pane is, the bare `:addr` (path empty) included.
var serial_name: [32]u8 = undefined;
- const subject = if (pane.image) |iv| iv.path else if (path.len > 0) path else std.fmt.bufPrint(&serial_name, config.pane_addr ++ "{d}", .{pane.serial}) catch "the pane";
+ const subject = if (pane.addon) |k| k.path else if (path.len > 0) path else std.fmt.bufPrint(&serial_name, config.pane_addr ++ "{d}", .{pane.serial}) catch "the pane";
var cut: [miss_path_cap + "…".len]u8 = undefined;
return missed(p, id, "{s} is no text to address", .{missText(subject, &cut)});
};
@@ -1882,20 +1877,21 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void {
}
settleLine(p, id, target.path, target.at, before, opened);
},
- .image => |target| {
+ .kind => |target| {
if (focusPaneByPath(p, target.path, .{})) return;
const free = p.freeSlot() orelse return p.reportError(id, "look", error.NoPaneSlots);
- const made = panes.Image.create(p, free, target.path, &.{}) catch |err|
+ const made = Kind.openFile(p, free, target.ops, target.path) catch |err|
return lookOpenFailed(p, id, target.path, err);
- // One it cannot read (corrupt, truncated, not what its name says)
- // is said, a failure, rather than shown as a blank pane.
- if (!@import("image.zig").readable(made.image.?.raw)) {
+ // 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: [miss_path_cap + 64]u8 = undefined;
+ var said: [miss_path_cap + 160]u8 = undefined;
var cut: [miss_path_cap + "…".len]u8 = undefined;
- return p.reportFailure(id, std.fmt.bufPrint(&said, "look: {s}: not an image pardes can read", .{missText(target.path, &cut)}) catch "look: not an image pardes can read");
+ return p.reportFailure(id, std.fmt.bufPrint(&said, "look: {s}: {s}", .{ missText(target.path, &cut), made.addon.?.refused.slice() }) catch "look: refused");
}
+ Kind.startTicking(p, made);
exec.placeNew(p, id, id, free, .doc);
},
}