-
Notifications
You must be signed in to change notification settings - Fork 0
/
prev
executable file
·176 lines (170 loc) · 4.53 KB
/
prev
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
#!/bin/sh
# LINES=$(tput lines)
LINES=100
# COLUMNS=$(tput cols)
COLUMNS=100
# kitty +kitten icat --transfer-mode file --clear
optimize() {
head -$LINES </dev/stdin | colrm $COLUMNS | nvimpager -c +"setf $1"
}
# Format input based on file type
parse() {
case $1 in
csv) column -s, -o, -t "$2" | optimize csv ;;
pdf) pdftotext "$2" - | optimize log ;;
esac
}
# Wrap around libreoffice's --convert-to option and clean up when done.
libreoffice_wrapper() {
tmp=$(mktemp -p /tmp -d prev.XXX)
libreoffice --headless --convert-to "$1" "$2" --outdir "$tmp"
parse "$1" "$tmp/"*
rm -r "$tmp"
}
case "$(file -b --mime-type "$1")" in
audio/*)
mediainfo "$1" | optimize make
;;
video/*)
tmp=$(mktemp -p /tmp prevffmpegthumbnailer.XXX.png)
ffmpegthumbnailer -i "$1" -o "$tmp"
# TODO: Add support for more image previewers (kitty)
chafa -c 256 "$tmp"
mediainfo "$1" | optimize make
rm "$tmp"
;;
image/vnd.djvu)
djvutxt "$1" | optimize log
;;
application/pdf)
parse pdf "$1"
;;
image/*)
chafa -c 256 "$1"
# kitty +kitten icat --transfer-mode file --place 80x40@88x1 --silent "$1"
# ueberview "$1"
mediainfo "$1" | optimize make
;;
application/zip)
zipinfo "$1" | optimize lsl
;;
application/x-7z-compressed | application/x-rar)
7z l -pa "$1" | optimize lsl
;;
application/zstd)
# zstdcat "$1" | head -n30
tar -tf "$1" | optimize lsl
;;
inode/symlink)
# Shell recursion >:^)
$0 "$(readlink -f "$1")"
;;
inode/chardevice)
file -b "$1"
;;
application/x-iso9660-image)
iso-info --no-header --show-rock-ridge 0 -l "$1" | optimize log
echo "__________________________________"
hexdump -C "$1" | optimize log | head -10
echo "__________________________________"
# shellcheck disable=SC2046,2005
echo $(strings "$1") | fold -w 80
;;
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)
libreoffice_wrapper csv "$1"
;;
application/vnd.openxmlformats-officedocument.presentationml.presentation)
libreoffice_wrapper pdf "$1"
;;
# odt
application/vnd.oasis.* | application/vnd.openxmlformats* | application/msword)
# pandoc "$1" -t markdown | $HIGHLIGHT_CMD --syntax markdown
libreoffice_wrapper txt "$1"
;;
*/csv)
parse csv "$1"
;;
application/json)
if jq_output="$(jq <"$1" 2>/dev/null)"; then
optimize json <"$jq_output"
else
nvimpager -c "$1"
fi
;;
application/*sqlite3)
sqlite3 "$1" .dump | optimize sql
;;
application/x-xz)
case $1 in
*.tar.xz*) tar -tvf "$1" ;;
*) xzcat "$1" ;;
esac | optimize lsl
;;
application/gzip)
case $1 in
*.tar.gz* | .tgz*) tar -ztvf "$1" | optimize lsl ;;
# gzip -l "$1" | $HIGHLIGHT_CMD --syntax ls
*) gzip --uncompress -c "$1" | optimize log ;;
esac
;;
application/x-bzip2)
# NOTE: Get more verbose output with -v flag
tar -jtvf "$1" | optimize lsl
;;
# TODO: Experiment with disassembling binaries
application/octet-stream | text/x-bytecode* | application/x-object | \
application/x-pie-* | application/x-dos* | application/x-sharedlib | \
application/x-msi | application/x-matlab-data | application/x-executable | \
application/x-hdf5 | application/x-chrome-extension | application/x-dmp)
case "$(file -b "$1")" in
"Apple binary property list")
plistutil -i "$1" | optimize xml
;;
*)
file -b "$1"
tput setaf 13
echo "------------------------------------------------------------+----------------+"
hexdump -C "$1" | optimize log | head -10
echo "------------------------------------------------------------+----------------+"
# shellcheck disable=SC2046,2005
echo $(strings "$1") | fold -w 80
;;
esac
;;
application/vnd.tcpdump.pcap)
tshark -r "$1" | optimize log
;;
application/x-tar)
tar -tvf "$1" | optimize lsl
;;
*)
case $1 in
*.res)
optimize json <"$1"
;;
*.vdf* | *.acf | *_legacy.bin) # Steam files
optimize yaml <"$1"
;;
*.mesh)
sed 's/]/]\n/g' <"$1" | optimize glsl
;;
*.rbxm)
optimize xml <"$1"
;;
*)
case "$(file -b "$1")" in
"ASCII text, with very long lines"*)
if jq_output="$(jq <"$1" 2>/dev/null)"; then
optimize json <"$jq_output"
else
nvimpager -c "$1"
fi
;;
*)
nvimpager -c "$1"
;;
esac
;;
esac
;;
esac