My build of nnn with minor changes
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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