imgcat (3346B)
1 #!/bin/bash 2 3 # tmux requires unrecognized OSC sequences to be wrapped with DCS tmux; 4 # <sequence> ST, and for all ESCs in <sequence> to be replaced with ESC ESC. It 5 # only accepts ESC backslash for ST. We use TERM instead of TMUX because TERM 6 # gets passed through ssh. 7 function print_osc() { 8 if [[ $TERM == screen* ]] ; then 9 printf "\033Ptmux;\033\033]" 10 else 11 printf "\033]" 12 fi 13 } 14 15 # More of the tmux workaround described above. 16 function print_st() { 17 if [[ $TERM == screen* ]] ; then 18 printf "\a\033\\" 19 else 20 printf "\a" 21 fi 22 } 23 24 function load_version() { 25 if [ -z ${IMGCAT_BASE64_VERSION+x} ]; then 26 export IMGCAT_BASE64_VERSION=$(base64 --version 2>&1) 27 fi 28 } 29 30 function b64_encode() { 31 load_version 32 if [[ "$IMGCAT_BASE64_VERSION" =~ GNU ]]; then 33 # Disable line wrap 34 base64 -w0 35 else 36 base64 37 fi 38 } 39 40 function b64_decode() { 41 load_version 42 if [[ "$IMGCAT_BASE64_VERSION" =~ fourmilab ]]; then 43 BASE64ARG=-d 44 elif [[ "$IMGCAT_BASE64_VERSION" =~ GNU ]]; then 45 BASE64ARG=-di 46 else 47 BASE64ARG=-D 48 fi 49 base64 $BASE64ARG 50 } 51 52 # print_image filename inline base64contents print_filename 53 # filename: Filename to convey to client 54 # inline: 0 or 1 55 # base64contents: Base64-encoded contents 56 # print_filename: If non-empty, print the filename 57 # before outputting the image 58 function print_image() { 59 print_osc 60 printf '1337;File=' 61 if [[ -n "$1" ]]; then 62 printf 'name='`printf "%s" "$1" | b64_encode`";" 63 fi 64 65 printf "%s" "$3" | b64_decode | wc -c | awk '{printf "size=%d",$1}' 66 printf ";inline=$2" 67 printf ":" 68 printf "%s" "$3" 69 print_st 70 printf '\n' 71 if [[ -n "$4" ]]; then 72 echo $1 73 fi 74 } 75 76 function error() { 77 echo "ERROR: $*" 1>&2 78 } 79 80 function show_help() { 81 echo "Usage: imgcat [-p] filename ..." 1>& 2 82 echo " or: cat filename | imgcat" 1>& 2 83 } 84 85 function check_dependency() { 86 if ! (builtin command -V "$1" > /dev/null 2>& 1); then 87 echo "imgcat: missing dependency: can't find $1" 1>& 2 88 exit 1 89 fi 90 } 91 92 ## Main 93 94 if [ -t 0 ]; then 95 has_stdin=f 96 else 97 has_stdin=t 98 fi 99 100 # Show help if no arguments and no stdin. 101 if [ $has_stdin = f -a $# -eq 0 ]; then 102 show_help 103 exit 104 fi 105 106 check_dependency awk 107 check_dependency base64 108 check_dependency wc 109 110 # Look for command line flags. 111 while [ $# -gt 0 ]; do 112 case "$1" in 113 -h|--h|--help) 114 show_help 115 exit 116 ;; 117 -p|--p|--print) 118 print_filename=1 119 ;; 120 -u|--u|--url) 121 check_dependency curl 122 encoded_image=$(curl -s "$2" | b64_encode) || (error "No such file or url $2"; exit 2) 123 has_stdin=f 124 print_image "$2" 1 "$encoded_image" "$print_filename" 125 set -- ${@:1:1} "-u" ${@:3} 126 if [ "$#" -eq 2 ]; then 127 exit 128 fi 129 ;; 130 -*) 131 error "Unknown option flag: $1" 132 show_help 133 exit 1 134 ;; 135 *) 136 if [ -r "$1" ] ; then 137 has_stdin=f 138 print_image "$1" 1 "$(b64_encode < "$1")" "$print_filename" 139 else 140 error "imgcat: $1: No such file or directory" 141 exit 2 142 fi 143 ;; 144 esac 145 shift 146 done 147 148 # Read and print stdin 149 if [ $has_stdin = t ]; then 150 print_image "" 1 "$(cat | b64_encode)" "" 151 fi 152 153 exit 0