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