it2copy (1519B)
1 #!/bin/bash 2 3 trap clean_up EXIT 4 trap clean_up INT 5 6 inosc=0 7 8 function clean_up() { 9 if [[ $inosc == 1 ]] 10 then 11 print_st 12 fi 13 } 14 15 function show_help() { 16 echo "Usage: $(basename $0)" 1>& 2 17 echo " Copies to clipboard from standard input" 1>& 2 18 echo " $(basename $0) filename" 1>& 2 19 echo " Copies to clipboard from file" 1>& 2 20 } 21 22 # tmux requires unrecognized OSC sequences to be wrapped with DCS tmux; 23 # <sequence> ST, and for all ESCs in <sequence> to be replaced with ESC ESC. It 24 # only accepts ESC backslash for ST. 25 function print_osc() { 26 if [[ $TERM == screen* ]] ; then 27 printf "\033Ptmux;\033\033]" 28 else 29 printf "\033]" 30 fi 31 } 32 33 # More of the tmux workaround described above. 34 function print_st() { 35 if [[ $TERM == screen* ]] ; then 36 printf "\a\033\\" 37 else 38 printf "\a" 39 fi 40 } 41 42 # Look for command line flags. 43 while [ $# -gt 0 ]; do 44 case "$1" in 45 -h|--h|--help) 46 show_help 47 exit 48 ;; 49 -*) 50 error "Unknown option flag: $1" 51 show_help 52 exit 1 53 ;; 54 *) 55 if [ -r "$1" ] ; then 56 data=$(base64 < "$1") 57 print_osc 58 inosc=1 59 printf '1337;Copy=:%s' "$data" 60 print_st 61 inosc=0 62 exit 0 63 else 64 error "it2copy: $1: No such file or directory" 65 exit 2 66 fi 67 ;; 68 esac 69 shift 70 done 71 72 data=$(base64) 73 print_osc 74 inosc=1 75 printf '1337;Copy=:%s' "$data" 76 print_st 77 inosc=0 78