My build of nnn with minor changes
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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