-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreview
executable file
·220 lines (203 loc) · 4.4 KB
/
preview
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/bin/sh
# this file in public domain
previewdir="${CACHEDIR:-"${XDG_CACHE_HOME:-"$HOME/.cache"}"}/preview"
# show usage
usage() {
echo "usage: preview filename [w h x y]" >&2
exit 1
}
# get file extension
getextension() {
echo "${1##*.}" | tr '[:upper:]' '[:lower:]'
}
# get file mimetype
getfilemime() {
file -ib "$@" | cut -d';' -f1
}
# get cached file
getcache() {
mkdir -p "$previewdir"
printf "%s/%s" "$previewdir" "$(stat "$1" | sha256).png"
}
# preview image
img() {
PATH="$PATH:/usr/local/lib/w3m/"
PATH="$PATH:/usr/local/libexec/w3m/"
PATH="$PATH:/usr/local/lib64/w3m/"
PATH="$PATH:/usr/local/libexec64/w3m/"
PATH="$PATH:/usr/lib/w3m/"
PATH="$PATH:/usr/libexec/w3m/"
PATH="$PATH:/usr/lib64/w3m/"
PATH="$PATH:/usr/libexec64/w3m/"
filename="$1"
{
# get information about terminal X11 window
xdotool getwindowgeometry --shell "$WINDOWID" 2>/dev/null | cut -d= -f2
# get information about terminal character column and row
stty -f /dev/tty size
# get information about image
printf "5;%s\n" "$filename" | w3mimgdisplay
} | tr '\n' ' ' | {
read -r WIN_ID WIN_X WIN_Y WIN_W WIN_H SCREEN LINES COLUMNS IMG_W IMG_H
# do nothing on dummy variables, just for shellcheck to be happy
: "$WIN_ID" "$WIN_X" "$WIN_Y" "$SCREEN"
# compute stuff
FONT_W="$(( WIN_W / COLUMNS ))"
FONT_H="$(( WIN_H / LINES ))"
# perform w3m(1) black magic
if test "$cmdname" = "unpreview"
then
FINAL_X="$(( FONT_W * ( X + 1 ) ))"
FINAL_Y="$(( FONT_H * ( Y + 1 ) ))"
FINAL_W="$(( FONT_W * ( W - 2 ) ))"
FINAL_H="$(( FONT_H * ( H - 2 ) ))"
FINAL_W="$((FINAL_W + FONT_W))"
FINAL_H="$((FINAL_H + FONT_H))"
printf '6;%s;%s;%s;%s;\n4;\n3;\n' \
"$FINAL_X" "$FINAL_Y" "$FINAL_W" "$FINAL_H" | w3mimgdisplay
else
MAX_W="$(( FONT_W * (W - 3) ))"
MAX_H="$(( FONT_H * (H - 3) ))"
FINAL_X="$(( FONT_W * (X + 1) ))"
FINAL_Y="$(( FONT_H * (Y + 1) ))"
FINAL_W="$IMG_W"
FINAL_H="$IMG_H"
# resize image
if test "$FINAL_H" -gt "$MAX_H"
then
FINAL_W="$(( FINAL_W * MAX_H / FINAL_H ))"
FINAL_H="$MAX_H"
fi
if test "$FINAL_W" -gt "$MAX_W"
then
FINAL_H="$(( FINAL_H * MAX_W / FINAL_W ))"
FINAL_W="$MAX_W"
fi
printf '6;%s;%s;%s;%s;\n0;1;%s;%s;%s;%s;;;;;%s\n4;\n3;\n' \
"$FINAL_X" "$FINAL_Y" "$FINAL_W" "$FINAL_H" \
"$FINAL_X" "$FINAL_Y" "$FINAL_W" "$FINAL_H" "$filename" | w3mimgdisplay
fi
}
}
# preview file and exit
preview() {
case "$2" in
"")
if test "$cmdname" != "unpreview"
then
echo '----- File Type Classification -----'
file -b "$1"
fi
exit
;;
a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip|7z)
if test "$cmdname" != "unpreview"
then
bsdtar -tf "$1"
fi
exit
;;
pdf)
if test "$cmdname" != "unpreview"
then
pdftotext -layout -- "$1" -
fi
exit
;;
htm|html|xhtml)
if test "$cmdname" != "unpreview"
then
w3m -dump "$1"
fi
exit
;;
1|2|3|4|5|6|7|8|9)
if test "$cmdname" != "unpreview"
then
MANWIDTH="$W" man -Tutf8 -l "$1" | sed 's/.//g'
fi
exit
;;
text/*|*/xml|application/x-shellscript)
cat "$1"
exit
;;
xpm|xbm)
if test "$IMG" -eq 1 && cache="$(getcache "$1")" && [ -f "$cache" ] || convert "$1" "$cache"
then
img "$cache"
exit 1
elif test "$cmdname" != "unpreview"
then
cat "$1"
exit
fi
;;
image/svg+xml)
if test "$IMG" -eq 1 && cache="$(getcache "$1")" && [ -f "$cache" ] || rsvg-convert "$1" >"$cache"
then
img "$cache"
exit 1
elif test "$cmdname" != "unpreview"
then
cat "$1"
exit
fi
;;
image/*)
if test "$IMG" -eq 1
then
img "$1"
exit 1
elif test "$cmdname" != "unpreview"
then
exiftool "$1"
exit
fi
;;
video/*)
if test "$IMG" -eq 1 && cache="$(getcache "$1")" && [ -f "$cache" ] || ffmpegthumbnailer -i "$1" -o "$cache" -s 0
then
img "$cache"
exit 1
elif test "$cmdname" != "unpreview"
then
mediainfo "$1"
exit
fi
exit
;;
audio/*|application/octet-stream)
if test "$cmdname" != "unpreview"
then
mediainfo "$1"
fi
exit
;;
esac
}
cmdname="${0##*/}"
case "$#" in
1)
W=72
H=20
X=0
Y=0
IMG=0
;;
5)
W=$2
H=$3
X=$4
Y=$5
IMG=1
;;
*)
usage
;;
esac
test -p "$1" && exit 1
preview "$1" "$(getextension "$1")" # try first to preview by file extension
preview "$1" "$(getfilemime "$1")" # then try to preview by mimetype
preview "$1" # then just print its file type