grace/tpb
Tee your Paste Board
git clone https://git.gae.moe/grace/tpb
63265d1
0.1.0
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 31if ( is_ssh ) { 32var clip = try Clip . OSC52 . init ( alloc , io , size ) ; 33defer clip . free () ; 34try run ( io , & clip ) ; 35return ; 36} 37 38if ( comptime os == . macos ) { 39var clip = try Clip . NSPasteboard . init ( alloc , io , size ) ; 40defer clip . free () ; 41try run ( io , & clip ) ; 42return ; 43} 44 45// Fallback Clipboard 46var clip = try Clip . OSC52 . init ( alloc , io , size ) ; 47defer clip . free () ; 48try run ( io , & clip ) ; 49} 50 51pub fn run ( io : std . Io , clip : anytype ) ! void { 52std . debug . ( " \x1b [0;32mCopying the following to Pasteboard \x1b [0m \n " , . {}) ; 53 54var buf : [ 4096 ] u8 = undefined ; 55while ( true ) { 56// Read as much of stdin there is 57const bytes = File . stdin () . readStreaming ( io , & . { & buf }) catch | err | { 58if ( err == File . ReadStreamingError . EndOfStream ) { 59break ; 60} else { 61return err ; 62} 63} ; 64// Passthrough to stdout 65_ = try File . stdout () . writeStreamingAll ( io , buf [ 0 .. bytes ]) ; 66 67// Write to clip 68try clip . writeCopyBuffer ( buf [ 0 .. bytes ]) ; 69} 70 71try clip . writePasteboard () ; 72 73std . debug . ( " \x1b [0;32mCopied To Pasteboard! \x1b [0m \n " , . {}) ; 74} 75 76test { 77_ = @import ( "Clip.zig" ) ; 78}