feat: added ForkOut backend and now forks out to xclip and wl-copy on linux
All checks were successful
ci/crow/push/release Pipeline was successful
ci/crow/tag/build Pipeline was successful

This commit is contained in:
Grace Yoder 2026-07-03 19:43:26 -06:00
parent 43c454a08c
commit c228995f20
Signed by: grace
SSH key fingerprint: SHA256:Wet3mU9H5QLc02gUBCI8mKRmHnkeOpnMIYsjx/YSbdQ
4 changed files with 43 additions and 1 deletions

View file

@ -1,6 +1,6 @@
.{ .{
.name = .tpb, .name = .tpb,
.version = "0.1.0", .version = "0.1.1",
.fingerprint = 0x203a601c46df0265, .fingerprint = 0x203a601c46df0265,
.minimum_zig_version = "0.16.0", .minimum_zig_version = "0.16.0",
.paths = .{ .paths = .{

View file

@ -7,6 +7,8 @@ pub const OSC52 = @import("Clip/OSC52.zig");
pub const NSPasteboard = pub const NSPasteboard =
if (builtin.target.os.tag == .macos) @import("Clip/NSPasteboard.zig") else void; if (builtin.target.os.tag == .macos) @import("Clip/NSPasteboard.zig") else void;
pub const ForkOut = @import("Clip/ForkOut.zig");
// I am not sure if this is super idiomatic Zig or if this is bad practice, but // I am not sure if this is super idiomatic Zig or if this is bad practice, but
// by ensuring that all Clip backends implement the same methods, we are able // by ensuring that all Clip backends implement the same methods, we are able
// to use the `anytype` type and we are able to have an interface that is able // to use the `anytype` type and we are able to have an interface that is able

24
src/Clip/ForkOut.zig Normal file
View file

@ -0,0 +1,24 @@
/// ForkOut Clipboard backend
/// Fulfills `Clip` interface
pub const ForkOut = @This();
const std = @import("std");
extern fn sendPB(text: [*:0]const u8) void;
extern fn initPB() void;
io: std.Io,
child: std.process.Child,
pub fn init(io: std.Io, argv: []const []const u8) !ForkOut {
return .{ .child = try std.process.spawn(io, .{ .argv = argv, .stdin = .pipe }), .io = io };
}
pub fn writeCopyBuffer(self: *ForkOut, buf: []const u8) anyerror!void {
try self.child.stdin.?.writeStreamingAll(self.io, buf);
}
pub fn writePasteboard(self: *ForkOut) anyerror!void {
self.child.stdin.?.close(self.io);
}

View file

@ -42,6 +42,22 @@ pub fn main(init: std.process.Init) !void {
return; return;
} }
// I don't like this but it will stay until I review how dispatch works
// with zig since I will probably be refactoring there
linux: {
const xdg_type = init.environ_map.get("XDG_SESSION_TYPE") orelse "";
if (std.mem.eql(u8, xdg_type, "wayland")) {
var clip = Clip.ForkOut.init(io, &[_][]const u8{"wl-copy"}) catch break :linux;
try run(io, &clip);
return;
} else if (std.mem.eql(u8, xdg_type, "x11")) {
var clip = Clip.ForkOut.init(io, &[_][]const u8{ "xclip", "-selection", "clipboard" }) catch break :linux;
try run(io, &clip);
return;
}
}
// Fallback Clipboard // Fallback Clipboard
var clip = try Clip.OSC52.init(alloc, io, size); var clip = try Clip.OSC52.init(alloc, io, size);
defer clip.free(); defer clip.free();