imgls (2238B)
1 #!/bin/bash 2 # tmux requires unrecognized OSC sequences to be wrapped with DCS tmux; 3 # <sequence> ST, and for all ESCs in <sequence> to be replaced with ESC ESC. It 4 # only accepts ESC backslash for ST. 5 function print_osc() { 6 if [ x"$TERM" = "xscreen" ] ; then 7 printf "\033Ptmux;\033\033]" 8 else 9 printf "\033]" 10 fi 11 } 12 13 function check_dependency() { 14 if ! (builtin command -V "$1" > /dev/null 2>& 1); then 15 echo "imgcat: missing dependency: can't find $1" 1>& 2 16 exit 1 17 fi 18 } 19 20 # More of the tmux workaround described above. 21 function print_st() { 22 if [ x"$TERM" = "xscreen" ] ; then 23 printf "\a\033\\" 24 else 25 printf "\a" 26 fi 27 } 28 29 function list_file() { 30 fn=$1 31 test -f "$fn" || return 0 32 dims=$(php -r 'if (!is_file($argv[1])) exit(1); $a = getimagesize($argv[1]); if ($a==FALSE) exit(1); else { echo $a[0] . "x" .$a[1]; exit(0); }' -- "$fn") 33 rc=$? 34 if [[ $rc == 0 ]] ; then 35 print_osc 36 printf '1337;File=name='`echo -n "$fn" | base64`";" 37 wc -c -- "$fn" | awk '{printf "size=%d",$1}' 38 printf ";inline=1;height=3;width=3;preserveAspectRatio=true" 39 printf ":" 40 base64 < "$fn" 41 print_st 42 if [ x"$TERM" == "xscreen" ] ; then 43 # This works in plain-old tmux but does the wrong thing in iTerm2's tmux 44 # integration mode. tmux doesn't know that the cursor moves when the 45 # image code is sent, while iTerm2 does. I had to pick one, since 46 # integration mode is undetectable, so I picked the failure mode that at 47 # least produces useful output (there is just too much whitespace in 48 # integration mode). This could be fixed by not moving the cursor while 49 # in integration mode. A better fix would be for tmux to interpret the 50 # image sequence, though. 51 # 52 # tl;dr: If you use tmux in integration mode, replace this with the printf 53 # from the else clause. 54 printf '\033[4C\033[Bx' 55 else 56 printf '\033[A' 57 fi 58 echo -n "$dims " 59 ls -ld -- "$fn" 60 else 61 ls -ld -- "$fn" 62 fi 63 } 64 65 check_dependency php 66 check_dependency base64 67 check_dependency wc 68 69 if [ $# -eq 0 ]; then 70 for fn in * 71 do 72 list_file "$fn" 73 done < <(ls -ls) 74 else 75 for fn in "$@" 76 do 77 list_file "$fn" 78 done 79 fi 80