Merge commit '77d3a60869e7a780c6ae069e51530e1eacece5e2'
[fanfix.git] / src / jexer / examples / imgls
1 #!/bin/bash
2
3 # This is a modified version of the 'imgls' from iTerm2 located at
4 # https://iterm2.com/utilities/imgls, modified to emit images with the
5 # Jexer image protocol.
6
7 # tmux requires unrecognized OSC sequences to be wrapped with DCS tmux;
8 # <sequence> ST, and for all ESCs in <sequence> to be replaced with ESC ESC. It
9 # only accepts ESC backslash for ST.
10 function print_osc() {
11 if [ x"$TERM" = "xscreen" ] ; then
12 printf "\033Ptmux;\033\033]"
13 else
14 printf "\033]"
15 fi
16 }
17
18 function check_dependency() {
19 if ! (builtin command -V "$1" > /dev/null 2>& 1); then
20 echo "imgcat: missing dependency: can't find $1" 1>& 2
21 exit 1
22 fi
23 }
24
25 # More of the tmux workaround described above.
26 function print_st() {
27 if [ x"$TERM" = "xscreen" ] ; then
28 printf "\a\033\\"
29 else
30 printf "\a"
31 fi
32 }
33
34 function list_file() {
35 fn=$1
36 test -f "$fn" || return 0
37
38 if [ "${fn: -4}" == ".png" ]; then
39 print_osc
40 printf '444;1;1;'
41 base64 < "$fn"
42 print_st
43 elif [ "${fn: -4}" == ".jpg" ]; then
44 print_osc
45 printf '444;2;1;'
46 base64 < "$fn"
47 print_st
48 fi
49 }
50
51 check_dependency base64
52
53 if [ $# -eq 0 ]; then
54 for fn in *
55 do
56 list_file "$fn"
57 done < <(ls -ls)
58 else
59 for fn in "$@"
60 do
61 list_file "$fn"
62 done
63 fi
64