From ec524f039c7af886a26bb401ba88eae4a5d31285 Mon Sep 17 00:00:00 2001 From: Grace Yoder Date: Mon, 6 Jul 2026 09:49:35 -0600 Subject: [PATCH 1/3] fix: added os comptime check for running ForkOut --- src/main.zig | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main.zig b/src/main.zig index 60b498d..36c5bde 100644 --- a/src/main.zig +++ b/src/main.zig @@ -45,16 +45,18 @@ pub fn main(init: std.process.Init) !void { // 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 (comptime os == .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; + 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; + } } } From 130e668dd511466797d0fb75c0ef23e0c2e382cb Mon Sep 17 00:00:00 2001 From: Grace Yoder Date: Mon, 6 Jul 2026 13:35:03 -0600 Subject: [PATCH 2/3] feat(OSC52): added tmux mode to use tmux passthrough --- src/Clip/OSC52.zig | 62 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/src/Clip/OSC52.zig b/src/Clip/OSC52.zig index 8b751f5..1d0ce5b 100644 --- a/src/Clip/OSC52.zig +++ b/src/Clip/OSC52.zig @@ -12,6 +12,8 @@ base64buffer: std.ArrayList(u8), leftover_bytes: [3]u8, leftover_count: u8, +is_tmux: bool, + const base64encoder = std.base64.Base64Encoder.init(std.base64.standard_alphabet_chars, '='); @@ -22,6 +24,7 @@ pub fn init(alloc: std.mem.Allocator, io: std.Io, buf_size: usize) !OSC52 { .base64buffer = try std.ArrayList(u8).initCapacity(alloc, buf_size), .leftover_bytes = undefined, .leftover_count = 0, + .is_tmux = false, }; } @@ -71,25 +74,54 @@ pub fn writePasteboard(self: *OSC52) anyerror!void { try self.encodeRemaining(); // https://ghostty.org/docs/vt/osc/52 - const osc_header = &[_]u8{ - 0x1b, // ESC - 0x5d, // ']' - 0x35, 0x32, // Code 52 - 0x3b, // ';' - // 0x70, // 'p' Primary Clipboard - 0x63, // 'c' Standard Clipboard - 0x3b, // ';' - }; - const osc_footer = &[_]u8{ - 0x1b, // ESC - 0x5c, // '\' - }; // Write data to stdout const stdout = std.Io.File.stdout(); - try stdout.writeStreamingAll(self.io, osc_header); + try stdout.writeStreamingAll(self.io, self.osc_header()); try stdout.writeStreamingAll(self.io, self.base64buffer.items); - try stdout.writeStreamingAll(self.io, osc_footer); + try stdout.writeStreamingAll(self.io, self.osc_footer()); +} + +fn osc_header(self: *OSC52) []const u8 { + if (self.is_tmux) { + return &[_]u8{ + 0x1b, // ESC + 'P', 't', 'm', 'u', 'x', ';', // tmux escape + 0x1b, 0x1b, // ESC ESC + 0x5d, // ']' + 0x35, 0x32, // Code 52 + 0x3b, // ';' + // 0x70, // 'p' Primary Clipboard + 0x63, // 'c' Standard Clipboard + 0x3b, // ';' + }; + } else { + return &[_]u8{ + 0x1b, // ESC + 0x5d, // ']' + 0x35, 0x32, // Code 52 + 0x3b, // ';' + // 0x70, // 'p' Primary Clipboard + 0x63, // 'c' Standard Clipboard + 0x3b, // ';' + }; + } +} + +fn osc_footer(self: *OSC52) []const u8 { + if (self.is_tmux) { + return &[_]u8{ + 0x1b, 0x1b, // ESC ESC + 0x5c, // '\' + 0x1b, // ESC + 0x5c, // '\' + }; + } else { + return &[_]u8{ + 0x1b, // ESC + 0x5c, // '\' + }; + } } test "basic base64 encode" { From 2178c4d83916eadade432906838e844efd7f63d1 Mon Sep 17 00:00:00 2001 From: Grace Yoder Date: Mon, 6 Jul 2026 14:22:52 -0600 Subject: [PATCH 3/3] feat(OSC52): added tmux autodetect --- build.zig.zon | 2 +- src/Clip/OSC52.zig | 22 ++++++++++++++++------ src/main.zig | 7 +++++-- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/build.zig.zon b/build.zig.zon index 14a1d13..e0a2a4b 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,6 +1,6 @@ .{ .name = .tpb, - .version = "0.1.1", + .version = "0.1.2", .fingerprint = 0x203a601c46df0265, .minimum_zig_version = "0.16.0", .paths = .{ diff --git a/src/Clip/OSC52.zig b/src/Clip/OSC52.zig index 1d0ce5b..e52c6fc 100644 --- a/src/Clip/OSC52.zig +++ b/src/Clip/OSC52.zig @@ -12,19 +12,29 @@ base64buffer: std.ArrayList(u8), leftover_bytes: [3]u8, leftover_count: u8, +/// This is to wrap the OSC buffer in the tmux passthrough sequence. This lets +/// OSC52 clipboard requests go directly to the host instead of going to a tmux +/// buffer. +/// https://github.com/tmux/tmux/wiki/FAQ#what-is-the-passthrough-escape-sequence-and-how-do-i-use-it +/// Since tmux v3.7, there is a `get-clipboard` option and when it is set to +/// `request` any OSC52 query that comes to it will cause the tmux buffer to +/// update before sending the information. +/// https://raw.githubusercontent.com/tmux/tmux/3.7b/CHANGES +/// This still wraps the tmux session in an escape whenever one is detected +/// since this option is still incredibly new. is_tmux: bool, const base64encoder = std.base64.Base64Encoder.init(std.base64.standard_alphabet_chars, '='); -pub fn init(alloc: std.mem.Allocator, io: std.Io, buf_size: usize) !OSC52 { +pub fn init(alloc: std.mem.Allocator, io: std.Io, buf_size: usize, is_tmux: bool) !OSC52 { return .{ .alloc = alloc, .io = io, .base64buffer = try std.ArrayList(u8).initCapacity(alloc, buf_size), .leftover_bytes = undefined, .leftover_count = 0, - .is_tmux = false, + .is_tmux = is_tmux, }; } @@ -125,7 +135,7 @@ fn osc_footer(self: *OSC52) []const u8 { } test "basic base64 encode" { - var osc = try OSC52.init(std.testing.allocator, std.testing.io, 1024); + var osc = try OSC52.init(std.testing.allocator, std.testing.io, 1024, false); defer osc.free(); try osc.writeCopyBuffer("part1"); try osc.encodeRemaining(); @@ -133,7 +143,7 @@ test "basic base64 encode" { } test "multi part base64 encode" { - var osc = try OSC52.init(std.testing.allocator, std.testing.io, 1024); + var osc = try OSC52.init(std.testing.allocator, std.testing.io, 1024, false); defer osc.free(); try osc.writeCopyBuffer("part1"); try osc.writeCopyBuffer("part2"); @@ -142,7 +152,7 @@ test "multi part base64 encode" { } test "tiny buffers base64 encode" { - var osc = try OSC52.init(std.testing.allocator, std.testing.io, 1024); + var osc = try OSC52.init(std.testing.allocator, std.testing.io, 1024, false); defer osc.free(); try osc.writeCopyBuffer("part1"); try osc.writeCopyBuffer("part2"); @@ -158,7 +168,7 @@ test "tiny buffers base64 encode" { } test "big buffers base64 encode" { - var osc = try OSC52.init(std.testing.allocator, std.testing.io, 1024); + var osc = try OSC52.init(std.testing.allocator, std.testing.io, 1024, false); defer osc.free(); try osc.writeCopyBuffer("part1part1part1part1part1part1part1part1part1|"); try osc.writeCopyBuffer("part2part2part2part2part2part2part2part2part2"); diff --git a/src/main.zig b/src/main.zig index 36c5bde..9ae524b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -28,8 +28,11 @@ pub fn main(init: std.process.Init) !void { init.environ_map.get("SSH_CLIENT") != null or init.environ_map.get("SSH_TTY") != null; + const is_tmux = + init.environ_map.get("TMUX") != null; + if (is_ssh) { - var clip = try Clip.OSC52.init(alloc, io, size); + var clip = try Clip.OSC52.init(alloc, io, size, is_tmux); defer clip.free(); try run(io, &clip); return; @@ -61,7 +64,7 @@ pub fn main(init: std.process.Init) !void { } // Fallback Clipboard - var clip = try Clip.OSC52.init(alloc, io, size); + var clip = try Clip.OSC52.init(alloc, io, size, is_tmux); defer clip.free(); try run(io, &clip); }