-
Notifications
You must be signed in to change notification settings - Fork 3
/
fmawk-previewer
executable file
·150 lines (143 loc) · 5.04 KB
/
fmawk-previewer
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env sh
#fm.awk previewer script.
path=$1
cache=$2
b=$3
vRange=$4
top=$5
prevRange=$6
set -e
batorcat() {
if command -v bat > /dev/null 2>&1; then
bat --color=always --style=plain --pager=never "$1"
else
cat "$1"
fi
}
_preview_print () {
b=$1; e=$2; top=$3; shift 3
awk -F"\n" -v b="$b" -v e="$e" -v top="$top" '{i++; gsub(/\000\r/, "", $1); printf("\033[%s;%sH%s\n",top+i-1,b,$1); if (i >= e) exit }'
}
_check_dep () {
! command -v "$1" 1>/dev/null 2>&1 &&
printf "\033\13338;5;0m\033\13348;5;15m%s\033\133m" "ERROR: Dependency $1 not met." |
_preview_print "$b" "$vRange" "$top" "$@"
}
_fmawk_preview () {
[ -f "$cache.jpg" ] && rm "$cache.jpg" > /dev/null #If there's a remaining preview, remove it first (could prevent issues)
[ -p "$FIFO_UEBERZUG" ] && printf '{"action":"remove","identifier":"PREVIEW"}\n' > "$FIFO_UEBERZUG" #Remove Ueberzug preview if applicable.
case "$path" in
*.mp4|*.avi|*.wmv|*.3gp|*.ogv|*.mkv|*.mpg|*.mpeg|*.vob|*.mov|*.webm|*.ts|*.mts) #Video file preview.
prog="ffmpegthumbnailer"
_check_dep "$prog" ||
ffmpegthumbnailer -i "$path" -o "$cache.jpg" -c jpg -s0 -q5 2>/dev/null
fmawk_dest="$cache.jpg"
;;
*.pdf) #PDF preview.
prog="pdftoppm"
_check_dep "$prog" ||
pdftoppm -jpeg -f 1 -singlefile "$path" "$cache" 2>/dev/null
fmawk_dest="$cache.jpg"
;;
*.epub)
prog="epub-thumbnailer"
_check_dep "$prog" ||
epub-thumbnailer "$path" "$cache" 1024
fmawk_dest="$cache.jpg"
;;
*.bmp|*.jpg|*.jpeg|*.png|*.webp|*.gif|*.xmp|*.JPG) #Image preview
fmawk_dest="$path"
;;
*.tgz|*.tar.gz)
prog="tar"
_check_dep "$prog" ||
tar tzf "$path" | _preview_print "$b" "$vRange" "$top" "$@"
;;
*.tar.bz2|*.tbz2)
prog="tar"
_check_dep "$prog" ||
tar tjf "$path" | _preview_print "$b" "$vRange" "$top" "$@"
;;
*.tar.txz|*.txz)
prog="xz"
_check_dep "$prog" ||
xz --list "$path" | _preview_print "$b" "$vRange" "$top" "$@"
;;
*.tar)
prog="tar"
_check_dep "$prog" ||
tar tf "$path" | _preview_print "$b" "$vRange" "$top" "$@"
;;
*.gz)
prog="gunzip"
_check_dep "$prog" ||
gunzip -l "$path" | _preview_print "$b" "$vRange" "$top" "$@"
;;
*.zip|*.jar|*.war|*.ear|*.oxt)
prog="unzip"
_check_dep "$prog" ||
unzip -l "$path" | _preview_print "$b" "$vRange" "$top" "$@"
;;
*.rar)
prog="unrar"
_check_dep "$prog" ||
unrar l "$path" | _preview_print "$b" "$vRange" "$top" "$@"
;;
*.7z)
prog="7z"
_check_dep "$prog" ||
7z l "$path" | _preview_print "$b" "$vRange" "$top" "$@"
;;
*.[1-8])
man "$path" | col -b | _preview_print "$b" "$vRange" "$top" "$@"
;;
*.iso)
prog="iso-info"
_check_dep "$prog" ||
iso-info --no-header -l "$path" | _preview_print "$b" "$vRange" "$top" "$@"
;;
*.odt|*.ods|*.odp|*.sxw)
prog="odt2txt"
_check_dep "$prog" ||
odt2txt "$path" | _preview_print "$b" "$vRange" "$top" "$@"
;;
*.doc)
prog="catdoc"
_check_dep "$prog" ||
catdoc "$path" | _preview_print "$b" "$vRange" "$top" "$@"
;;
*.docx)
prog="docx2txt"
_check_dep "$prog" ||
docx2txt "$path" - | _preview_print "$b" "$vRange" "$top" "$@"
;;
*.xls|*.xlsx)
prog="ssconvert"
_check_dep "$prog" ||
ssconvert --export-type=Gnumeric_stf:stf_csv "$path" "fd://1" | batorcat --language=csv | _preview_print "$b" "$vRange" "$top" "$@"
;;
*.wav|*.mp3|*.flac|*.m4a|*.wma|*.ape|*.ac3|*.og[agx]|*.spx|*.opus|*.as[fx]|*.mka)
prog="exiftool"
_check_dep "$prog" ||
exiftool "$path" | _preview_print "$b" "$vRange" "$top" "$@"
;;
*)
batorcat "$path" | _preview_print "$b" "$vRange" "$top" "$@"
;;
esac
if [ -f "$fmawk_dest" ]; then #Confirm destination file.
if [ -p "$FIFO_UEBERZUG" ]; then #Use Ueberzug.
printf '{ "action":"add", "identifier":"PREVIEW", "x":"%s", "y":"%s", "width":"%s", "height":"%s", "scaler":"contain", "path":"%s" }\n' "$b" 1 "$prevRange" "$vRange" "$fmawk_dest" > "$FIFO_UEBERZUG"
else
if command -v chafa > /dev/null; then
chafa --size "$vRange" "$fmawk_dest" 2>/dev/null | _preview_print "$b" "$vRange" "$top" "$@"
else
printf "\033\13338;5;0m\033\13348;5;15m%s\033\133m" "Chafa not found. Unable to display."
fi
fi
fi
}
if [ -f "$path" ]; then #Ensure path exists first.
_fmawk_preview "$@"
fi
unset fmawk_dest