Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

fix(tui): preserve wrapped composer link targets

yxlyx8c2893dc561a
diff --git a/TUI/chrome.zig b/TUI/chrome.zig
index c2186f93..25b9c263 100644
--- a/TUI/chrome.zig
+++ b/TUI/chrome.zig
@@ -4,6 +4,7 @@ const std = @import("std");
const app = @import("app.zig");
const catalog = @import("catalog.zig");
+const composer_links = @import("composer_links.zig");
const engine = @import("engine.zig");
const glyphs = @import("glyphs.zig");
const panel = @import("panel.zig");
@@ -49,14 +50,18 @@ pub fn promptBox(self: *const Model, a: std.mem.Allocator, width: usize) ![]cons
try body.appendSlice(try imageChips(self, a, th.accent, th.text));
try body.appendSlice(" ");
}
- try body.appendSlice(try self.input.view(a));
- const wrapped = try theme_mod.wrapPreferWords(a, body.items, inner);
+ const value = self.input.getValue();
+ if (value.len == 0) {
+ try body.appendSlice(try self.input.view(a));
+ } else {
+ try body.appendSlice(try composer_links.inputView(a, value, self.input.cursor));
+ }
+ const max_body: usize = 8;
+ const wrapped = try composer_links.wrap(a, body.items, inner, max_body);
var lines = std.array_list.Managed([]const u8).init(a);
var it = std.mem.splitScalar(u8, wrapped, '\n');
while (it.next()) |ln| try lines.append(ln);
- const max_body: usize = 8;
- const start: usize = if (lines.items.len > max_body) lines.items.len - max_body else 0;
- for (lines.items[start..]) |ln| {
+ for (lines.items) |ln| {
try out.appendSlice(try rowInner(a, border, th.text, ln, inner));
try out.append('\n');
}
diff --git a/TUI/composer_links.zig b/TUI/composer_links.zig
new file mode 100644
index 00000000..ed248716
--- /dev/null
+++ b/TUI/composer_links.zig
@@ -0,0 +1,438 @@
+//! Safe OSC 8 links for the editable composer.
+//!
+//! The transcript has its own Markdown renderer. The composer must preserve the
+//! draft byte-for-byte, including cursor position, while making supported web
+//! addresses clickable across application-inserted row breaks.
+
+const std = @import("std");
+const glyphs = @import("glyphs.zig");
+const theme = @import("theme.zig");
+
+const osc_open_prefix = "\x1b]8;id=graff-composer-";
+const osc_close = "\x1b]8;;\x07";
+
+const Link = struct {
+ end: usize,
+ www: bool,
+};
+
+const Found = struct {
+ start: usize,
+ link: Link,
+};
+
+/// Render the raw draft with its cursor and allowlisted OSC 8 spans. The cursor
+/// is never part of the destination; when it bisects a link, the same link is
+/// closed around the glyph and reopened on the other side.
+pub fn inputView(a: std.mem.Allocator, value: []const u8, cursor: usize) ![]const u8 {
+ var out = std.array_list.Managed(u8).init(a);
+ errdefer out.deinit();
+ const cur = cursorBoundary(value, cursor);
+ var cursor_drawn = false;
+ var i: usize = 0;
+ while (nextLink(value, i)) |found| {
+ try appendPlain(&out, value[i..found.start], i, cur, &cursor_drawn);
+ try appendLinked(&out, value, found.start, found.link, cur, &cursor_drawn);
+ i = found.link.end;
+ }
+ try appendPlain(&out, value[i..], i, cur, &cursor_drawn);
+ if (!cursor_drawn) try out.appendSlice(glyphs.cursor);
+ return out.toOwnedSlice();
+}
+
+const Row = struct {
+ bytes: []const u8,
+ active_at_start: ?[]const u8,
+};
+
+/// Word-wrap a composer body and retain only its visible tail. Generated links
+/// are balanced on every retained row, so padding, borders, and independently
+/// painted frames cannot inherit hyperlink state. Bounding before serialization
+/// also keeps a pasted N-byte URL O(N): its full target is repeated at most
+/// `max_rows` times, never once for every discarded continuation row.
+pub fn wrap(a: std.mem.Allocator, body: []const u8, width: usize, max_rows: usize) ![]const u8 {
+ if (max_rows == 0) return a.dupe(u8, "");
+ const wrapped = try theme.wrapPreferWords(a, body, width);
+ defer a.free(wrapped);
+ var rows = std.array_list.Managed(Row).init(a);
+ defer rows.deinit();
+ var active: ?[]const u8 = null;
+ var active_at_start: ?[]const u8 = null;
+ var row_start: usize = 0;
+ var i: usize = 0;
+ while (i < wrapped.len) {
+ if (wrapped[i] == '\n') {
+ try keepRow(&rows, max_rows, .{ .bytes = wrapped[row_start..i], .active_at_start = active_at_start });
+ row_start = i + 1;
+ active_at_start = active;
+ i += 1;
+ continue;
+ }
+ if (wrapped[i] == 0x1b) {
+ const end = theme.skipEsc(wrapped, i);
+ noteLink(wrapped[i..end], &active);
+ i = end;
+ continue;
+ }
+ i += 1;
+ }
+ try keepRow(&rows, max_rows, .{ .bytes = wrapped[row_start..], .active_at_start = active_at_start });
+
+ var out = std.array_list.Managed(u8).init(a);
+ errdefer out.deinit();
+ for (rows.items, 0..) |row, row_no| {
+ var row_active = row.active_at_start;
+ if (row_active) |open| try out.appendSlice(open);
+ i = 0;
+ while (i < row.bytes.len) {
+ if (row.bytes[i] == 0x1b) {
+ const end = theme.skipEsc(row.bytes, i);
+ const seq = row.bytes[i..end];
+ try out.appendSlice(seq);
+ noteLink(seq, &row_active);
+ i = end;
+ continue;
+ }
+ try out.append(row.bytes[i]);
+ i += 1;
+ }
+ if (row_active != null) try out.appendSlice(osc_close);
+ if (row_no + 1 < rows.items.len) try out.append('\n');
+ }
+ return out.toOwnedSlice();
+}
+
+fn keepRow(rows: *std.array_list.Managed(Row), max_rows: usize, row: Row) !void {
+ if (rows.items.len == max_rows) _ = rows.orderedRemove(0);
+ try rows.append(row);
+}
+
+fn noteLink(seq: []const u8, active: *?[]const u8) void {
+ if (std.mem.startsWith(u8, seq, osc_open_prefix)) {
+ active.* = seq;
+ } else if (active.* != null and std.mem.eql(u8, seq, osc_close)) {
+ active.* = null;
+ }
+}
+
+fn cursorBoundary(value: []const u8, cursor: usize) usize {
+ var cur = @min(cursor, value.len);
+ while (cur > 0 and cur < value.len and value[cur] & 0xc0 == 0x80) cur -= 1;
+ return cur;
+}
+
+fn appendPlain(out: *std.array_list.Managed(u8), text: []const u8, base: usize, cursor: usize, drawn: *bool) !void {
+ if (!drawn.* and cursor >= base and cursor < base + text.len) {
+ const at = cursor - base;
+ try out.appendSlice(text[0..at]);
+ try out.appendSlice(glyphs.cursor);
+ try out.appendSlice(text[at..]);
+ drawn.* = true;
+ } else {
+ try out.appendSlice(text);
+ }
+}
+
+fn appendLinked(out: *std.array_list.Managed(u8), value: []const u8, start: usize, link: Link, cursor: usize, drawn: *bool) !void {
+ const split = !drawn.* and cursor >= start and cursor < link.end;
+ if (!split or cursor > start) {
+ try appendOpen(out, value[start..link.end], start, link.www);
+ try out.appendSlice(value[start..if (split) cursor else link.end]);
+ try out.appendSlice(osc_close);
+ }
+ if (split) {
+ try out.appendSlice(glyphs.cursor);
+ drawn.* = true;
+ if (cursor < link.end) {
+ try appendOpen(out, value[start..link.end], start, link.www);
+ try out.appendSlice(value[cursor..link.end]);
+ try out.appendSlice(osc_close);
+ }
+ }
+}
+
+fn appendOpen(out: *std.array_list.Managed(u8), visible: []const u8, id: usize, www: bool) !void {
+ var id_buf: [32]u8 = undefined;
+ const number = try std.fmt.bufPrint(&id_buf, "{d};", .{id});
+ try out.appendSlice(osc_open_prefix);
+ try out.appendSlice(number);
+ if (www) try out.appendSlice("https://");
+ try out.appendSlice(visible);
+ try out.append(0x07);
+}
+
+fn nextLink(s: []const u8, from: usize) ?Found {
+ var i = from;
+ while (i < s.len) {
+ const candidate = leftBoundary(s, i) and (startsIgnoreCase(s[i..], "http://") or startsIgnoreCase(s[i..], "https://") or startsIgnoreCase(s[i..], "www."));
+ if (candidate) {
+ if (linkAt(s, i)) |link| return .{ .start = i, .link = link };
+ i = tokenEnd(s, i);
+ continue;
+ }
+ i += 1;
+ }
+ return null;
+}
+
+fn linkAt(s: []const u8, start: usize) ?Link {
+ if (!leftBoundary(s, start)) return null;
+ const http_len: usize = if (startsIgnoreCase(s[start..], "https://")) 8 else if (startsIgnoreCase(s[start..], "http://")) 7 else 0;
+ const www = http_len == 0 and startsIgnoreCase(s[start..], "www.");
+ if (http_len == 0 and !www) return null;
+ var end = tokenEnd(s, start);
+ end = trimOuterDelimiters(s, start, end);
+ if (end <= start + if (www) @as(usize, 4) else http_len) return null;
+ if (www) {
+ if (!validWwwHost(s[start + 4 .. authorityEnd(s, start + 4, end)])) return null;
+ } else if (!validAuthority(s[start + http_len .. authorityEnd(s, start + http_len, end)])) {
+ return null;
+ }
+ return .{ .end = end, .www = www };
+}
+
+fn leftBoundary(s: []const u8, start: usize) bool {
+ if (start == 0) return true;
+ const c = s[start - 1];
+ if (c == '_') {
+ var run = start - 1;
+ while (run > 0 and s[run - 1] == '_') run -= 1;
+ return run == 0 or !std.ascii.isAlphanumeric(s[run - 1]);
+ }
+ if (c >= 0x80 or std.ascii.isAlphanumeric(c)) return false;
+ return std.mem.indexOfScalar(u8, ".-/:@%", c) == null;
+}
+
+fn tokenEnd(s: []const u8, start: usize) usize {
+ var i = start;
+ while (i < s.len) {
+ const c = s[i];
+ if (c < 0x20 or c == 0x7f or std.ascii.isWhitespace(c)) break;
+ const len = std.unicode.utf8ByteSequenceLength(c) catch 1;
+ const step = @min(@as(usize, len), s.len - i);
+ const cp = std.unicode.utf8Decode(s[i .. i + step]) catch {
+ i += step;
+ continue;
+ };
+ if (unicodeSpace(cp)) break;
+ i += step;
+ }
+ return i;
+}
+
+fn unicodeSpace(cp: u21) bool {
+ return cp == 0x00a0 or cp == 0x1680 or cp == 0x2028 or cp == 0x2029 or cp == 0x202f or cp == 0x205f or cp == 0x3000 or (cp >= 0x2000 and cp <= 0x200a);
+}
+
+fn trimOuterDelimiters(s: []const u8, start: usize, initial_end: usize) usize {
+ var structural_end = initial_end;
+ while (structural_end > start and std.mem.indexOfScalar(u8, ".,", s[structural_end - 1]) != null) structural_end -= 1;
+
+ var wrapper_end = trimMatchingWrapper(s, start, structural_end);
+ if (wrapper_end != structural_end) {
+ while (wrapper_end > start and std.mem.indexOfScalar(u8, ".,", s[wrapper_end - 1]) != null) wrapper_end -= 1;
+ return wrapper_end;
+ }
+
+ var end = structural_end;
+ for ([_]struct { open: u8, close: u8 }{
+ .{ .open = '(', .close = ')' },
+ .{ .open = '[', .close = ']' },
+ .{ .open = '{', .close = '}' },
+ }) |pair| {
+ const opens = countByte(s[start..end], pair.open);
+ var closes = countByte(s[start..end], pair.close);
+ while (end > start and s[end - 1] == pair.close and closes > opens) {
+ end -= 1;
+ closes -= 1;
+ }
+ }
+ if (end != structural_end) return end;
+
+ if (start > 0 and end > start) {
+ const before = s[start - 1];
+ if ((before == '<' and s[end - 1] == '>') or
+ (before == '\'' and s[end - 1] == '\'') or
+ (before == '"' and s[end - 1] == '"')) return end - 1;
+ }
+ return structural_end;
+}
+
+fn trimMatchingWrapper(s: []const u8, start: usize, initial_end: usize) usize {
+ if (start == 0 or initial_end <= start) return initial_end;
+ const mark = s[start - 1];
+ if (std.mem.indexOfScalar(u8, "*_~`", mark) == null) return initial_end;
+ var opens: usize = 0;
+ var i = start;
+ while (i > 0 and s[i - 1] == mark) : (i -= 1) opens += 1;
+ var closes: usize = 0;
+ i = initial_end;
+ while (i > start and s[i - 1] == mark and closes < opens) : (i -= 1) closes += 1;
+ return initial_end - closes;
+}
+
+fn authorityEnd(s: []const u8, start: usize, end: usize) usize {
+ var i = start;
+ while (i < end and std.mem.indexOfScalar(u8, "/?#", s[i]) == null) : (i += 1) {}
+ return i;
+}
+
+fn validAuthority(authority: []const u8) bool {
+ if (authority.len == 0) return false;
+ for (authority) |c| if (c < 0x21 or c == 0x7f or std.mem.indexOfScalar(u8, "<>\"'", c) != null) return false;
+ const host_port = if (std.mem.lastIndexOfScalar(u8, authority, '@')) |at| authority[at + 1 ..] else authority;
+ return validHostPort(host_port, false);
+}
+
+fn validWwwHost(host_port: []const u8) bool {
+ return validHostPort(host_port, true);
+}
+
+fn validHostPort(host_port: []const u8, require_dot: bool) bool {
+ if (host_port.len == 0) return false;
+ if (host_port[0] == '[') {
+ const close = std.mem.indexOfScalar(u8, host_port, ']') orelse return false;
+ if (close == 1 or !validIpv6(host_port[1..close])) return false;
+ return close + 1 == host_port.len or (host_port[close + 1] == ':' and validPort(host_port[close + 2 ..]));
+ }
+ const colon = std.mem.lastIndexOfScalar(u8, host_port, ':');
+ const host = if (colon) |at| host_port[0..at] else host_port;
+ if (std.mem.indexOfScalar(u8, host, ':') != null or !validHost(host, require_dot)) return false;
+ return if (colon) |at| validPort(host_port[at + 1 ..]) else true;
+}
+
+fn validHost(host: []const u8, require_dot: bool) bool {
+ if (host.len == 0 or host.len > 253) return false;
+ var labels = std.mem.splitScalar(u8, host, '.');
+ var count: usize = 0;
+ while (labels.next()) |label| {
+ if (label.len == 0 or label.len > 63 or label[0] == '-' or label[label.len - 1] == '-') return false;
+ for (label) |c| if (!std.ascii.isAlphanumeric(c) and c != '-') return false;
+ count += 1;
+ }
+ return !require_dot or count > 1;
+}
+
+fn validIpv6(host: []const u8) bool {
+ _ = std.Io.net.IpAddress.parseIp6(host, 0) catch return false;
+ return true;
+}
+
+fn validPort(port: []const u8) bool {
+ if (port.len == 0) return false;
+ for (port) |c| if (!std.ascii.isDigit(c)) return false;
+ _ = std.fmt.parseInt(u16, port, 10) catch return false;
+ return true;
+}
+
+fn startsIgnoreCase(s: []const u8, prefix: []const u8) bool {
+ return s.len >= prefix.len and std.ascii.eqlIgnoreCase(s[0..prefix.len], prefix);
+}
+
+fn countByte(s: []const u8, needle: u8) usize {
+ var n: usize = 0;
+ for (s) |c| {
+ if (c == needle) n += 1;
+ }
+ return n;
+}
+
+test "composer links allowlist web targets and preserve visible text" {
+ const dump = @import("dump.zig");
+ const cases = [_]struct { source: []const u8, target: ?[]const u8 }{
+ .{ .source = "http://example.test/path", .target = "http://example.test/path" },
+ .{ .source = "HTTPS://example.test/path", .target = "HTTPS://example.test/path" },
+ .{ .source = "www.example.test/path", .target = "https://www.example.test/path" },
+ .{ .source = "https://example.test/path.", .target = "https://example.test/path" },
+ .{ .source = "**http://localhost:3003.**", .target = "http://localhost:3003" },
+ .{ .source = "_https://example.test/path_", .target = "https://example.test/path" },
+ .{ .source = "snake_case https://example.test/a_", .target = "https://example.test/a_" },
+ .{ .source = "https://example.test/path(a)", .target = "https://example.test/path(a)" },
+ .{ .source = "https://en.wikipedia.org/wiki/Yahoo!", .target = "https://en.wikipedia.org/wiki/Yahoo!" },
+ .{ .source = "javascript:https://example.test", .target = null },
+ .{ .source = "[email protected]", .target = null },
+ .{ .source = "file:///tmp/a", .target = null },
+ .{ .source = "https://:80/path", .target = null },
+ .{ .source = "https://a.-b.example/path", .target = null },
+ .{ .source = "https://" ++ ("a" ** 64) ++ ".example/path", .target = null },
+ .{ .source = "https://[:::]/path", .target = null },
+ .{ .source = "www.example.test:bad/path", .target = null },
+ .{ .source = "www.", .target = null },
+ };
+ for (cases) |case| {
+ const got = try inputView(std.testing.allocator, case.source, case.source.len);
+ defer std.testing.allocator.free(got);
+ const visible = try dump.visible(std.testing.allocator, got);
+ defer std.testing.allocator.free(visible);
+ try std.testing.expectEqual(case.source.len + glyphs.cursor.len, visible.len);
+ try std.testing.expect(std.mem.startsWith(u8, visible, case.source));
+ try std.testing.expect(std.mem.endsWith(u8, visible, glyphs.cursor));
+ if (case.target) |target| {
+ const needle = try std.fmt.allocPrint(std.testing.allocator, ";{s}\x07", .{target});
+ defer std.testing.allocator.free(needle);
+ try std.testing.expect(std.mem.indexOf(u8, got, needle) != null);
+ try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, got, osc_open_prefix));
+ } else {
+ try std.testing.expectEqual(@as(usize, 0), std.mem.count(u8, got, osc_open_prefix));
+ }
+ }
+}
+
+test "composer link cursor keeps the complete target and stays outside it" {
+ const value = "https://example.test/a/long/path";
+ const cursor = std.mem.indexOf(u8, value, "example").? + 3;
+ const got = try inputView(std.testing.allocator, value, cursor);
+ defer std.testing.allocator.free(got);
+ try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, got, osc_open_prefix));
+ try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, got, value));
+ try std.testing.expect(std.mem.indexOf(u8, got, osc_close ++ glyphs.cursor ++ osc_open_prefix) != null);
+}
+
+test "composer cursor snaps away from a UTF-8 continuation byte" {
+ const dump = @import("dump.zig");
+ const value = "https://example.test/é";
+ const got = try inputView(std.testing.allocator, value, value.len - 1);
+ defer std.testing.allocator.free(got);
+ const visible = try dump.visible(std.testing.allocator, got);
+ defer std.testing.allocator.free(visible);
+ try std.testing.expectEqualStrings("https://example.test/" ++ glyphs.cursor ++ "é", visible);
+ try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, got, osc_open_prefix));
+ try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, got, osc_close));
+}
+
+test "wrapped composer links are balanced on every retained physical row" {
+ const value = "https://example.test/one/two/three/four/five/six/seven";
+ const linked = try inputView(std.testing.allocator, value, value.len);
+ defer std.testing.allocator.free(linked);
+ const got = try wrap(std.testing.allocator, linked, 12, 8);
+ defer std.testing.allocator.free(got);
+ var rows: usize = 0;
+ var it = std.mem.splitScalar(u8, got, '\n');
+ while (it.next()) |row| {
+ if (std.mem.indexOf(u8, row, osc_open_prefix) == null) continue;
+ rows += 1;
+ try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, row, osc_open_prefix));
+ try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, row, osc_close));
+ try std.testing.expect(theme.visibleLen(row) <= 12);
+ }
+ try std.testing.expect(rows > 2);
+}
+
+test "long composer links stay linear before the eight-row window" {
+ const value = "https://example.test/" ++ ("a" ** 10_000);
+ const linked = try inputView(std.testing.allocator, value, value.len);
+ defer std.testing.allocator.free(linked);
+ const got = try wrap(std.testing.allocator, linked, 38, 8);
+ defer std.testing.allocator.free(got);
+ try std.testing.expect(got.len < value.len * 10);
+ try std.testing.expectEqual(@as(usize, 8), std.mem.count(u8, got, osc_open_prefix));
+ try std.testing.expectEqual(@as(usize, 8), std.mem.count(u8, got, osc_close));
+}
+
+test "malformed repeated link candidates are scanned once" {
+ const value = "(www." ** 2_000;
+ const got = try inputView(std.testing.allocator, value, value.len);
+ defer std.testing.allocator.free(got);
+ try std.testing.expectEqual(@as(usize, 0), std.mem.count(u8, got, osc_open_prefix));
+ try std.testing.expectEqual(value.len + glyphs.cursor.len, got.len);
+}
diff --git a/TUI/input.zig b/TUI/input.zig
index f4916ae7..4c73b5ff 100644
--- a/TUI/input.zig
+++ b/TUI/input.zig
@@ -62,6 +62,7 @@ pub const Input = struct {
pub fn insertSlice(self: *Input, text: []const u8) void {
if (text.len == 0) return;
+ self.cursor = scalarBoundary(self.buf.items, self.cursor);
self.pushUndo();
self.buf.insertSlice(self.cursor, text) catch return;
self.cursor += text.len;
@@ -74,7 +75,11 @@ pub const Input = struct {
pub fn handle(self: *Input, k: Key) void {
switch (k) {
.char => |c| {
- self.pushUndo();
+ self.cursor = scalarBoundary(self.buf.items, self.cursor);
+ // Raw terminal UTF-8 arrives one byte at a time. Continuation
+ // bytes belong to the leading byte's edit, or undo could
+ // restore a lone prefix and leave the draft malformed.
+ if (c & 0xc0 != 0x80) self.pushUndo();
self.buf.insert(self.cursor, c) catch return;
self.cursor += 1;
},
@@ -84,15 +89,14 @@ pub const Input = struct {
self.insertSlice(b[0..n]);
},
.backspace => {
- if (self.cursor == 0) return;
- self.pushUndo();
- _ = self.buf.orderedRemove(self.cursor - 1);
- self.cursor -= 1;
- },
- .left => self.cursor -|= 1,
- .right => {
- if (self.cursor < self.buf.items.len) self.cursor += 1;
+ const end = scalarBoundary(self.buf.items, self.cursor);
+ const start = prevScalar(self.buf.items, end);
+ if (start == end) return;
+ self.killRange(start, end);
+ self.cursor = start;
},
+ .left => self.cursor = prevScalar(self.buf.items, self.cursor),
+ .right => self.cursor = nextScalar(self.buf.items, self.cursor),
.home => self.cursor = 0,
.end => self.cursor = self.buf.items.len,
.word_left => self.cursor = prevWord(self.buf.items, self.cursor),
@@ -100,14 +104,20 @@ pub const Input = struct {
.delete_word => self.killTo(prevWord(self.buf.items, self.cursor)),
.delete_to_start => self.killTo(0),
.delete_to_end => self.killRange(self.cursor, self.buf.items.len),
- .delete => self.killRange(self.cursor, if (self.cursor < self.buf.items.len) self.cursor + 1 else self.cursor),
+ .delete => {
+ self.cursor = scalarBoundary(self.buf.items, self.cursor);
+ self.killRange(self.cursor, nextScalar(self.buf.items, self.cursor));
+ },
.ctrl => |c| switch (c) {
'a' => self.cursor = 0,
'e' => self.cursor = self.buf.items.len,
'k' => self.killRange(self.cursor, self.buf.items.len),
'u' => self.killTo(0),
'w' => self.killTo(prevWord(self.buf.items, self.cursor)),
- 'd' => self.killRange(self.cursor, if (self.cursor < self.buf.items.len) self.cursor + 1 else self.cursor),
+ 'd' => {
+ self.cursor = scalarBoundary(self.buf.items, self.cursor);
+ self.killRange(self.cursor, nextScalar(self.buf.items, self.cursor));
+ },
else => {},
},
else => {},
@@ -143,6 +153,28 @@ pub const Input = struct {
}
};
+fn scalarBoundary(s: []const u8, cursor: usize) usize {
+ var i = @min(cursor, s.len);
+ while (i > 0 and i < s.len and s[i] & 0xc0 == 0x80) i -= 1;
+ return i;
+}
+
+fn prevScalar(s: []const u8, cursor: usize) usize {
+ var i = scalarBoundary(s, cursor);
+ if (i == 0) return 0;
+ i -= 1;
+ while (i > 0 and s[i] & 0xc0 == 0x80) i -= 1;
+ return i;
+}
+
+fn nextScalar(s: []const u8, cursor: usize) usize {
+ var i = scalarBoundary(s, cursor);
+ if (i >= s.len) return s.len;
+ i += 1;
+ while (i < s.len and s[i] & 0xc0 == 0x80) i += 1;
+ return i;
+}
+
fn prevWord(s: []const u8, cur: usize) usize {
var i = cur;
while (i > 0 and s[i - 1] == ' ') i -= 1;
@@ -169,6 +201,22 @@ test "insert and backspace" {
try std.testing.expectEqualStrings("héllo", in.getValue());
}
+test "cursor movement and deletion keep UTF-8 scalar boundaries" {
+ var in = Input.init(std.testing.allocator);
+ defer in.deinit();
+ try in.setValue("aé日");
+ in.handle(.left);
+ try std.testing.expectEqual(@as(usize, 3), in.cursor);
+ in.handle(.left);
+ try std.testing.expectEqual(@as(usize, 1), in.cursor);
+ in.handle(.{ .char = 'x' });
+ try std.testing.expectEqualStrings("axé日", in.getValue());
+ in.handle(.delete);
+ try std.testing.expectEqualStrings("ax日", in.getValue());
+ in.handle(.backspace);
+ try std.testing.expectEqualStrings("a日", in.getValue());
+}
+
test "cmd-delete and alt-backspace kill the line and the word" {
var in = Input.init(std.testing.allocator);
defer in.deinit();
@@ -192,6 +240,16 @@ test "ctrl-z undoes the last edit" {
try std.testing.expect(!in.undo());
}
+test "raw UTF-8 bytes undo as one scalar edit" {
+ var in = Input.init(std.testing.allocator);
+ defer in.deinit();
+ in.handle(.{ .char = 0xc3 });
+ in.handle(.{ .char = 0xa9 });
+ try std.testing.expectEqualStrings("é", in.getValue());
+ try std.testing.expect(in.undo());
+ try std.testing.expectEqualStrings("", in.getValue());
+}
+
test "kitty codepoint inserts UTF-8" {
var in = Input.init(std.testing.allocator);
defer in.deinit();
diff --git a/TUI/issue_788_tests.zig b/TUI/issue_788_tests.zig
new file mode 100644
index 00000000..34afecb5
--- /dev/null
+++ b/TUI/issue_788_tests.zig
@@ -0,0 +1,122 @@
+//! Regression coverage for issue 788: wrapped composer URLs keep one safe OSC 8
+//! target without leaking hyperlink state into row padding, borders, or footer.
+
+const std = @import("std");
+const render_mod = @import("render.zig");
+const sim = @import("sim.zig");
+const theme = @import("theme.zig");
+
+const open_prefix = "\x1b]8;id=graff-composer-";
+const close = "\x1b]8;;\x07";
+
+fn expectTargetRows(a: std.mem.Allocator, frame: []const u8, target: []const u8, minimum: usize) !usize {
+ const target_end = try std.fmt.allocPrint(a, ";{s}\x07", .{target});
+ defer a.free(target_end);
+ var count: usize = 0;
+ var it = std.mem.splitScalar(u8, frame, '\n');
+ while (it.next()) |row| {
+ const opens = std.mem.count(u8, row, open_prefix);
+ const closes = std.mem.count(u8, row, close);
+ if (opens == 0) {
+ try std.testing.expectEqual(@as(usize, 0), closes);
+ continue;
+ }
+ const target_at = std.mem.indexOf(u8, row, target_end) orelse return error.WrongLinkTarget;
+ count += 1;
+ const open_at = std.mem.lastIndexOf(u8, row[0 .. target_at + 1], open_prefix) orelse return error.MissingLinkOpen;
+ const linked_at = target_at + target_end.len;
+ const close_at = std.mem.indexOfPos(u8, row, linked_at, close) orelse return error.MissingLinkClose;
+ const border_at = std.mem.lastIndexOf(u8, row, "│") orelse return error.MissingComposerBorder;
+ try std.testing.expect(open_at < target_at);
+ try std.testing.expect(theme.visibleLen(row[linked_at..close_at]) > 0);
+ try std.testing.expect(close_at < border_at);
+ try std.testing.expectEqual(opens, closes);
+ }
+ try std.testing.expect(count >= minimum);
+ return count;
+}
+
+fn renderTerm(term: *sim.Term) ![]const u8 {
+ return render_mod.render(&term.model, term.alloc, term.cols, term.rows, term.now_ms);
+}
+
+test "headless composer preserves wrapped http https and www targets" {
+ const cases = [_]struct { visible: []const u8, target: []const u8 }{
+ .{
+ .visible = "http://example.test/one/two/three/four/five/six/seven/eight/nine",
+ .target = "http://example.test/one/two/three/four/five/six/seven/eight/nine",
+ },
+ .{
+ .visible = "https://example.test/one/two/three/four/five/six/seven/eight/nine",
+ .target = "https://example.test/one/two/three/four/five/six/seven/eight/nine",
+ },
+ .{
+ .visible = "www.example.test/one/two/three/four/five/six/seven/eight/nine",
+ .target = "https://www.example.test/one/two/three/four/five/six/seven/eight/nine",
+ },
+ };
+ for (cases) |case| {
+ var term: sim.Term = undefined;
+ term.init(std.testing.allocator, 40, 24);
+ defer term.deinit();
+ _ = term.typeText(case.visible);
+ const narrow = try renderTerm(&term);
+ defer std.testing.allocator.free(narrow);
+ _ = try expectTargetRows(std.testing.allocator, narrow, case.target, 2);
+
+ term.cols = 57;
+ const resized = try renderTerm(&term);
+ defer std.testing.allocator.free(resized);
+ _ = try expectTargetRows(std.testing.allocator, resized, case.target, 2);
+ }
+}
+
+test "headless composer starts a truncated eight-row link window balanced" {
+ const target = "https://example.test/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
+ var term: sim.Term = undefined;
+ term.init(std.testing.allocator, 40, 24);
+ defer term.deinit();
+ _ = term.typeText(target);
+ const frame = try renderTerm(&term);
+ defer std.testing.allocator.free(frame);
+ const rows = try expectTargetRows(std.testing.allocator, frame, target, 8);
+ try std.testing.expectEqual(@as(usize, 8), rows);
+}
+
+test "headless composer edits non-ASCII links only at scalar boundaries" {
+ const original = "https://example.test/é";
+ var term: sim.Term = undefined;
+ term.init(std.testing.allocator, 40, 24);
+ defer term.deinit();
+ _ = term.typeText(original);
+ _ = term.press(.left);
+ _ = term.typeText("x");
+ try std.testing.expectEqualStrings("https://example.test/xé", term.model.input.getValue());
+ const frame = try renderTerm(&term);
+ defer std.testing.allocator.free(frame);
+ _ = try expectTargetRows(std.testing.allocator, frame, "https://example.test/xé", 1);
+}
+
+test "headless composer does not emit links for unsafe or embedded forms" {
+ const cases = [_][]const u8{
+ "javascript:alert(1)",
+ "data:text/html,hello",
+ "file:///tmp/a",
+ "mailto:[email protected]",
+ "ssh://example.test/path",
+ "javascript:https://example.test/path",
+ "https://a.-b.example/path",
+ "https://[:::]/path",
+ "www.example.test:bad/path",
+ };
+ for (cases) |value| {
+ var term: sim.Term = undefined;
+ term.init(std.testing.allocator, 40, 24);
+ defer term.deinit();
+ _ = term.typeText(value);
+ const frame = try renderTerm(&term);
+ defer std.testing.allocator.free(frame);
+ try std.testing.expectEqual(@as(usize, 0), std.mem.count(u8, frame, open_prefix));
+ }
+}
diff --git a/TUI/root.zig b/TUI/root.zig
index 3d5981bb..12f090e1 100644
--- a/TUI/root.zig
+++ b/TUI/root.zig
@@ -87,6 +87,8 @@ test {
_ = @import("issue_537_reviewer_tests.zig");
_ = @import("issue_524_tests.zig");
_ = @import("issue_737_tests.zig");
+ _ = @import("issue_788_tests.zig");
+ _ = @import("composer_links.zig");
_ = @import("input.zig");
_ = @import("keys.zig");
_ = @import("nav.zig");