grace/tpb

Tee your Paste Board

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

Grace Yoderfeat: added ForkOut backend and now forks out to `xclip` and `wl-copy` on linuxc228995

0.1.1
2.8 KiB94 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    if (is_ssh) {
32        var clip = try Clip.OSC52.init(alloc, io, size);
33        defer clip.free();
34        try run(io, &clip);
35        return;
36    }
37
38    if (comptime os == .macos) {
39        var clip = try Clip.NSPasteboard.init(alloc, io, size);
40        defer clip.free();
41        try run(io, &clip);
42        return;
43    }
44
45    // I don't like this but it will stay until I review how dispatch works
46    // with zig since I will probably be refactoring there
47    linux: {
48        const xdg_type = init.environ_map.get("XDG_SESSION_TYPE") orelse "";
49
50        if (std.mem.eql(u8, xdg_type, "wayland")) {
51            var clip = Clip.ForkOut.init(io, &[_][]const u8{"wl-copy"}) catch break :linux;
52            try run(io, &clip);
53            return;
54        } else if (std.mem.eql(u8, xdg_type, "x11")) {
55            var clip = Clip.ForkOut.init(io, &[_][]const u8{ "xclip", "-selection", "clipboard" }) catch break :linux;
56            try run(io, &clip);
57            return;
58        }
59    }
60
61    // Fallback Clipboard
62    var clip = try Clip.OSC52.init(alloc, io, size);
63    defer clip.free();
64    try run(io, &clip);
65}
66
67pub fn run(io: std.Io, clip: anytype) !void {
68    std.debug.print("\x1b[0;32mCopying the following to Pasteboard\x1b[0m\n", .{});
69
70    var buf: [4096]u8 = undefined;
71    while (true) {
72        // Read as much of stdin there is
73        const bytes = File.stdin().readStreaming(io, &.{&buf}) catch |err| {
74            if (err == File.ReadStreamingError.EndOfStream) {
75                break;
76            } else {
77                return err;
78            }
79        };
80        // Passthrough to stdout
81        _ = try File.stdout().writeStreamingAll(io, buf[0..bytes]);
82
83        // Write to clip
84        try clip.writeCopyBuffer(buf[0..bytes]);
85    }
86
87    try clip.writePasteboard();
88
89    std.debug.print("\x1b[0;32mCopied To Pasteboard!\x1b[0m\n", .{});
90}
91
92test {
93    _ = @import("Clip.zig");
94}