libghostty: expose paste encode to C API

Add ghostty_paste_encode() which encodes paste data for writing to
the terminal pty. It strips unsafe control bytes, wraps in bracketed
paste sequences when requested, and replaces newlines with carriage
returns for unbracketed mode. The input buffer is modified in place
and the encoded result is written to a caller-provided output buffer,
following the same buffer/out_written pattern as the other encode
functions like ghostty_size_report_encode.

Update the c-vt-paste example with an encode_example() demonstrating
the new function and add corresponding @snippet references in the
header documentation.
This commit is contained in:
Mitchell Hashimoto
2026-03-26 11:26:56 -07:00
parent 6ebbd4785b
commit 11574c35a2
6 changed files with 165 additions and 6 deletions

View File

@@ -167,6 +167,7 @@ comptime {
@export(&c.focus_encode, .{ .name = "ghostty_focus_encode" });
@export(&c.mode_report_encode, .{ .name = "ghostty_mode_report_encode" });
@export(&c.paste_is_safe, .{ .name = "ghostty_paste_is_safe" });
@export(&c.paste_encode, .{ .name = "ghostty_paste_encode" });
@export(&c.size_report_encode, .{ .name = "ghostty_size_report_encode" });
@export(&c.style_default, .{ .name = "ghostty_style_default" });
@export(&c.style_is_default, .{ .name = "ghostty_style_is_default" });

View File

@@ -115,6 +115,7 @@ pub const mouse_encoder_reset = mouse_encode.reset;
pub const mouse_encoder_encode = mouse_encode.encode;
pub const paste_is_safe = paste.is_safe;
pub const paste_encode = paste.encode;
pub const alloc_alloc = allocator.alloc;
pub const alloc_free = allocator.free;

View File

@@ -1,12 +1,104 @@
const std = @import("std");
const lib = @import("../lib.zig");
const paste = @import("../../input/paste.zig");
const Result = @import("result.zig").Result;
pub fn is_safe(data: ?[*]const u8, len: usize) callconv(lib.calling_conv) bool {
const slice: []const u8 = if (data) |v| v[0..len] else &.{};
return paste.isSafe(slice);
}
pub fn encode(
data: ?[*]u8,
data_len: usize,
bracketed: bool,
out_: ?[*]u8,
out_len: usize,
out_written: *usize,
) callconv(lib.calling_conv) Result {
const slice: []u8 = if (data) |v| v[0..data_len] else &.{};
const result = paste.encode(slice, .{ .bracketed = bracketed });
const total = result[0].len + result[1].len + result[2].len;
out_written.* = total;
const out: []u8 = if (out_) |o| o[0..out_len] else &.{};
if (out.len < total) return .out_of_space;
var offset: usize = 0;
for (result) |segment| {
@memcpy(out[offset..][0..segment.len], segment);
offset += segment.len;
}
return .success;
}
test "encode bracketed" {
const testing = std.testing;
const input = try testing.allocator.dupe(u8, "hello");
defer testing.allocator.free(input);
var buf: [64]u8 = undefined;
var written: usize = 0;
const result = encode(input.ptr, input.len, true, &buf, buf.len, &written);
try testing.expectEqual(.success, result);
try testing.expectEqualStrings("\x1b[200~hello\x1b[201~", buf[0..written]);
}
test "encode unbracketed no newlines" {
const testing = std.testing;
const input = try testing.allocator.dupe(u8, "hello");
defer testing.allocator.free(input);
var buf: [64]u8 = undefined;
var written: usize = 0;
const result = encode(input.ptr, input.len, false, &buf, buf.len, &written);
try testing.expectEqual(.success, result);
try testing.expectEqualStrings("hello", buf[0..written]);
}
test "encode unbracketed newlines" {
const testing = std.testing;
const input = try testing.allocator.dupe(u8, "hello\nworld");
defer testing.allocator.free(input);
var buf: [64]u8 = undefined;
var written: usize = 0;
const result = encode(input.ptr, input.len, false, &buf, buf.len, &written);
try testing.expectEqual(.success, result);
try testing.expectEqualStrings("hello\rworld", buf[0..written]);
}
test "encode strip unsafe bytes" {
const testing = std.testing;
const input = try testing.allocator.dupe(u8, "hel\x1blo\x00world");
defer testing.allocator.free(input);
var buf: [64]u8 = undefined;
var written: usize = 0;
const result = encode(input.ptr, input.len, true, &buf, buf.len, &written);
try testing.expectEqual(.success, result);
try testing.expectEqualStrings("\x1b[200~hel lo world\x1b[201~", buf[0..written]);
}
test "encode with insufficient buffer" {
const testing = std.testing;
const input = try testing.allocator.dupe(u8, "hello");
defer testing.allocator.free(input);
var buf: [1]u8 = undefined;
var written: usize = 0;
const result = encode(input.ptr, input.len, true, &buf, buf.len, &written);
try testing.expectEqual(.out_of_space, result);
try testing.expectEqual(17, written);
}
test "encode with null buffer" {
const testing = std.testing;
const input = try testing.allocator.dupe(u8, "hello");
defer testing.allocator.free(input);
var written: usize = 0;
const result = encode(input.ptr, input.len, true, null, 0, &written);
try testing.expectEqual(.out_of_space, result);
try testing.expectEqual(17, written);
}
test "is_safe with safe data" {
const testing = std.testing;
const safe = "hello world";