-
Notifications
You must be signed in to change notification settings - Fork 0
/
pv
executable file
·149 lines (141 loc) · 3.54 KB
/
pv
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
#!/bin/sh
set -euf
exec 2>/dev/null
draw() {
jq -nc --argjson x "$x" --argjson y "$y" \
--argjson width "$width" --argjson height "$height" \
--arg path "$(readlink -f -- "$1")" '
{
"action": "add",
"identifier": "preview",
"x": $x,
"y": $y,
"width": $width,
"height": $height,
"scaler": "contain",
"scaling_position_x": 0.5,
"scaling_position_y": 0.5,
"path": $path
}' >"$FIFO_UEBERZUG"
exit 1
}
hash() {
hash="$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f -- "$1")" | sha256sum | cut -d' ' -f1)"
dir="$(printf '%s' "$hash" | cut -c1-2)"
name="$(printf '%s' "$hash" | cut -c3-)"
cache="$HOME/.cache/lf/$dir/$name"
}
cache() {
cache="$cache.$1"
if ! [ -f "$cache" ]; then
dir="$(dirname -- "$cache")"
[ -d "$dir" ] || mkdir -p -- "$dir"
shift
"$@"
fi
draw "$cache"
}
file="$1"
width="$2"
height="$3"
x="$4"
y="$5"
[ -f "$file" ] || [ -h "$file" ]
default_x="1920"
default_y="1080"
mime="$(file -Lb --mime-type -- "$file")"
case "$mime" in
application/gzip|\
application/x-bzip2|\
application/x-xz|\
application/x-lzma|\
application/x-lz4|\
application/zstd|\
application/x-snappy-framed|\
application/x-tar|\
application/x-gtar|\
application/x-ustar|\
application/zip|\
application/x-7z-compressed|\
application/x-rar)
exec ouch list -t "$file";;
application/pdf)
if [ -n "${FIFO_UEBERZUG-}" ]; then
hash "$file"
cache jpg \
pdftoppm -f 1 -l 1 \
-scale-to-x "$default_x" \
-scale-to-y -1 \
-singlefile \
-jpeg \
-- "$file" "$cache"
else
exec pdftotext -nopgbrk -q -- "$file" -
fi
;;
image/vnd.djvu)
if [ -n "${FIFO_UEBERZUG-}" ]; then
hash "$file"
cache tiff \
ddjvu -format=tiff -quality=90 -page=1 \
-size="${default_x}x${default_y}" "$file" "$cache.tiff"
else
exec djvutxt - <"$file"
fi
;;
application/vnd.openxmlformats-officedocument.wordprocessingml.document|\
application/vnd.oasis.opendocument.text|\
application/epub+zip|\
text/rtf)
exec pandoc -s -t plain -- "$file";;
text/troff)
COLUMNS="$width" man -- "$file" | col -b; exit;;
text/html)
exec lynx -width="$width" -dump -- "$file";;
application/json)
exec jq -C <"$file";;
text/*|application/javascript)
exec bat --color=always -- "$file";;
image/svg+xml)
if [ -n "${FIFO_UEBERZUG-}" ]; then
hash "$file"
cache jpg magick -- "$file" "$cache.jpg"
fi
;;
image/*)
if [ -n "${FIFO_UEBERZUG-}" ]; then
# ueberzug doesn't handle image orientation correctly
orientation="$(magick identify -format '%[orientation]\n' -- "$file[0]")"
if [ -n "$orientation" ] \
&& [ "$orientation" != Undefined ] \
&& [ "$orientation" != TopLeft ]; then
hash "$file"
cache jpg magick -- "$file[0]" -auto-orient "$cache.jpg"
else
draw "$file"
fi
fi
;;
video/*)
if [ -n "${FIFO_UEBERZUG-}" ]; then
hash "$file"
cache jpg ffmpegthumbnailer -i "$file" -o "$cache.jpg" -s 0
fi
;;
esac
header="File Type Classification"
# lf incorrectly reports the width + 2
: "$((width -= 2))"
padding="$((width - ${#header}))"
len="$((padding / 2 - 2))"
if [ "$len" -gt 0 ]; then
header=" $header "
for i in $(seq "$len"); do
header="─$header─"
done
if [ "$((padding % 2))" -eq 1 ]; then
header="$header─"
fi
fi
printf '\033[7m %s \033[0m\n' "$header"
file -Lb -- "$file" | fold -s -w "$width"