My build of nnn with minor changes
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

498 řádky
16 KiB

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