grace/tpb

Tee your Paste Board

git clone https://git.gae.moe/grace/tpb

Grace Yoderfeat(OSC52): added tmux autodetect2178c4d

latest
2.9 KiB99 linesraw
1const std = @import("std");
2const builtin = @import("builtin");
3
4const os = builtin.target.os.tag;
5const File = std.Io.File;
6const Clip = @import("Clip.zig");
7
8pub fn main(init: std.process.Init) !void {
9    const io = init.io;
10
11    // This program should not be run interactively
12    if (try File.stdin().isTty(io)) {
13        std.debug.print(
14            \\ This program is not meant to be run interactively.
15            \\ Please pipe into this program.
16            \\
17        , .{});
18        std.process.exit(1);
19    }
20
21    // We want to avoid doing as many allocations as possible so having raw
22    // pages with it being larger should be totally fine
23    const alloc = std.heap.page_allocator;
24    const size = std.heap.pageSize();
25
26    const is_ssh =
27        init.environ_map.get("SSH_CONNECTION") != null or
28        init.environ_map.get("SSH_CLIENT") != null or
29        init.environ_map.get("SSH_TTY") != null;
30
31    const is_tmux =
32        init.environ_map.get("TMUX") != null;
33
34    if (is_ssh) {
35        var clip = try Clip.OSC52.init(alloc, io, size, is_tmux);
36        defer clip.free();
37        try run(io, &clip);
38        return;
39    }
40
41    if (comptime os == .macos) {
42        var clip = try Clip.NSPasteboard.init(alloc, io, size);
43        defer clip.free();
44        try run(io, &clip);
45        return;
46    }
47
48    // I don't like this but it will stay until I review how dispatch works
49    // with zig since I will probably be refactoring there
50    linux: {
51        if (comptime os == .linux) {
52            const xdg_type = init.environ_map.get("XDG_SESSION_TYPE") orelse "";
53
54            if (std.mem.eql(u8, xdg_type, "wayland")) {
55                var clip = Clip.ForkOut.init(io, &[_][]const u8{"wl-copy"}) catch break :linux;
56                try run(io, &clip);
57                return;
58            } else if (std.mem.eql(u8, xdg_type, "x11")) {
59                var clip = Clip.ForkOut.init(io, &[_][]const u8{ "xclip", "-selection", "clipboard" }) catch break :linux;
60                try run(io, &clip);
61                return;
62            }
63        }
64    }
65
66    // Fallback Clipboard
67    var clip = try Clip.OSC52.init(alloc, io, size, is_tmux);
68    defer clip.free();
69    try run(io, &clip);
70}
71
72pub fn run(io: std.Io, clip: anytype) !void {
73    std.debug.print("\x1b[0;32mCopying the following to Pasteboard\x1b[0m\n", .{});
74
75    var buf: [4096]u8 = undefined;
76    while (true) {
77        // Read as much of stdin there is
78        const bytes = File.stdin().readStreaming(io, &.{&buf}) catch |err| {
79            if (err == File.ReadStreamingError.EndOfStream) {
80                break;
81            } else {
82                return err;
83            }
84        };
85        // Passthrough to stdout
86        _ = try File.stdout().writeStreamingAll(io, buf[0..bytes]);
87
88        // Write to clip
89        try clip.writeCopyBuffer(buf[0..bytes]);
90    }
91
92    try clip.writePasteboard();
93
94    std.debug.print("\x1b[0;32mCopied To Pasteboard!\x1b[0m\n", .{});
95}
96
97test {
98    _ = @import("Clip.zig");
99}