My build of nnn with minor changes
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

521 lignes
17 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' or '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, media_client (Haiku), 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, media_client (Haiku), 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="${VISUAL:-${EDITOR:-vi}}"
  75. PAGER="${PAGER:-less -R}"
  76. ext="${FNAME##*.}"
  77. if [ -n "$ext" ]; then
  78. ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
  79. fi
  80. is_mac() {
  81. uname | grep -q "Darwin"
  82. }
  83. handle_pdf() {
  84. if [ "$GUI" -ne 0 ] && is_mac; then
  85. nohup open "${FPATH}" >/dev/null 2>&1 &
  86. exit 0
  87. elif [ "$GUI" -ne 0 ] && which zathura >/dev/null 2>&1; then
  88. nohup 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. handle_audio() {
  103. if which mocp >/dev/null 2>&1 && which mocplay >/dev/null 2>&1; then
  104. mocplay "${FPATH}" "opener" >/dev/null 2>&1
  105. exit 0
  106. elif which mpv >/dev/null 2>&1; then
  107. mpv "${FPATH}" >/dev/null 2>&1 &
  108. exit 0
  109. elif which media_client >/dev/null 2>&1; then
  110. media_client play "${FPATH}" >/dev/null 2>&1 &
  111. exit 0
  112. elif which mediainfo >/dev/null 2>&1; then
  113. mediainfo "${FPATH}" | eval "$PAGER"
  114. exit 0
  115. elif which exiftool >/dev/null 2>&1; then
  116. exiftool "${FPATH}"| eval "$PAGER"
  117. exit 0
  118. fi
  119. }
  120. handle_video() {
  121. if [ "$GUI" -ne 0 ] && is_mac; then
  122. nohup open "${FPATH}" >/dev/null 2>&1 &
  123. exit 0
  124. elif [ "$GUI" -ne 0 ] && which smplayer >/dev/null 2>&1; then
  125. nohup smplayer "${FPATH}" >/dev/null 2>&1 &
  126. exit 0
  127. elif [ "$GUI" -ne 0 ] && which mpv >/dev/null 2>&1; then
  128. nohup mpv "${FPATH}" >/dev/null 2>&1 &
  129. exit 0
  130. elif which ffmpegthumbnailer >/dev/null 2>&1; then
  131. # Thumbnail
  132. [ -d "${IMAGE_CACHE_PATH}" ] || mkdir "${IMAGE_CACHE_PATH}"
  133. ffmpegthumbnailer -i "${FPATH}" -o "${IMAGE_CACHE_PATH}/${FNAME}.jpg" -s 0
  134. viu -n "${IMAGE_CACHE_PATH}/${FNAME}.jpg" | eval "$PAGER"
  135. exit 0
  136. elif which mediainfo >/dev/null 2>&1; then
  137. mediainfo "${FPATH}" | eval "$PAGER"
  138. exit 0
  139. elif which exiftool >/dev/null 2>&1; then
  140. exiftool "${FPATH}"| eval "$PAGER"
  141. exit 0
  142. fi
  143. }
  144. # handle this extension and exit
  145. handle_extension() {
  146. case "${ext}" in
  147. ## Archive
  148. a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
  149. rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)
  150. if which atool >/dev/null 2>&1; then
  151. atool --list -- "${FPATH}" | eval "$PAGER"
  152. exit 0
  153. elif which bsdtar >/dev/null 2>&1; then
  154. bsdtar --list --file "${FPATH}" | eval "$PAGER"
  155. exit 0
  156. fi
  157. exit 1;;
  158. rar)
  159. if which unrar >/dev/null 2>&1; then
  160. ## Avoid password prompt by providing empty password
  161. unrar lt -p- -- "${FPATH}" | eval "$PAGER"
  162. fi
  163. exit 1;;
  164. 7z)
  165. if which 7z >/dev/null 2>&1; then
  166. ## Avoid password prompt by providing empty password
  167. 7z l -p -- "${FPATH}" | eval "$PAGER"
  168. exit 0
  169. fi
  170. exit 1;;
  171. ## PDF
  172. pdf)
  173. handle_pdf
  174. exit 1;;
  175. ## Audio
  176. aac|flac|m4a|mid|midi|mpa|mp2|mp3|ogg|wav|wma)
  177. handle_audio
  178. exit 1;;
  179. ## Video
  180. avi|mkv|mp4)
  181. handle_video
  182. exit 1;;
  183. ## Log files
  184. log)
  185. "$EDITOR" "${FPATH}"
  186. exit 0;;
  187. ## BitTorrent
  188. torrent)
  189. if which rtorrent >/dev/null 2>&1; then
  190. rtorrent "${FPATH}"
  191. exit 0
  192. elif which transmission-show >/dev/null 2>&1; then
  193. transmission-show -- "${FPATH}"
  194. exit 0
  195. fi
  196. exit 1;;
  197. ## OpenDocument
  198. odt|ods|odp|sxw)
  199. if which odt2txt >/dev/null 2>&1; then
  200. ## Preview as text conversion
  201. odt2txt "${FPATH}" | eval "$PAGER"
  202. exit 0
  203. fi
  204. exit 1;;
  205. ## Markdown
  206. md)
  207. if which glow >/dev/null 2>&1; then
  208. glow -sdark "${FPATH}" | eval "$PAGER"
  209. exit 0
  210. elif which lowdown >/dev/null 2>&1; then
  211. lowdown -Tterm "${FPATH}" | eval "$PAGER"
  212. exit 0
  213. fi
  214. ;;
  215. ## HTML
  216. htm|html|xhtml)
  217. ## Preview as text conversion
  218. if which w3m >/dev/null 2>&1; then
  219. w3m -dump "${FPATH}" | eval "$PAGER"
  220. exit 0
  221. elif which lynx >/dev/null 2>&1; then
  222. lynx -dump -- "${FPATH}" | eval "$PAGER"
  223. exit 0
  224. elif which elinks >/dev/null 2>&1; then
  225. elinks -dump "${FPATH}" | eval "$PAGER"
  226. exit 0
  227. fi
  228. ;;
  229. ## JSON
  230. json)
  231. if which jq >/dev/null 2>&1; then
  232. jq --color-output . "${FPATH}" | eval "$PAGER"
  233. exit 0
  234. elif which python >/dev/null 2>&1; then
  235. python -m json.tool -- "${FPATH}" | eval "$PAGER"
  236. exit 0
  237. fi
  238. ;;
  239. esac
  240. }
  241. abspath() {
  242. case "$1" in
  243. /*) printf "%s\n" "$1";;
  244. *) printf "%s\n" "$PWD/$1";;
  245. esac
  246. }
  247. listimages() {
  248. find -L "$(dirname "$target")" -maxdepth 1 -type f -iregex \
  249. '.*\(jpe?g\|bmp\|webp\|png\|gif\)$' -print0 | sort -z
  250. }
  251. load_dir() {
  252. target="$(abspath "$2")"
  253. count="$(listimages | grep -a -m 1 -ZznF "$target" | cut -d: -f1)"
  254. if [ -n "$count" ]; then
  255. if [ "$GUI" -ne 0 ]; then
  256. listimages | xargs -0 nohup "$1" -n "$count" --
  257. else
  258. listimages | xargs -0 "$1" -n "$count" --
  259. fi
  260. else
  261. shift
  262. "$1" -- "$@" # fallback
  263. fi
  264. }
  265. handle_multimedia() {
  266. ## Size of the preview if there are multiple options or it has to be
  267. ## rendered from vector graphics. If the conversion program allows
  268. ## specifying only one dimension while keeping the aspect ratio, the width
  269. ## will be used.
  270. # local DEFAULT_SIZE="1920x1080"
  271. mimetype="${1}"
  272. case "${mimetype}" in
  273. ## SVG
  274. # image/svg+xml|image/svg)
  275. # convert -- "${FPATH}" "${IMAGE_CACHE_PATH}" && exit 6
  276. # exit 1;;
  277. ## DjVu
  278. # image/vnd.djvu)
  279. # ddjvu -format=tiff -quality=90 -page=1 -size="${DEFAULT_SIZE}" \
  280. # - "${IMAGE_CACHE_PATH}" < "${FPATH}" \
  281. # && exit 6 || exit 1;;
  282. ## Image
  283. image/*)
  284. if [ "$GUI" -ne 0 ] && is_mac; then
  285. nohup open "${FPATH}" >/dev/null 2>&1 &
  286. exit 0
  287. elif [ "$GUI" -ne 0 ] && which imvr >/dev/null 2>&1; then
  288. load_dir imvr "${FPATH}" >/dev/null 2>&1 &
  289. exit 0
  290. elif [ "$GUI" -ne 0 ] && which sxiv >/dev/null 2>&1; then
  291. load_dir sxiv "${FPATH}" >/dev/null 2>&1 &
  292. exit 0
  293. elif which viu >/dev/null 2>&1; then
  294. viu -n "${FPATH}" | eval "$PAGER"
  295. exit 0
  296. elif which img2txt >/dev/null 2>&1; then
  297. img2txt --gamma=0.6 -- "${FPATH}" | eval "$PAGER"
  298. exit 0
  299. elif which exiftool >/dev/null 2>&1; then
  300. exiftool "${FPATH}" | eval "$PAGER"
  301. exit 0
  302. fi
  303. # local orientation
  304. # orientation="$( identify -format '%[EXIF:Orientation]\n' -- "${FPATH}" )"
  305. ## If orientation data is present and the image actually
  306. ## needs rotating ("1" means no rotation)...
  307. # if [[ -n "$orientation" && "$orientation" != 1 ]]; then
  308. ## ...auto-rotate the image according to the EXIF data.
  309. # convert -- "${FPATH}" -auto-orient "${IMAGE_CACHE_PATH}" && exit 6
  310. # fi
  311. ## `w3mimgdisplay` will be called for all images (unless overridden
  312. ## as above), but might fail for unsupported types.
  313. exit 7;;
  314. ## PDF
  315. application/pdf)
  316. handle_pdf
  317. exit 1;;
  318. ## Audio
  319. audio/*)
  320. handle_audio
  321. exit 1;;
  322. ## Video
  323. video/*)
  324. handle_video
  325. exit 1;;
  326. # pdftoppm -f 1 -l 1 \
  327. # -scale-to-x "${DEFAULT_SIZE%x*}" \
  328. # -scale-to-y -1 \
  329. # -singlefile \
  330. # -jpeg -tiffcompression jpeg \
  331. # -- "${FPATH}" "${IMAGE_CACHE_PATH%.*}" \
  332. # && exit 6 || exit 1;;
  333. ## ePub, MOBI, FB2 (using Calibre)
  334. # application/epub+zip|application/x-mobipocket-ebook|\
  335. # application/x-fictionbook+xml)
  336. # # ePub (using https://github.com/marianosimone/epub-thumbnailer)
  337. # epub-thumbnailer "${FPATH}" "${IMAGE_CACHE_PATH}" \
  338. # "${DEFAULT_SIZE%x*}" && exit 6
  339. # ebook-meta --get-cover="${IMAGE_CACHE_PATH}" -- "${FPATH}" \
  340. # >/dev/null && exit 6
  341. # exit 1;;
  342. ## Font
  343. # application/font*|application/*opentype)
  344. # preview_png="/tmp/$(basename "${IMAGE_CACHE_PATH%.*}").png"
  345. # if fontimage -o "${preview_png}" \
  346. # --pixelsize "120" \
  347. # --fontname \
  348. # --pixelsize "80" \
  349. # --text " ABCDEFGHIJKLMNOPQRSTUVWXYZ " \
  350. # --text " abcdefghijklmnopqrstuvwxyz " \
  351. # --text " 0123456789.:,;(*!?') ff fl fi ffi ffl " \
  352. # --text " The quick brown fox jumps over the lazy dog. " \
  353. # "${FPATH}";
  354. # then
  355. # convert -- "${preview_png}" "${IMAGE_CACHE_PATH}" \
  356. # && rm "${preview_png}" \
  357. # && exit 6
  358. # else
  359. # exit 1
  360. # fi
  361. # ;;
  362. ## Preview archives using the first image inside.
  363. ## (Very useful for comic book collections for example.)
  364. # application/zip|application/x-rar|application/x-7z-compressed|\
  365. # application/x-xz|application/x-bzip2|application/x-gzip|application/x-tar)
  366. # local fn=""; local fe=""
  367. # local zip=""; local rar=""; local tar=""; local bsd=""
  368. # case "${mimetype}" in
  369. # application/zip) zip=1 ;;
  370. # application/x-rar) rar=1 ;;
  371. # application/x-7z-compressed) ;;
  372. # *) tar=1 ;;
  373. # esac
  374. # { [ "$tar" ] && fn=$(tar --list --file "${FPATH}"); } || \
  375. # { fn=$(bsdtar --list --file "${FPATH}") && bsd=1 && tar=""; } || \
  376. # { [ "$rar" ] && fn=$(unrar lb -p- -- "${FPATH}"); } || \
  377. # { [ "$zip" ] && fn=$(zipinfo -1 -- "${FPATH}"); } || return
  378. #
  379. # fn=$(echo "$fn" | python -c "import sys; import mimetypes as m; \
  380. # [ print(l, end='') for l in sys.stdin if \
  381. # (m.guess_type(l[:-1])[0] or '').startswith('image/') ]" |\
  382. # sort -V | head -n 1)
  383. # [ "$fn" = "" ] && return
  384. # [ "$bsd" ] && fn=$(printf '%b' "$fn")
  385. #
  386. # [ "$tar" ] && tar --extract --to-stdout \
  387. # --file "${FPATH}" -- "$fn" > "${IMAGE_CACHE_PATH}" && exit 6
  388. # fe=$(echo -n "$fn" | sed 's/[][*?\]/\\\0/g')
  389. # [ "$bsd" ] && bsdtar --extract --to-stdout \
  390. # --file "${FPATH}" -- "$fe" > "${IMAGE_CACHE_PATH}" && exit 6
  391. # [ "$bsd" ] || [ "$tar" ] && rm -- "${IMAGE_CACHE_PATH}"
  392. # [ "$rar" ] && unrar p -p- -inul -- "${FPATH}" "$fn" > \
  393. # "${IMAGE_CACHE_PATH}" && exit 6
  394. # [ "$zip" ] && unzip -pP "" -- "${FPATH}" "$fe" > \
  395. # "${IMAGE_CACHE_PATH}" && exit 6
  396. # [ "$rar" ] || [ "$zip" ] && rm -- "${IMAGE_CACHE_PATH}"
  397. # ;;
  398. esac
  399. }
  400. handle_mime() {
  401. mimetype="${1}"
  402. case "${mimetype}" in
  403. ## Manpages
  404. text/troff)
  405. man -l "${FPATH}"
  406. exit 0;;
  407. ## Text
  408. text/* | */xml)
  409. "$EDITOR" "${FPATH}"
  410. exit 0;;
  411. ## Syntax highlight
  412. # if [[ "$( stat --printf='%s' -- "${FPATH}" )" -gt "${HIGHLIGHT_SIZE_MAX}" ]]; then
  413. # exit 2
  414. # fi
  415. # if [[ "$( tput colors )" -ge 256 ]]; then
  416. # local pygmentize_format='terminal256'
  417. # local highlight_format='xterm256'
  418. # else
  419. # local pygmentize_format='terminal'
  420. # local highlight_format='ansi'
  421. # fi
  422. # env HIGHLIGHT_OPTIONS="${HIGHLIGHT_OPTIONS}" highlight \
  423. # --out-format="${highlight_format}" \
  424. # --force -- "${FPATH}" && exit 5
  425. # pygmentize -f "${pygmentize_format}" -O "style=${PYGMENTIZE_STYLE}"\
  426. # -- "${FPATH}" && exit 5
  427. # exit 2;;
  428. ## DjVu
  429. image/vnd.djvu)
  430. if which djvutxt >/dev/null 2>&1; then
  431. ## Preview as text conversion (requires djvulibre)
  432. djvutxt "${FPATH}" | eval "$PAGER"
  433. exit 0
  434. elif which exiftool >/dev/null 2>&1; then
  435. exiftool "${FPATH}" | eval "$PAGER"
  436. exit 0
  437. fi
  438. exit 1;;
  439. esac
  440. }
  441. handle_fallback() {
  442. if [ "$GUI" -ne 0 ] && which xdg-open >/dev/null 2>&1; then
  443. nohup xdg-open "${FPATH}" >/dev/null 2>&1 &
  444. exit 0
  445. elif [ "$GUI" -ne 0 ] && which open >/dev/null 2>&1; then
  446. nohup open "${FPATH}" >/dev/null 2>&1 &
  447. exit 0
  448. fi
  449. echo '----- File details -----' && file --dereference --brief -- "${FPATH}"
  450. exit 1
  451. }
  452. handle_blocked() {
  453. case "${MIMETYPE}" in
  454. application/x-sharedlib)
  455. exit 0;;
  456. application/x-shared-library-la)
  457. exit 0;;
  458. application/x-executable)
  459. exit 0;;
  460. application/x-shellscript)
  461. exit 0;;
  462. application/octet-stream)
  463. exit 0;;
  464. esac
  465. }
  466. MIMETYPE="$( file --dereference --brief --mime-type -- "${FPATH}" )"
  467. handle_extension
  468. handle_multimedia "${MIMETYPE}"
  469. handle_mime "${MIMETYPE}"
  470. handle_blocked "${MIMETYPE}"
  471. handle_fallback
  472. exit 1