diff options
| author | Mitchell Hashimoto <m@mitchellh.com> | 2025-10-06 21:04:18 -0700 |
|---|---|---|
| committer | Mitchell Hashimoto <m@mitchellh.com> | 2025-10-06 21:09:28 -0700 |
| commit | bf9f025aec78aedcb9431d503fd6c4b14f579fbb (patch) | |
| tree | 3d0d82e9cc53d6bf3800d581c04b5d23cb8dfba5 /src | |
| parent | 5ece02fa76a6aefc0c617dc0ec27624951ef502b (diff) | |
lib-vt: begin paste utilities exports starting with safe paste
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib_vt.zig | 1 | ||||
| -rw-r--r-- | src/terminal/c/main.zig | 4 | ||||
| -rw-r--r-- | src/terminal/c/paste.zig | 36 |
3 files changed, 41 insertions, 0 deletions
diff --git a/src/lib_vt.zig b/src/lib_vt.zig index 73a030333..1df8330ea 100644 --- a/src/lib_vt.zig +++ b/src/lib_vt.zig @@ -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" }); } } diff --git a/src/terminal/c/main.zig b/src/terminal/c/main.zig index 500dbf56c..f68333d9b 100644 --- a/src/terminal/c/main.zig +++ b/src/terminal/c/main.zig @@ -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"); diff --git a/src/terminal/c/paste.zig b/src/terminal/c/paste.zig new file mode 100644 index 000000000..eb4117a70 --- /dev/null +++ b/src/terminal/c/paste.zig @@ -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)); +} |
