summaryrefslogtreecommitdiffstats
path: root/bin/nuke
diff options
context:
space:
mode:
authorYigit Sever2020-09-16 15:22:27 +0300
committerYigit Sever2020-09-16 15:22:27 +0300
commit7419b9fc45ba291d5d10a9f5bea3fa5d33dfedc0 (patch)
treedbc1e308cfbb398f77c784b7ea52c08e5aeb8332 /bin/nuke
parent44ff427c77e4c3848d273b0bc3484435ec9528c7 (diff)
downloaddotfiles-7419b9fc45ba291d5d10a9f5bea3fa5d33dfedc0.tar.gz
dotfiles-7419b9fc45ba291d5d10a9f5bea3fa5d33dfedc0.tar.bz2
dotfiles-7419b9fc45ba291d5d10a9f5bea3fa5d33dfedc0.zip
move bin to .local/bin
Diffstat (limited to 'bin/nuke')
-rwxr-xr-xbin/nuke501
1 files changed, 0 insertions, 501 deletions
diff --git a/bin/nuke b/bin/nuke
deleted file mode 100755
index de4f5c2..0000000
--- a/bin/nuke
+++ /dev/null
@@ -1,501 +0,0 @@
1#!/usr/bin/env sh
2
3# #############################################################################
4# Description: Sample script to play files in apps by file type or mime
5#
6# Shell: POSIX compliant
7# Usage: nuke filepath
8#
9# Integration with nnn:
10# 1. Export the required config:
11# export NNN_OPENER=/absolute/path/to/nuke
12# # Otherwise, if nuke is in $PATH
13# # export NNN_OPENER=nuke
14# 2. Run nnn with the program option to indicate a CLI opener
15# nnn -c
16# # The -c program option overrides option -e
17# 3. nuke can use nnn plugins (e.g. mocplay is used for audio), $PATH is updated.
18#
19# Details:
20# Inspired by ranger's scope.sh, modified for usage with nnn.
21#
22# Guards against accidentally opening mime types like executables, shared libs etc.
23#
24# Tries to play 'file' (1st argument) in the following order:
25# i. by extension
26# ii. by mime (image, video, audio, pdf)
27# iii. by mime (other file types)
28#
29# Modification tips:
30# 1. Invokes CLI utilities by default. Set GUI to 1 to enable GUI apps.
31# 2. PAGER is "less -R".
32# 3. Start GUI apps in bg to unblock. Redirect stdout and strerr if required.
33# 4. Some CLI utilities are piped to the $PAGER, to wait and quit uniformly.
34# 5. If the output cannot be paged use "read -r _" to wait for user input.
35# 6. On a DE, try 'xdg-open' in handle_fallback() as last resort.
36#
37# Feel free to change the utilities to your favourites and add more mimes.
38#
39# Defaults:
40# By extension (only the enabled ones):
41# most archives: list with atool, bsdtar
42# rar: list with unrar
43# 7-zip: list with 7z
44# pdf: zathura (GUI), pdftotext, mutool, exiftool
45# audio: mocplay (nnn plugin using MOC), mpv, mediainfo, exiftool
46# avi|mkv|mp4: smplayer, mpv (GUI), ffmpegthumbnailer, mediainfo, exiftool
47# log: vi
48# torrent: rtorrent, transmission-show
49# odt|ods|odp|sxw: odt2txt
50# md: glow (https://github.com/charmbracelet/glow), lowdown (https://kristaps.bsd.lv/lowdown)
51# htm|html|xhtml: w3m, lynx, elinks
52# json: jq, python (json.tool module)
53# Multimedia by mime:
54# image/*: imv/sxiv (GUI), viu (https://github.com/atanunq/viu), img2txt, exiftool
55# video/*: smplayer, mpv (GUI), ffmpegthumbnailer, mediainfo, exiftool
56# audio/*: mocplay (nnn plugin using MOC), mpv, mediainfo, exiftool
57# application/pdf: zathura (GUI), pdftotext, mutool, exiftool
58# Other mimes:
59# text/troff: man -l
60# text/* | */xml: vi
61# image/vnd.djvu): djvutxt, exiftool
62#
63# ToDo:
64# 1. Adapt, test and enable all mimes
65# 2. Clean-up the unnecessary exit codes
66# #############################################################################
67
68# set to 1 to enable GUI apps
69GUI="1"
70
71set -euf -o noclobber -o noglob -o nounset
72IFS="$(printf '%b_' '\n')"; IFS="${IFS%_}" # protect trailing \n
73
74PATH=$PATH:"${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins"
75IMAGE_CACHE_PATH="$(dirname "$1")"/.thumbs
76
77FPATH="$1"
78FNAME=$(basename "$1")
79EDITOR="${EDITOR:-vi}"
80PAGER="${PAGER:-less -R}"
81ext="${FNAME##*.}"
82if ! [ -z "$ext" ]; then
83 ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
84fi
85
86handle_pdf() {
87 if [ "$GUI" -ne 0 ] && which zathura >/dev/null 2>&1; then
88 zathura "${FPATH}" >/dev/null 2>&1 &
89 exit 0
90 elif which pdftotext >/dev/null 2>&1; then
91 ## Preview as text conversion
92 pdftotext -l 10 -nopgbrk -q -- "${FPATH}" - | eval "$PAGER"
93 exit 0
94 elif which mutool >/dev/null 2>&1; then
95 mutool draw -F txt -i -- "${FPATH}" 1-10 | eval "$PAGER"
96 exit 0
97 elif which exiftool >/dev/null 2>&1; then
98 exiftool "${FPATH}" | eval "$PAGER"
99 exit 0
100 fi
101}
102
103handle_audio() {
104 if which mocp >/dev/null 2>&1 && which mocplay >/dev/null 2>&1; then
105 mocplay "${FPATH}" "opener" >/dev/null 2>&1
106 exit 0
107 elif which mpv >/dev/null 2>&1; then
108 mpv "${FPATH}" >/dev/null 2>&1 &
109 exit 0
110 elif which mediainfo >/dev/null 2>&1; then
111 mediainfo "${FPATH}" | eval "$PAGER"
112 exit 0
113 elif which exiftool >/dev/null 2>&1; then
114 exiftool "${FPATH}"| eval "$PAGER"
115 exit 0
116 fi
117}
118
119handle_video() {
120 if [ "$GUI" -ne 0 ] && which smplayer >/dev/null 2>&1; then
121 smplayer "${FPATH}" >/dev/null 2>&1 &
122 exit 0
123 elif [ "$GUI" -ne 0 ] && which mpv >/dev/null 2>&1; then
124 mpv "${FPATH}" >/dev/null 2>&1 &
125 exit 0
126 elif which ffmpegthumbnailer >/dev/null 2>&1; then
127 # Thumbnail
128 [ -d "${IMAGE_CACHE_PATH}" ] || mkdir "${IMAGE_CACHE_PATH}"
129 ffmpegthumbnailer -i "${FPATH}" -o "${IMAGE_CACHE_PATH}/${FNAME}.jpg" -s 0
130 viu -n "${IMAGE_CACHE_PATH}/${FNAME}.jpg" | eval "$PAGER"
131 exit 0
132 elif which mediainfo >/dev/null 2>&1; then
133 mediainfo "${FPATH}" | eval "$PAGER"
134 exit 0
135 elif which exiftool >/dev/null 2>&1; then
136 exiftool "${FPATH}"| eval "$PAGER"
137 exit 0
138 fi
139}
140
141# handle this extension and exit
142handle_extension() {
143 case "${ext}" in
144 ## Archive
145 a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
146 rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)
147 if which atool >/dev/null 2>&1; then
148 atool --list -- "${FPATH}" | eval "$PAGER"
149 exit 0
150 elif which bsdtar >/dev/null 2>&1; then
151 bsdtar --list --file "${FPATH}" | eval "$PAGER"
152 exit 0
153 fi
154 exit 1;;
155 rar)
156 if which unrar >/dev/null 2>&1; then
157 ## Avoid password prompt by providing empty password
158 unrar lt -p- -- "${FPATH}" | eval "$PAGER"
159 fi
160 exit 1;;
161 7z)
162 if which 7z >/dev/null 2>&1; then
163 ## Avoid password prompt by providing empty password
164 7z l -p -- "${FPATH}" | eval "$PAGER"
165 exit 0
166 fi
167 exit 1;;
168
169 ## PDF
170 pdf)
171 handle_pdf
172 exit 1;;
173
174 ## Audio
175 aac|flac|m4a|mid|midi|mpa|mp2|mp3|ogg|wav|wma)
176 handle_audio
177 exit 1;;
178
179 ## Video
180 avi|mkv|mp4)
181 handle_video
182 exit 1;;
183
184 ## Log files
185 log)
186 # "$EDITOR" "${FPATH}"
187 "$PAGER" "${FPATH}"
188 exit 0;;
189
190 ## BitTorrent
191 torrent)
192 if which rtorrent >/dev/null 2>&1; then
193 rtorrent "${FPATH}"
194 exit 0
195 elif which transmission-show >/dev/null 2>&1; then
196 transmission-show -- "${FPATH}"
197 exit 0
198 fi
199 exit 1;;
200
201 ## OpenDocument
202 odt|ods|odp|sxw|pptx)
203 if which odt2txt >/dev/null 2>&1; then
204 ## Preview as text conversion
205 odt2txt "${FPATH}" | eval "$PAGER"
206 exit 0
207 elif [ "$GUI" -ne 0 ] && which libreoffice >/dev/null 2>&1; then
208 libreoffice "${FPATH}" &
209 exit 0
210 fi
211 exit 1;;
212
213 ## Markdown
214 md)
215 if which glow >/dev/null 2>&1; then
216 glow -sdark "${FPATH}" | eval "$PAGER"
217 exit 0
218 elif which lowdown >/dev/null 2>&1; then
219 lowdown -Tterm "${FPATH}" | eval "$PAGER"
220 exit 0
221 fi
222 ;;
223
224 ## HTML
225 htm|html|xhtml)
226 ## Preview as text conversion
227 if which w3m >/dev/null 2>&1; then
228 w3m -dump "${FPATH}" | eval "$PAGER"
229 exit 0
230 elif which lynx >/dev/null 2>&1; then
231 lynx -dump -- "${FPATH}" | eval "$PAGER"
232 exit 0
233 elif which elinks >/dev/null 2>&1; then
234 elinks -dump "${FPATH}" | eval "$PAGER"
235 exit 0
236 fi
237 ;;
238
239 ## JSON
240 json)
241 if which jq >/dev/null 2>&1; then
242 jq --color-output . "${FPATH}" | eval "$PAGER"
243 exit 0
244 elif which python >/dev/null 2>&1; then
245 python -m json.tool -- "${FPATH}" | eval "$PAGER"
246 exit 0
247 fi
248 ;;
249 esac
250}
251
252abspath() {
253 case "$1" in
254 /*) printf "%s\n" "$1";;
255 *) printf "%s\n" "$PWD/$1";;
256 esac
257}
258
259listimages() {
260 find -L "$(dirname "$target")" -maxdepth 1 -type f -iregex \
261 '.*\(jpe?g\|bmp\|png\|gif\)$' -print0 | sort -z
262}
263
264load_dir() {
265 target="$(abspath "$2")"
266 count="$(listimages | grep -a -m 1 -ZznF "$target" | cut -d: -f1)"
267
268 if [ -n "$count" ]; then
269 listimages | xargs -0 "$1" -n "$count" --
270 else
271 shift
272 "$1" -- "$@" # fallback
273 fi
274}
275
276handle_multimedia() {
277 ## Size of the preview if there are multiple options or it has to be
278 ## rendered from vector graphics. If the conversion program allows
279 ## specifying only one dimension while keeping the aspect ratio, the width
280 ## will be used.
281 # local DEFAULT_SIZE="1920x1080"
282
283 mimetype="${1}"
284 case "${mimetype}" in
285 ## SVG
286 # image/svg+xml|image/svg)
287 # convert -- "${FPATH}" "${IMAGE_CACHE_PATH}" && exit 6
288 # exit 1;;
289
290 ## DjVu
291 # image/vnd.djvu)
292 # ddjvu -format=tiff -quality=90 -page=1 -size="${DEFAULT_SIZE}" \
293 # - "${IMAGE_CACHE_PATH}" < "${FPATH}" \
294 # && exit 6 || exit 1;;
295
296 ## Image
297 image/*)
298 if [ "$GUI" -ne 0 ] && which imvr >/dev/null 2>&1; then
299 load_dir imvr "${FPATH}" >/dev/null 2>&1 &
300 exit 0
301 elif [ "$GUI" -ne 0 ] && which sxiv >/dev/null 2>&1; then
302 load_dir sxiv "${FPATH}" >/dev/null 2>&1 &
303 exit 0
304 elif which viu >/dev/null 2>&1; then
305 viu -n "${FPATH}" | eval "$PAGER"
306 exit 0
307 elif which img2txt >/dev/null 2>&1; then
308 img2txt --gamma=0.6 -- "${FPATH}" | eval "$PAGER"
309 exit 0
310 elif which exiftool >/dev/null 2>&1; then
311 exiftool "${FPATH}" | eval "$PAGER"
312 exit 0
313 fi
314 # local orientation
315 # orientation="$( identify -format '%[EXIF:Orientation]\n' -- "${FPATH}" )"
316 ## If orientation data is present and the image actually
317 ## needs rotating ("1" means no rotation)...
318 # if [[ -n "$orientation" && "$orientation" != 1 ]]; then
319 ## ...auto-rotate the image according to the EXIF data.
320 # convert -- "${FPATH}" -auto-orient "${IMAGE_CACHE_PATH}" && exit 6
321 # fi
322
323 ## `w3mimgdisplay` will be called for all images (unless overridden
324 ## as above), but might fail for unsupported types.
325 exit 7;;
326
327 ## PDF
328 application/pdf)
329 handle_pdf
330 exit 1;;
331
332 ## Audio
333 audio/*)
334 handle_audio
335 exit 1;;
336
337 ## Video
338 video/*)
339 handle_video
340 exit 1;;
341
342 # pdftoppm -f 1 -l 1 \
343 # -scale-to-x "${DEFAULT_SIZE%x*}" \
344 # -scale-to-y -1 \
345 # -singlefile \
346 # -jpeg -tiffcompression jpeg \
347 # -- "${FPATH}" "${IMAGE_CACHE_PATH%.*}" \
348 # && exit 6 || exit 1;;
349
350
351 ## ePub, MOBI, FB2 (using Calibre)
352 # application/epub+zip|application/x-mobipocket-ebook|\
353 # application/x-fictionbook+xml)
354 # # ePub (using https://github.com/marianosimone/epub-thumbnailer)
355 # epub-thumbnailer "${FPATH}" "${IMAGE_CACHE_PATH}" \
356 # "${DEFAULT_SIZE%x*}" && exit 6
357 # ebook-meta --get-cover="${IMAGE_CACHE_PATH}" -- "${FPATH}" \
358 # >/dev/null && exit 6
359 # exit 1;;
360
361 ## Font
362 # application/font*|application/*opentype)
363 # preview_png="/tmp/$(basename "${IMAGE_CACHE_PATH%.*}").png"
364 # if fontimage -o "${preview_png}" \
365 # --pixelsize "120" \
366 # --fontname \
367 # --pixelsize "80" \
368 # --text " ABCDEFGHIJKLMNOPQRSTUVWXYZ " \
369 # --text " abcdefghijklmnopqrstuvwxyz " \
370 # --text " 0123456789.:,;(*!?') ff fl fi ffi ffl " \
371 # --text " The quick brown fox jumps over the lazy dog. " \
372 # "${FPATH}";
373 # then
374 # convert -- "${preview_png}" "${IMAGE_CACHE_PATH}" \
375 # && rm "${preview_png}" \
376 # && exit 6
377 # else
378 # exit 1
379 # fi
380 # ;;
381
382 ## Preview archives using the first image inside.
383 ## (Very useful for comic book collections for example.)
384 # application/zip|application/x-rar|application/x-7z-compressed|\
385 # application/x-xz|application/x-bzip2|application/x-gzip|application/x-tar)
386 # local fn=""; local fe=""
387 # local zip=""; local rar=""; local tar=""; local bsd=""
388 # case "${mimetype}" in
389 # application/zip) zip=1 ;;
390 # application/x-rar) rar=1 ;;
391 # application/x-7z-compressed) ;;
392 # *) tar=1 ;;
393 # esac
394 # { [ "$tar" ] && fn=$(tar --list --file "${FPATH}"); } || \
395 # { fn=$(bsdtar --list --file "${FPATH}") && bsd=1 && tar=""; } || \
396 # { [ "$rar" ] && fn=$(unrar lb -p- -- "${FPATH}"); } || \
397 # { [ "$zip" ] && fn=$(zipinfo -1 -- "${FPATH}"); } || return
398 #
399 # fn=$(echo "$fn" | python -c "import sys; import mimetypes as m; \
400 # [ print(l, end='') for l in sys.stdin if \
401 # (m.guess_type(l[:-1])[0] or '').startswith('image/') ]" |\
402 # sort -V | head -n 1)
403 # [ "$fn" = "" ] && return
404 # [ "$bsd" ] && fn=$(printf '%b' "$fn")
405 #
406 # [ "$tar" ] && tar --extract --to-stdout \
407 # --file "${FPATH}" -- "$fn" > "${IMAGE_CACHE_PATH}" && exit 6
408 # fe=$(echo -n "$fn" | sed 's/[][*?\]/\\\0/g')
409 # [ "$bsd" ] && bsdtar --extract --to-stdout \
410 # --file "${FPATH}" -- "$fe" > "${IMAGE_CACHE_PATH}" && exit 6
411 # [ "$bsd" ] || [ "$tar" ] && rm -- "${IMAGE_CACHE_PATH}"
412 # [ "$rar" ] && unrar p -p- -inul -- "${FPATH}" "$fn" > \
413 # "${IMAGE_CACHE_PATH}" && exit 6
414 # [ "$zip" ] && unzip -pP "" -- "${FPATH}" "$fe" > \
415 # "${IMAGE_CACHE_PATH}" && exit 6
416 # [ "$rar" ] || [ "$zip" ] && rm -- "${IMAGE_CACHE_PATH}"
417 # ;;
418 esac
419}
420
421handle_mime() {
422 mimetype="${1}"
423 case "${mimetype}" in
424 ## Manpages
425 text/troff)
426 man -l "${FPATH}"
427 exit 0;;
428
429 ## Text
430 text/* | */xml)
431 "$EDITOR" "${FPATH}"
432 exit 0;;
433 ## Syntax highlight
434 # if [[ "$( stat --printf='%s' -- "${FPATH}" )" -gt "${HIGHLIGHT_SIZE_MAX}" ]]; then
435 # exit 2
436 # fi
437 # if [[ "$( tput colors )" -ge 256 ]]; then
438 # local pygmentize_format='terminal256'
439 # local highlight_format='xterm256'
440 # else
441 # local pygmentize_format='terminal'
442 # local highlight_format='ansi'
443 # fi
444 # env HIGHLIGHT_OPTIONS="${HIGHLIGHT_OPTIONS}" highlight \
445 # --out-format="${highlight_format}" \
446 # --force -- "${FPATH}" && exit 5
447 # pygmentize -f "${pygmentize_format}" -O "style=${PYGMENTIZE_STYLE}"\
448 # -- "${FPATH}" && exit 5
449 # exit 2;;
450
451 ## DjVu
452 image/vnd.djvu)
453 if which djvutxt >/dev/null 2>&1; then
454 ## Preview as text conversion (requires djvulibre)
455 djvutxt "${FPATH}" | eval "$PAGER"
456 exit 0
457 elif which exiftool >/dev/null 2>&1; then
458 exiftool "${FPATH}" | eval "$PAGER"
459 exit 0
460 fi
461 exit 1;;
462 esac
463}
464
465handle_fallback() {
466 if [ "$GUI" -ne 0 ]; then
467 xdg-open "${FPATH}" >/dev/null 2>&1 &
468 exit 0
469 fi
470
471 echo '----- File details -----' && file --dereference --brief -- "${FPATH}"
472 exit 1
473}
474
475handle_blocked() {
476 case "${MIMETYPE}" in
477 application/x-sharedlib)
478 exit 0;;
479
480 application/x-shared-library-la)
481 exit 0;;
482
483 application/x-executable)
484 exit 0;;
485
486 application/x-shellscript)
487 exit 0;;
488
489 application/octet-stream)
490 exit 0;;
491 esac
492}
493
494MIMETYPE="$( file --dereference --brief --mime-type -- "${FPATH}" )"
495handle_extension
496handle_multimedia "${MIMETYPE}"
497handle_mime "${MIMETYPE}"
498handle_blocked "${MIMETYPE}"
499handle_fallback
500
501exit 1