diff options
| -rw-r--r-- | abi/term.zig | 10 | ||||
| -rw-r--r-- | core/Term.zig | 7 | ||||
| -rw-r--r-- | core/config.zig | 10 | ||||
| -rw-r--r-- | core/exec.zig | 53 | ||||
| -rw-r--r-- | core/term/term.zig | 1 | ||||
| -rw-r--r-- | core/term/terminal.zig | 30 | ||||
| -rw-r--r-- | dev/host_io.zig | 1 | ||||
| -rw-r--r-- | docs/macos.md | 11 |
8 files changed, 117 insertions, 6 deletions
diff --git a/abi/term.zig b/abi/term.zig index 4dfd523a..64541d00 100644 --- a/abi/term.zig +++ b/abi/term.zig @@ -238,6 +238,10 @@ pub const ImagesAt = struct { /// marked it input, and its style's identity. pub const EchoCell = struct { cp: u21, input: bool, style: u32 }; +/// A link (OSC 8) under a cell: its URI (the emulator's bytes: copied before +/// the next feed) and the cells `lo..hi` its run takes on that row. +pub const Link = struct { uri: []const u8, lo: u16, hi: u16 }; + /// What the dump keeps of a terminal beside its emulator, by name as every /// dump is (core/walk.zig, through the core's `Term.State`). pub const Parts = struct { @@ -356,6 +360,10 @@ pub const Ops = struct { promptCut: *const fn (s: *State, row: i32) ?PromptCut, /// What a predicted echo reads of cell `x`, `y` of its active area. echoCell: *const fn (s: *const State, x: u16, y: u16) ?EchoCell, + /// The link (OSC 8) cell `x` of row `row` (from the top of its history) + /// carries; `cut`: `x` and the run counted in the row's text, a + /// prompt's own cells cut off (a view out of tty mode). + link: *const fn (s: *State, row: i32, x: u16, cut: bool) ?Link, // ---- the pager ---- /// `raw`, a program's output, as a terminal reads it: its text and its @@ -391,7 +399,7 @@ pub const replicated = [_][]const u8{ "open", "close", "revive", "digest", "parts", "snapshot", "load", "reload", "media", "feed", "grid", "resize", "replies", "key", "mouse", "wheel", "paste", "modes", "promptMoves", "prompt", "commandOutput", - "endOutputHere", "scrollback", "scrollbackLen", "textRows", "viewport", "editRows", "promptCut", "page", + "endOutputHere", "scrollback", "scrollbackLen", "textRows", "link", "viewport", "editRows", "promptCut", "page", "filter", "spanColor", "hasImages", }; pub const local = [_][]const u8{ "start", "stop", "gpa", "due", "cellPixels", "echoCell", "paint", "images" }; diff --git a/core/Term.zig b/core/Term.zig index 32b98cd9..6415b199 100644 --- a/core/Term.zig +++ b/core/Term.zig @@ -56,6 +56,7 @@ pub const Painted = iface.Painted; pub const PaintedRow = iface.PaintedRow; pub const ImagesAt = iface.ImagesAt; pub const EchoCell = iface.EchoCell; +pub const Link = iface.Link; fn the() *const iface.Ops { return ops orelse unreachable; // no State is made without it @@ -350,6 +351,11 @@ pub fn echoCell(s: *const State, x: u16, y: u16) ?EchoCell { return the().echoCell(s.impl, x, y); } +/// The link (OSC 8) a cell of a row (from the top of the history) carries. +pub fn link(s: *State, row: i32, x: u16, cut: bool) ?Link { + return the().link(s.impl, row, x, cut); +} + // ---- the pager ---- /// A program's output, as a terminal reads it, paged (`pardes -`, a @@ -442,6 +448,7 @@ pub const calls = [_]struct { []const u8, []const u8, []const u8 }{ .{ "scrollbackLen", "ninep/ctl.zig:1 ninep/pane.zig:1", "READ, NOT COVERED: the history's text's length (its row count is in the digest)" }, .{ "commandOutput", "ninep/pty.zig:1", "READ, NOT COVERED: a command's output rows (a 9P pty/run): taped as the session's answer" }, .{ "textRows", "edit.zig:2", "READ, NOT COVERED: every row's text, the history's among them (a selection, a Look over a shell)" }, + .{ "link", "exec.zig:1", "READ, NOT COVERED: the URI of a link (OSC 8) a Look over a shell takes, as its text (`textRows`)" }, .{ "viewport", "body_layer.zig:1 replica.zig:1", "READ: the rows' text a view's place (in the digest, with its pane) shows (body_layer.zig: a draw's; replica.zig: a test's)" }, .{ "editRows", "ansi.zig:1 panes.zig:1", "READ: rows' text for a draw; a command's last rows read for the places its report names when it fails (panes.zig commandDone): in the digest as the emulator's rows are" }, // ---- local ---- diff --git a/core/config.zig b/core/config.zig index 69636d1d..f02732fb 100644 --- a/core/config.zig +++ b/core/config.zig @@ -241,9 +241,17 @@ pub const tty_toggle_default: u21 = 'b'; // cursor-addressing program fail at the terminfo lookup. There is no installed // `xterm-ghostty` entry to name, and a name with no entry is worse than a // conservative one that every system ships. +// +// TERM_PROGRAM names Ghostty, whose VT every pane is (the user's ruling of +// 2026-11-27: "posing as ghostty is fine by me, it's a good measure for us to +// have feature parity with it"): programs that pick their features by name +// alone (codex's kitty images) find them, and whatever they then expect of +// Ghostty is pardes's to give (a Look on an OSC 8 link opens its URI). Its +// version is the pinned ghostty's (build.zig.zon), not the launcher's. pub const child_term = "xterm-256color"; pub const child_colorterm = "truecolor"; -pub const child_term_program = "pardes"; +pub const child_term_program = "ghostty"; +pub const child_term_program_version = "1.4.0"; /// The window row's words (UX K'2): at its right end, `Restore <dump>` /// after them once a dump exists (tagline.headerWords). The rest of the 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.?); + } +} diff --git a/core/term/term.zig b/core/term/term.zig index 025f7239..e6250aae 100644 --- a/core/term/term.zig +++ b/core/term/term.zig @@ -51,6 +51,7 @@ pub const ops: iface.Ops = .{ .editRows = cast(iface.Ops, "editRows", terminal_.editRows), .promptCut = cast(iface.Ops, "promptCut", terminal_.promptCutAt), .echoCell = cast(iface.Ops, "echoCell", terminal_.echoCell), + .link = cast(iface.Ops, "link", terminal_.link), .page = pager.parse, .filter = terminal_.pagerFilter, .spanColor = terminal_.spanColor, diff --git a/core/term/terminal.zig b/core/term/terminal.zig index 12306957..d31545f5 100644 --- a/core/term/terminal.zig +++ b/core/term/terminal.zig @@ -1122,6 +1122,36 @@ pub fn echoCell(state: *const State, x: u16, y: u16) ?Term.EchoCell { return .{ .cp = ci.cell.codepoint(), .input = ci.cell.semantic_content == .input, .style = ci.cell.style_id }; } +/// The link (OSC 8) cell `x` of row `row` (from the top of the history) +/// carries, and the cells its run takes on that row; `cut`: counted in the +/// row's text, its prompt's own cells cut off (promptCut). +pub fn link(state: *State, row: i32, x: u16, cut: bool) ?Term.Link { + if (row < 0) return null; + const pin = state.vt.screens.active.pages.pin(.{ .screen = .{ .x = 0, .y = @intCast(row) } }) orelse return null; + const shift: usize = if (!cut or pin.rowAndCell().row.semantic_prompt == .none) 0 else switch (promptCut(pin)) { + .keep => 0, + .blank => return null, + .cut => |n| n, + }; + const cells = pin.cells(.all); + const at = @as(usize, x) + shift; + if (at >= cells.len) return null; + const page = pin.node.page(); + if (!cells[at].hyperlink) return null; + const id = page.lookupHyperlink(&cells[at]) orelse return null; + var lo = at; + while (lo > shift and sameLink(page, &cells[lo - 1], id)) lo -= 1; + var hi = at + 1; + while (hi < cells.len and sameLink(page, &cells[hi], id)) hi += 1; + const uri = page.hyperlink_set.get(page.memory, id).uri.slice(page.memory); + return .{ .uri = uri, .lo = @intCast(lo - shift), .hi = @intCast(hi - shift) }; +} + +fn sameLink(page: *const ghostty_vt.page.Page, cell: *const ghostty_vt.page.Cell, id: anytype) bool { + if (!cell.hyperlink) return false; + return (page.lookupHyperlink(cell) orelse return false) == id; +} + fn promptRow(pin: ghostty_vt.Pin, raw: []const u8) []const u8 { const cols = switch (promptCut(pin)) { .keep => return raw, diff --git a/dev/host_io.zig b/dev/host_io.zig index f189c8ea..e3969365 100644 --- a/dev/host_io.zig +++ b/dev/host_io.zig @@ -243,6 +243,7 @@ pub const ChildEnv = struct { "TERM=" ++ pardes.config.child_term, "COLORTERM=" ++ pardes.config.child_colorterm, "TERM_PROGRAM=" ++ pardes.config.child_term_program, + "TERM_PROGRAM_VERSION=" ++ pardes.config.child_term_program_version, }; /// What this editor tells its children of itself (exportEnv, `$winid`), diff --git a/docs/macos.md b/docs/macos.md index 98cd5312..a398879b 100644 --- a/docs/macos.md +++ b/docs/macos.md @@ -463,15 +463,18 @@ See [the control filesystem](typ/reference.typ) for paths and transports. Two more things this host cannot inherit from its launcher, because a `.app` has none: -- **The terminal's identity.** `host_io.ChildEnv` writes `TERM`, `COLORTERM` - and `TERM_PROGRAM` over whatever the environment carried and the child is +- **The terminal's identity.** `host_io.ChildEnv` writes `TERM`, `COLORTERM`, + `TERM_PROGRAM` and `TERM_PROGRAM_VERSION` over whatever the environment carried and the child is `execve`'d with that array — built before the fork, because a `setenv` between fork and exec can deadlock on the heap a pty reader thread was holding. Every pane is emulated by the bundled VT, so the value describes - pardes and never the terminal pardes was started from; `-Dplatform=gui` and + that VT and never the terminal pardes was started from (`TERM_PROGRAM` and + `TERM_PROGRAM_VERSION` name Ghostty, whose VT it is, so programs that pick + features by name find them); `-Dplatform=gui` and the tui shell take the same path, where the difference is only that their launcher usually happened to set something. The names come from - `config.child_term` / `child_colorterm` / `child_term_program`, and `TERM` + `config.child_term` / `child_colorterm` / `child_term_program` / + `child_term_program_version`, and `TERM` is deliberately `xterm-256color` rather than a name with no installed terminfo entry — `clear`, colour and cursor addressing all resolve through that lookup. |
