lib-vt: begin paste utilities exports starting with safe paste

This commit is contained in:
Mitchell Hashimoto
2025-10-06 21:04:18 -07:00
parent 5ece02fa76
commit bf9f025aec
10 changed files with 239 additions and 1 deletions

View File

@@ -122,6 +122,7 @@ comptime {
@export(&c.key_encoder_free, .{ .name = "ghostty_key_encoder_free" });
@export(&c.key_encoder_setopt, .{ .name = "ghostty_key_encoder_setopt" });
@export(&c.key_encoder_encode, .{ .name = "ghostty_key_encoder_encode" });
@export(&c.paste_is_safe, .{ .name = "ghostty_paste_is_safe" });
}
}

View File

@@ -1,6 +1,7 @@
pub const osc = @import("osc.zig");
pub const key_event = @import("key_event.zig");
pub const key_encode = @import("key_encode.zig");
pub const paste = @import("paste.zig");
// The full C API, unexported.
pub const osc_new = osc.new;
@@ -33,10 +34,13 @@ pub const key_encoder_free = key_encode.free;
pub const key_encoder_setopt = key_encode.setopt;
pub const key_encoder_encode = key_encode.encode;
pub const paste_is_safe = paste.is_safe;
test {
_ = osc;
_ = key_event;
_ = key_encode;
_ = paste;
// We want to make sure we run the tests for the C allocator interface.
_ = @import("../../lib/allocator.zig");

36
src/terminal/c/paste.zig Normal file
View File

@@ -0,0 +1,36 @@
const std = @import("std");
const paste = @import("../../input/paste.zig");
pub fn is_safe(data: ?[*]const u8, len: usize) callconv(.c) bool {
const slice: []const u8 = if (data) |v| v[0..len] else &.{};
return paste.isSafe(slice);
}
test "is_safe with safe data" {
const testing = std.testing;
const safe = "hello world";
try testing.expect(is_safe(safe.ptr, safe.len));
}
test "is_safe with newline" {
const testing = std.testing;
const unsafe = "hello\nworld";
try testing.expect(!is_safe(unsafe.ptr, unsafe.len));
}
test "is_safe with bracketed paste end" {
const testing = std.testing;
const unsafe = "hello\x1b[201~world";
try testing.expect(!is_safe(unsafe.ptr, unsafe.len));
}
test "is_safe with empty data" {
const testing = std.testing;
const empty = "";
try testing.expect(is_safe(empty.ptr, 0));
}
test "is_safe with null empty data" {
const testing = std.testing;
try testing.expect(is_safe(null, 0));
}