summaryrefslogtreecommitdiff
path: root/core/exec.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/exec.zig')
-rw-r--r--core/exec.zig53
1 files changed, 53 insertions, 0 deletions
diff --git a/core/exec.zig b/core/exec.zig
index fd2c6b1c..b6ab380c 100644
--- a/core/exec.zig
+++ b/core/exec.zig
@@ -164,6 +164,22 @@ pub fn operandAt(p: *Pardes, pane: *Pane, clicked: Pane.Sel, borrow: bool, for_l
return result;
}
}
+ // A link (OSC 8) pointed at in a terminal: a look takes its URI, as a
+ // look at the URI's own text would (a `file://` one, its path), and the
+ // label's cells are what it expanded to (the hover shows them). An exec
+ // takes the label's word, as anywhere else.
+ if (for_look and pane.isTerminal() and clicked.r0 >= pane.tag_rows and clicked.r0 == clicked.r1 and clicked.c0 == clicked.c1 and
+ clicked.c0 >= wrapped.pad and (raw or pane.ovl == null)) link: {
+ const pad = if (raw) 0 else wrapped.pad;
+ const l = panes.Term.link(pane.terminal.?, row, @intCast(clicked.c0 - pad), !raw) orelse break :link;
+ result.text = linkText(p.scratch.allocator(), l.uri) catch break :link;
+ var expanded = clicked;
+ expanded.c0 = @as(i32, l.lo) + pad;
+ expanded.c1 = @as(i32, l.hi) - 1 + pad;
+ result.preview = expanded;
+ result.expanded = expanded;
+ return result;
+ }
// A row that names an address (look.rowAddressAt), pointed at on it: a
// query pane's row on its address (its gutter), a diff's file or hunk
// header anywhere, a hunk line on its `+`/`-`/` ` prefix. It expands
@@ -225,6 +241,16 @@ pub fn operandAt(p: *Pardes, pane: *Pane, clicked: Pane.Sel, borrow: bool, for_l
return result;
}
+/// What a look takes of a link's URI: a `file://` one's path (its host
+/// dropped, its %XX decoded), which opens in a pane; any other as it is.
+fn linkText(arena: std.mem.Allocator, uri: []const u8) error{OutOfMemory}![]const u8 {
+ const scheme = "file://";
+ if (!std.ascii.startsWithIgnoreCase(uri, scheme)) return arena.dupe(u8, uri);
+ const rest = uri[scheme.len..];
+ const slash = std.mem.indexOfScalar(u8, rest, '/') orelse return arena.dupe(u8, uri);
+ return std.Uri.percentDecodeInPlace(try arena.dupe(u8, rest[slash..]));
+}
+
/// The cells bytes `lo..hi` of a file pane's line `row` take on screen,
/// soft-wrapped or not, as `clicked` reshaped; null when scrolled off.
fn fileWordCells(tab: u16, pane: *Pane, clicked: Pane.Sel, row: i32, lo: i32, hi: i32) ?Pane.Sel {
@@ -1190,3 +1216,30 @@ test "a switch clicked with a selection held flips unless the selection says on
// A word that takes any argument still gets the selection.
try std.testing.expectEqualStrings("Msg some text", withArg(p, "Msg", "some text"));
}
+
+test "a look at a link (OSC 8) in a terminal takes its URI, a file:// one its path, and expands to the label; out of tty mode too" {
+ if (panes.Term.ops == null) return error.SkipZigTest; // no emulator
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 40, .rows = 8 });
+ defer p.deinit();
+ while (p.nextEffect()) |_| {}
+ p.update(.{ .output = .{ .pane = .at(0), .bytes = "see \x1b]8;;https://ziglang.org\x1b\\the docs\x1b]8;;\x1b\\ here\r\n" ++
+ "\x1b]8;;file://box/tmp/a%20b.txt\x1b\\notes\x1b]8;;\x1b\\" } });
+ const pane = p.panes[0].?;
+ const at = struct {
+ fn f(pp: *Pardes, pn: *Pane, row: i32, col: i32, look_: bool) PointerOperand {
+ const r = row + @as(i32, pn.tag_rows);
+ return operandAt(pp, pn, .{ .r0 = r, .r1 = r, .c0 = col, .c1 = col }, false, look_);
+ }
+ }.f;
+ for ([_]bool{ true, false }) |tty| {
+ pane.body.mode = if (tty) .tty else .normal;
+ const docs = at(p, pane, 0, 6, true);
+ try std.testing.expectEqualStrings("https://ziglang.org", docs.text.?);
+ try std.testing.expectEqual(@as(i32, 4), docs.expanded.?.c0);
+ try std.testing.expectEqual(@as(i32, 11), docs.expanded.?.c1);
+ try std.testing.expectEqualStrings("/tmp/a b.txt", at(p, pane, 1, 0, true).text.?);
+ // Off the link, and an exec on it: the words as they read.
+ try std.testing.expectEqualStrings("see", at(p, pane, 0, 1, true).text.?);
+ try std.testing.expectEqualStrings("docs", at(p, pane, 0, 9, false).text.?);
+ }
+}