aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/preview
blob: ff77782962ebba1fdbc5a5d239bc568f35df4859 (plain) (blame)
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
#!/usr/bin/env bash

path="$1"
lines="$2"

[ ! -e "$path" ] && { echo "$path not found" ; exit 1; }
[ -z "$lines" ] && lines=40

filetype=$(file -b "$path")

if grep -q "text" <<< "$filetype"; then
    filetype="text"
elif grep -q "archive" <<< "$filetpye"; then
    filetype="archive"
elif grep -q "ISO" <<< "$filetype" ; then
    filetype="iso"
elif grep -q "PDF" <<< "$filetype"; then
    filetype="pdf"
elif grep -q "SQLite 3" <<< "$filetype"; then
    filetype="sqlite"
fi

case $filetype in
    "directory")
        fd . "$path"
        ;;
    "text")
        bat -p -r ":$lines" --color=always "$path"
        ;;
    "archive")
        echo "archive preview not currently supported"
        file -b "$path"
        ;;
    "iso")
        isoinfo -l -i "$path" | head -n "$lines"
        ;;
    "pdf")
        info=$(pdfinfo "$path")
        remaining_lines=$(echo "$lines - $(echo $info | wc -l) -1" | bc)
        echo "$info"
        echo "--------------"
        if [ "$remaining_lines" -gt 0 ]; then
            pdftotext -nodiag -nopgbrk "$path" - | head -n "$remaining_lines"
        fi
        ;;
    "sqlite")
        file -b "$path"
        sqlite3 -readonly "$path" ".schema"
        ;;
    *)
        file -b "$path"
        ;;
esac