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.
 
 
 
 
 
 

463 lignes
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. # # 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=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. handle_multimedia() {
  225. ## Size of the preview if there are multiple options or it has to be
  226. ## rendered from vector graphics. If the conversion program allows
  227. ## specifying only one dimension while keeping the aspect ratio, the width
  228. ## will be used.
  229. # local DEFAULT_SIZE="1920x1080"
  230. mimetype="${1}"
  231. case "${mimetype}" in
  232. ## SVG
  233. # image/svg+xml|image/svg)
  234. # convert -- "${FPATH}" "${IMAGE_CACHE_PATH}" && exit 6
  235. # exit 1;;
  236. ## DjVu
  237. # image/vnd.djvu)
  238. # ddjvu -format=tiff -quality=90 -page=1 -size="${DEFAULT_SIZE}" \
  239. # - "${IMAGE_CACHE_PATH}" < "${FPATH}" \
  240. # && exit 6 || exit 1;;
  241. ## Image
  242. image/*)
  243. if [ $GUI -ne 0 ] && which sxiv >/dev/null 2>&1; then
  244. sxiv -q "${FPATH}" &
  245. exit 0
  246. elif which viu >/dev/null 2>&1; then
  247. viu -n "${FPATH}" | less -R
  248. exit 0
  249. elif which img2txt >/dev/null 2>&1; then
  250. img2txt --gamma=0.6 -- "${FPATH}" | less -R
  251. exit 0
  252. elif which exiftool >/dev/null 2>&1; then
  253. exiftool "${FPATH}" | less -R
  254. exit 0
  255. fi
  256. # local orientation
  257. # orientation="$( identify -format '%[EXIF:Orientation]\n' -- "${FPATH}" )"
  258. ## If orientation data is present and the image actually
  259. ## needs rotating ("1" means no rotation)...
  260. # if [[ -n "$orientation" && "$orientation" != 1 ]]; then
  261. ## ...auto-rotate the image according to the EXIF data.
  262. # convert -- "${FPATH}" -auto-orient "${IMAGE_CACHE_PATH}" && exit 6
  263. # fi
  264. ## `w3mimgdisplay` will be called for all images (unless overriden
  265. ## as above), but might fail for unsupported types.
  266. exit 7;;
  267. ## PDF
  268. application/pdf)
  269. handle_pdf
  270. exit 1;;
  271. ## Audio
  272. audio/*)
  273. handle_audio
  274. exit 1;;
  275. ## Video
  276. video/*)
  277. handle_video
  278. exit 1;;
  279. # pdftoppm -f 1 -l 1 \
  280. # -scale-to-x "${DEFAULT_SIZE%x*}" \
  281. # -scale-to-y -1 \
  282. # -singlefile \
  283. # -jpeg -tiffcompression jpeg \
  284. # -- "${FPATH}" "${IMAGE_CACHE_PATH%.*}" \
  285. # && exit 6 || exit 1;;
  286. ## ePub, MOBI, FB2 (using Calibre)
  287. # application/epub+zip|application/x-mobipocket-ebook|\
  288. # application/x-fictionbook+xml)
  289. # # ePub (using https://github.com/marianosimone/epub-thumbnailer)
  290. # epub-thumbnailer "${FPATH}" "${IMAGE_CACHE_PATH}" \
  291. # "${DEFAULT_SIZE%x*}" && exit 6
  292. # ebook-meta --get-cover="${IMAGE_CACHE_PATH}" -- "${FPATH}" \
  293. # >/dev/null && exit 6
  294. # exit 1;;
  295. ## Font
  296. # application/font*|application/*opentype)
  297. # preview_png="/tmp/$(basename "${IMAGE_CACHE_PATH%.*}").png"
  298. # if fontimage -o "${preview_png}" \
  299. # --pixelsize "120" \
  300. # --fontname \
  301. # --pixelsize "80" \
  302. # --text " ABCDEFGHIJKLMNOPQRSTUVWXYZ " \
  303. # --text " abcdefghijklmnopqrstuvwxyz " \
  304. # --text " 0123456789.:,;(*!?') ff fl fi ffi ffl " \
  305. # --text " The quick brown fox jumps over the lazy dog. " \
  306. # "${FPATH}";
  307. # then
  308. # convert -- "${preview_png}" "${IMAGE_CACHE_PATH}" \
  309. # && rm "${preview_png}" \
  310. # && exit 6
  311. # else
  312. # exit 1
  313. # fi
  314. # ;;
  315. ## Preview archives using the first image inside.
  316. ## (Very useful for comic book collections for example.)
  317. # application/zip|application/x-rar|application/x-7z-compressed|\
  318. # application/x-xz|application/x-bzip2|application/x-gzip|application/x-tar)
  319. # local fn=""; local fe=""
  320. # local zip=""; local rar=""; local tar=""; local bsd=""
  321. # case "${mimetype}" in
  322. # application/zip) zip=1 ;;
  323. # application/x-rar) rar=1 ;;
  324. # application/x-7z-compressed) ;;
  325. # *) tar=1 ;;
  326. # esac
  327. # { [ "$tar" ] && fn=$(tar --list --file "${FPATH}"); } || \
  328. # { fn=$(bsdtar --list --file "${FPATH}") && bsd=1 && tar=""; } || \
  329. # { [ "$rar" ] && fn=$(unrar lb -p- -- "${FPATH}"); } || \
  330. # { [ "$zip" ] && fn=$(zipinfo -1 -- "${FPATH}"); } || return
  331. #
  332. # fn=$(echo "$fn" | python -c "import sys; import mimetypes as m; \
  333. # [ print(l, end='') for l in sys.stdin if \
  334. # (m.guess_type(l[:-1])[0] or '').startswith('image/') ]" |\
  335. # sort -V | head -n 1)
  336. # [ "$fn" = "" ] && return
  337. # [ "$bsd" ] && fn=$(printf '%b' "$fn")
  338. #
  339. # [ "$tar" ] && tar --extract --to-stdout \
  340. # --file "${FPATH}" -- "$fn" > "${IMAGE_CACHE_PATH}" && exit 6
  341. # fe=$(echo -n "$fn" | sed 's/[][*?\]/\\\0/g')
  342. # [ "$bsd" ] && bsdtar --extract --to-stdout \
  343. # --file "${FPATH}" -- "$fe" > "${IMAGE_CACHE_PATH}" && exit 6
  344. # [ "$bsd" ] || [ "$tar" ] && rm -- "${IMAGE_CACHE_PATH}"
  345. # [ "$rar" ] && unrar p -p- -inul -- "${FPATH}" "$fn" > \
  346. # "${IMAGE_CACHE_PATH}" && exit 6
  347. # [ "$zip" ] && unzip -pP "" -- "${FPATH}" "$fe" > \
  348. # "${IMAGE_CACHE_PATH}" && exit 6
  349. # [ "$rar" ] || [ "$zip" ] && rm -- "${IMAGE_CACHE_PATH}"
  350. # ;;
  351. esac
  352. }
  353. handle_mime() {
  354. mimetype="${1}"
  355. case "${mimetype}" in
  356. ## Manpages
  357. text/troff)
  358. man -l "${FPATH}"
  359. exit 0;;
  360. ## Text
  361. text/* | */xml)
  362. vi "${FPATH}"
  363. exit 0;;
  364. ## Syntax highlight
  365. # if [[ "$( stat --printf='%s' -- "${FPATH}" )" -gt "${HIGHLIGHT_SIZE_MAX}" ]]; then
  366. # exit 2
  367. # fi
  368. # if [[ "$( tput colors )" -ge 256 ]]; then
  369. # local pygmentize_format='terminal256'
  370. # local highlight_format='xterm256'
  371. # else
  372. # local pygmentize_format='terminal'
  373. # local highlight_format='ansi'
  374. # fi
  375. # env HIGHLIGHT_OPTIONS="${HIGHLIGHT_OPTIONS}" highlight \
  376. # --out-format="${highlight_format}" \
  377. # --force -- "${FPATH}" && exit 5
  378. # pygmentize -f "${pygmentize_format}" -O "style=${PYGMENTIZE_STYLE}"\
  379. # -- "${FPATH}" && exit 5
  380. # exit 2;;
  381. ## DjVu
  382. image/vnd.djvu)
  383. if which djvutxt >/dev/null 2>&1; then
  384. ## Preview as text conversion (requires djvulibre)
  385. djvutxt "${FPATH}" | less -R
  386. exit 0
  387. elif which exiftool >/dev/null 2>&1; then
  388. exiftool "${FPATH}" | less -R
  389. exit 0
  390. fi
  391. exit 1;;
  392. esac
  393. }
  394. handle_fallback() {
  395. if [ $GUI -ne 0 ]; then
  396. xdg-open "${FPATH}" >/dev/null 2>&1 &
  397. exit 0
  398. fi
  399. echo '----- File details -----' && file --dereference --brief -- "${FPATH}"
  400. exit 1
  401. }
  402. handle_blocked() {
  403. case "${MIMETYPE}" in
  404. application/x-sharedlib)
  405. exit 0;;
  406. application/x-shared-library-la)
  407. exit 0;;
  408. application/x-executable)
  409. exit 0;;
  410. application/x-shellscript)
  411. exit 0;;
  412. esac
  413. }
  414. MIMETYPE="$( file --dereference --brief --mime-type -- "${FPATH}" )"
  415. handle_blocked "${MIMETYPE}"
  416. handle_extension
  417. handle_multimedia "${MIMETYPE}"
  418. handle_mime "${MIMETYPE}"
  419. handle_fallback
  420. exit 1