grace/tpb
Tee your Paste Board
git clone https://git.gae.moe/grace/tpb
2178c4d
0.1.2
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 { 9const io = init . io ; 10 11// This program should not be run interactively 12if ( try File . stdin () . isTty ( io )) { 13std . debug . ( 14\\ This program is not meant to be run interactively. 15\\ Please pipe into this program. 16\\ 17, . {}) ; 18std . 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 23const alloc = std . heap . page_allocator ; 24const size = std . heap . pageSize () ; 25 26const is_ssh = 27init . environ_map . get ( "SSH_CONNECTION" ) != null or 28init . environ_map . get ( "SSH_CLIENT" ) != null or 29init . environ_map . get ( "SSH_TTY" ) != null ; 30 31const is_tmux = 32init . environ_map . get ( "TMUX" ) != null ; 33 34if ( is_ssh ) { 35var clip = try Clip . OSC52 . init ( alloc , io , size , is_tmux ) ; 36defer clip . free () ; 37try run ( io , & clip ) ; 38return ; 39} 40 41if ( comptime os == . macos ) { 42var clip = try Clip . NSPasteboard . init ( alloc , io , size ) ; 43defer clip . free () ; 44try run ( io , & clip ) ; 45return ; 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 50linux : { 51if ( comptime os == . linux ) { 52const xdg_type = init . environ_map . get ( "XDG_SESSION_TYPE" ) orelse "" ; 53 54if ( std . mem . eql ( u8 , xdg_type , "wayland" )) { 55var clip = Clip . ForkOut . init ( io , & [ _ ][] const u8 { "wl-copy" }) catch break : linux ; 56try run ( io , & clip ) ; 57return ; 58} else if ( std . mem . eql ( u8 , xdg_type , "x11" )) { 59var clip = Clip . ForkOut . init ( io , & [ _ ][] const u8 { "xclip" , "-selection" , "clipboard" }) catch break : linux ; 60try run ( io , & clip ) ; 61return ; 62} 63} 64} 65 66// Fallback Clipboard 67var clip = try Clip . OSC52 . init ( alloc , io , size , is_tmux ) ; 68defer clip . free () ; 69try run ( io , & clip ) ; 70} 71 72pub fn run ( io : std . Io , clip : anytype ) ! void { 73std . debug . ( " \x1b [0;32mCopying the following to Pasteboard \x1b [0m \n " , . {}) ; 74 75var buf : [ 4096 ] u8 = undefined ; 76while ( true ) { 77// Read as much of stdin there is 78const bytes = File . stdin () . readStreaming ( io , & . { & buf }) catch | err | { 79if ( err == File . ReadStreamingError . EndOfStream ) { 80break ; 81} else { 82return err ; 83} 84} ; 85// Passthrough to stdout 86_ = try File . stdout () . writeStreamingAll ( io , buf [ 0 .. bytes ]) ; 87 88// Write to clip 89try clip . writeCopyBuffer ( buf [ 0 .. bytes ]) ; 90} 91 92try clip . writePasteboard () ; 93 94std . debug . ( " \x1b [0;32mCopied To Pasteboard! \x1b [0m \n " , . {}) ; 95} 96 97test { 98_ = @import ( "Clip.zig" ) ; 99}