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