aboutsummaryrefslogtreecommitdiff
path: root/dot_config/lf/executable_previewer_kitty
blob: b23642fea8420636e72c58eb0aa588b6ffeb99ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/sh

k_draw() {
  kitten icat --stdin no --transfer-mode memory --place "${w}x${h}@${x}x${y}" "$1" </dev/null >/dev/tty
  exit 1
}

file="$1"
w="$2"
h="$3"
x="$4"
y="$5"
CACHE_DIR="${TMPDIR:-$HOME/.cache}/pv_cache"
mkdir "${CACHE_DIR}"

#FILE_LOC="$(pwd)/${file}"
TMP="$(head -c 1024 "${file}" | cksum -a md5)"
CACHE_F="${CACHE_DIR}/${TMP##* }"

case "$(file -Lb --mime-type "${file}")" in
    image/*)
        k_draw "${file}"
        ;;
    text/*|application/javascript|application/json)
        highlight -O ansi "${file}"
        ;;
    application/zstd|application/x-xz|application/gzip|application/x-bzip2|application/zlib)
        [ ! -e "${CACHE_F}" ] && tar -tf "${file}" > "${CACHE_F}"
        echo "Contents:"
        cat "${CACHE_F}"
        ;;
    application/zip)
        [ ! -e "${CACHE_F}" ] && unzip -l "${file}" > "${CACHE_F}"
        cat "${CACHE_F}"
        ;;
    application/pdf)
        [ ! -e "${CACHE_F}" ] && pdftoppm -png -singlefile "${file}" > "${CACHE_F}"
        k_draw "${CACHE_F}"
        ;;
    video/*|audio/*)
        [ ! -e "${CACHE_F}" ] && ffmpegthumbnailer -s0 -i "${file}" -o "${CACHE_F}"
        k_draw "${CACHE_F}"
        ;;
    application/octet-stream)
        case "${file}" in
            *.stl|*.STL)
                [ ! -e "${CACHE_F}" ] && simple-thumbnailer-stl -i "${file}" -o "${CACHE_F}" -s 600 -S 16
                k_draw "${CACHE_F}"
                ;;
            *.h264)
                [ ! -e "${CACHE_F}" ] && ffmpegthumbnailer -s0 -i "${file}" -o "${CACHE_F}"
                k_draw "${CACHE_F}"
                ;;
            *)
        	file -b "$1"
        	;;
        esac
        ;;
    *)
        file -b "$1"
esac