diff --git a/src/Clip.zig b/src/Clip.zig index b4e1187..299d4c6 100644 --- a/src/Clip.zig +++ b/src/Clip.zig @@ -4,7 +4,8 @@ const builtin = @import("builtin"); pub const OSC52 = @import("Clip/OSC52.zig"); // Ideally we would make else be `@compileError` but that // messes up with the interface checking -pub const NSPasteboard = if (builtin.target.os.tag == .macos) @import("Clip/NSPasteboard.zig") else void; +pub const NSPasteboard = + if (builtin.target.os.tag == .macos) @import("Clip/NSPasteboard.zig") else void; // I am not sure if this is super idiomatic Zig or if this is bad practice, but // by ensuring that all Clip backends implement the same methods, we are able diff --git a/src/Clip/OSC52.zig b/src/Clip/OSC52.zig index d95fca1..8b751f5 100644 --- a/src/Clip/OSC52.zig +++ b/src/Clip/OSC52.zig @@ -3,7 +3,6 @@ pub const OSC52 = @This(); const std = @import("std"); -const Io = std.Io; alloc: std.mem.Allocator, io: std.Io, @@ -13,9 +12,10 @@ base64buffer: std.ArrayList(u8), leftover_bytes: [3]u8, leftover_count: u8, -const base64encoder = std.base64.Base64Encoder.init(std.base64.standard_alphabet_chars, '='); +const base64encoder = + std.base64.Base64Encoder.init(std.base64.standard_alphabet_chars, '='); -pub fn init(alloc: std.mem.Allocator, io: Io, buf_size: usize) !OSC52 { +pub fn init(alloc: std.mem.Allocator, io: std.Io, buf_size: usize) !OSC52 { return .{ .alloc = alloc, .io = io, @@ -51,7 +51,6 @@ pub fn writeCopyBuffer(self: *OSC52, buf: []const u8) anyerror!void { const chunkable_length = buf.len - ((buf.len - buf_idx) % 3); var chunker = std.mem.window(u8, buf[buf_idx..chunkable_length], 3, 3); while (chunker.next()) |chunk| { - // var temp: [5]u8 = undefined; s = OSC52.base64encoder.encode(&temp, chunk); try self.base64buffer.appendSlice(self.alloc, s); } @@ -87,7 +86,7 @@ pub fn writePasteboard(self: *OSC52) anyerror!void { }; // Write data to stdout - const stdout = Io.File.stdout(); + const stdout = std.Io.File.stdout(); try stdout.writeStreamingAll(self.io, osc_header); try stdout.writeStreamingAll(self.io, self.base64buffer.items); try stdout.writeStreamingAll(self.io, osc_footer); diff --git a/src/main.zig b/src/main.zig index 0725ae7..bb0aa14 100644 --- a/src/main.zig +++ b/src/main.zig @@ -10,7 +10,11 @@ pub fn main(init: std.process.Init) !void { // 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.\n", .{}); + std.debug.print( + \\ This program is not meant to be run interactively. + \\ Please pipe into this program. + \\ + , .{}); std.process.exit(1); } @@ -19,27 +23,29 @@ pub fn main(init: std.process.Init) !void { const alloc = std.heap.page_allocator; const size = std.heap.pageSize(); - const is_ssh = init.environ_map.get("SSH_CONNECTION") != null or + 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; - // This can be cleaned up if (is_ssh) { var clip = try Clip.OSC52.init(alloc, io, size); defer clip.free(); try run(io, &clip); - } else { - if (comptime os == .macos) { - var clip = try Clip.NSPasteboard.init(alloc, io, size); - defer clip.free(); - try run(io, &clip); - } else { - // No Linux Clipboards yet - var clip = try Clip.OSC52.init(alloc, io, size); - 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; + } + + // Fallback Clipboard + var clip = try Clip.OSC52.init(alloc, io, size); + defer clip.free(); + try run(io, &clip); } pub fn run(io: std.Io, clip: anytype) !void {