grace/tpb

Tee your Paste Board

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

Grace Yodermisc: preformance enhancementsf6cc169

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