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.
 
 
 
 
 
 

250 lignes
7.9 KiB

  1. #!/usr/bin/env sh
  2. # Description: Terminal based file previewer
  3. #
  4. # Note: This plugin needs a "NNN_FIFO" to work. See man.
  5. #
  6. # Dependencies:
  7. # - Supports 3 independent methods to preview with:
  8. # - tmux (>=3.0), or
  9. # - kitty with allow_remote_control on, or
  10. # - $TERMINAL set to a terminal (it's xterm by default).
  11. # - less or $PAGER
  12. # - tree or exa or ls
  13. # - mediainfo or file
  14. # - mktemp
  15. # - unzip
  16. # - tar
  17. # - man
  18. # - optional: bat for code syntax highlighting
  19. # - optional: kitty terminal or catimg for images
  20. # - optional: scope.sh file viewer from ranger.
  21. # To use:
  22. # 1. drop scope.sh executable in $PATH
  23. # 2. set/export $USE_SCOPE as 1
  24. # - optional: pistol file viewer (https://github.com/doronbehar/pistol).
  25. # To use:
  26. # 1. install pistol
  27. # 2. set/export $USE_PISTOL as 1
  28. # - optional: ffmpegthumbnailer for video thumbnails (https://github.com/dirkvdb/ffmpegthumbnailer).
  29. # To use:
  30. # 1. install ffmpegthumbnailer
  31. # 2. set/export USE_VIDEOTHUMB as 1
  32. #
  33. # Usage:
  34. # You need to set a NNN_FIFO path and a key for the plugin with NNN_PLUG,
  35. # then start `nnn`:
  36. #
  37. # $ nnn -a
  38. #
  39. # or
  40. #
  41. # $ NNN_FIFO=/tmp/nnn.fifo nnn
  42. #
  43. # Then in `nnn`, launch the `preview-tui` plugin.
  44. #
  45. # If you provide the same NNN_FIFO to all nnn instances, there will be a
  46. # single common preview window. If you provide different FIFO path (e.g.
  47. # with -a), they will be independent.
  48. #
  49. # The previews will be shown in a tmux split. If that isn't possible, it
  50. # will try to use a kitty terminal split. And as a final fallback, a
  51. # different terminal window will be used ($TERMINAL).
  52. #
  53. # Tmux and kitty users can configure $SPLIT to either "h" or "v" to set a
  54. # 'h'orizontal split or a 'v'ertical split (as in, the line that splits the
  55. # windows will be horizontal or vertical).
  56. #
  57. # Kitty users need `allow_remote_control` set to `yes`. To customize the
  58. # window split, `enabled_layouts` has to be set to `all` or `splits` (the
  59. # former is the default value). This terminal is also able to show images
  60. # without extra dependencies.
  61. #
  62. # Shell: POSIX compliant
  63. # Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste, Mario Ortiz Manero
  64. SPLIT="$SPLIT" # you can set a permanent split here
  65. TERMINAL="$TERMINAL" # same goes for the terminal
  66. USE_SCOPE="${USE_SCOPE:-0}"
  67. USE_PISTOL="${USE_PISTOL:-0}"
  68. USE_VIDEOTHUMB="${USE_VIDEOTHUMB:-0}"
  69. PAGER="${PAGER:-less -R}"
  70. [ "$PAGER" = "most" ] && PAGER="less -R"
  71. if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.'; then
  72. TERMINAL=tmux
  73. elif [ -n "$KITTY_WINDOW_ID" ] && kitty @ ls >/dev/null 2>&1; then
  74. TERMINAL=kitty
  75. else
  76. TERMINAL="${TERMINAL:-xterm}"
  77. fi
  78. if [ -z "$SPLIT" ] && [ $(($(tput lines) * 2)) -gt "$(tput cols)" ]; then
  79. SPLIT='h'
  80. elif [ "$SPLIT" != 'h' ]; then
  81. SPLIT='v'
  82. fi
  83. exists() {
  84. which "$1" >/dev/null 2>&1
  85. }
  86. fifo_pager() {
  87. cmd="$1"
  88. shift
  89. # We use a FIFO to access $PAGER PID in jobs control
  90. tmpfifopath="${TMPDIR:-/tmp}/nnn-preview-tui-fifo.$$"
  91. mkfifo "$tmpfifopath" || return
  92. $PAGER < "$tmpfifopath" &
  93. (
  94. exec > "$tmpfifopath"
  95. "$cmd" "$@" &
  96. )
  97. rm "$tmpfifopath"
  98. }
  99. # Binary file: show file info inside the pager
  100. print_bin_info() {
  101. printf -- "-------- \033[1;31mBinary file\033[0m --------\n"
  102. if exists mediainfo; then
  103. mediainfo "$1" 2>/dev/null
  104. else
  105. file -b "$1"
  106. fi
  107. }
  108. preview_file () {
  109. kill %- %+ 2>/dev/null && wait %- %+ 2>/dev/null
  110. clear
  111. # Trying to use pistol if it's available.
  112. if [ "$USE_PISTOL" -ne 0 ] && exists pistol; then
  113. fifo_pager pistol "$1"
  114. return
  115. fi
  116. # Trying to use scope.sh if it's available.
  117. if [ "$USE_SCOPE" -ne 0 ] && exists scope.sh; then
  118. fifo_pager scope.sh "$1" "$cols" "$lines" "$(mktemp -d)" \
  119. "True" 2>/dev/null
  120. return
  121. fi
  122. # Detecting the exact type of the file: the encoding, mime type, and
  123. # extension in lowercase.
  124. encoding="$(file -Lb --mime-encoding -- "$1")"
  125. mimetype="$(file -Lb --mime-type -- "$1")"
  126. ext="${1##*.}"
  127. if [ -n "$ext" ]; then
  128. ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
  129. fi
  130. lines=$(($(tput lines)-1))
  131. cols=$(tput cols)
  132. # Otherwise, falling back to the defaults.
  133. if [ -d "$1" ]; then
  134. cd "$1" || return
  135. if exists tree; then
  136. fifo_pager tree -L 3 -F
  137. elif exists exa; then
  138. fifo_pager exa -G --colour=always 2>/dev/null
  139. else
  140. fifo_pager ls --color=always
  141. fi
  142. elif [ "$encoding" = "binary" ]; then
  143. if [ "${mimetype%%/*}" = "image" ] ; then
  144. if [ "$TERMINAL" = "kitty" ]; then
  145. # Kitty terminal users can use the native image preview method.
  146. kitty +kitten icat --silent --transfer-mode=stream --stdin=no \
  147. "$1" &
  148. elif exists catimg; then
  149. catimg "$1"
  150. elif exists viu; then
  151. viu -t "$1"
  152. else
  153. fifo_pager print_bin_info "$1"
  154. fi
  155. elif [ "${mimetype%%/*}" = "video" ] && [ "$USE_VIDEOTHUMB" -ne 0 ] && exists ffmpegthumbnailer; then
  156. ffmpegthumbnailer -s 0 -i "$1" -o "/tmp/videothumb.png"
  157. if [ "$TERMINAL" = "kitty" ]; then
  158. # Kitty terminal users can use the native image preview method.
  159. kitty +kitten icat --silent --transfer-mode=stream --stdin=no \
  160. "/tmp/videothumb.png" &
  161. elif exists catimg; then
  162. catimg "/tmp/videothumb.png"
  163. elif exists viu; then
  164. viu -t "/tmp/videothumb.png"
  165. else
  166. fifo_pager print_bin_info "$1"
  167. fi
  168. elif [ "$mimetype" = "application/zip" ] ; then
  169. fifo_pager unzip -l "$1"
  170. elif [ "$ext" = "gz" ] || [ "$ext" = "bz2" ] ; then
  171. fifo_pager tar -tvf "$1"
  172. else
  173. fifo_pager print_bin_info "$1"
  174. fi
  175. elif [ "$mimetype" = "text/troff" ] ; then
  176. fifo_pager man -Pcat -l "$1"
  177. else
  178. if exists bat; then
  179. fifo_pager bat --terminal-width="$cols" --paging=never --decorations=always --color=always \
  180. "$1" 2>/dev/null
  181. else
  182. $PAGER "$1" &
  183. fi
  184. fi
  185. }
  186. if [ "$PREVIEW_MODE" ] ; then
  187. if [ ! -r "$NNN_FIFO" ] ; then
  188. echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
  189. read -r
  190. exit 1
  191. fi
  192. preview_file "$1"
  193. # use cat instead of 'exec <' to avoid issues with dash shell
  194. # shellcheck disable=SC2002
  195. cat "$NNN_FIFO" |\
  196. while read -r selection ; do
  197. preview_file "$selection"
  198. done
  199. # Restoring the previous layout for kitty users. This will only work for
  200. # kitty >= 0.18.0.
  201. if [ "$TERMINAL" = "kitty" ]; then
  202. kitty @ last-used-layout --no-response >/dev/null 2>&1
  203. fi
  204. exit 0
  205. fi
  206. if [ "$TERMINAL" = "tmux" ]; then
  207. # tmux splits are inverted
  208. if [ "$SPLIT" = "v" ]; then SPLIT="h"; else SPLIT="v"; fi
  209. tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1" -d"$SPLIT" "$0" "$1"
  210. elif [ "$TERMINAL" = "kitty" ]; then
  211. # Setting the layout for the new window. It will be restored after the
  212. # script ends.
  213. kitty @ goto-layout splits >/dev/null
  214. # Trying to use kitty's integrated window management as the split window.
  215. # All environmental variables that will be used in the new window must
  216. # be explicitly passed.
  217. kitty @ launch --no-response --title "nnn preview" --keep-focus \
  218. --cwd "$PWD" --env "PATH=$PATH" --env "NNN_FIFO=$NNN_FIFO" \
  219. --env "PREVIEW_MODE=1" --env "PAGER=$PAGER" \
  220. --env "USE_SCOPE=$USE_SCOPE" --env "SPLIT=$SPLIT" \
  221. --env "USE_PISTOL=$USE_PISTOL" \
  222. --location "${SPLIT}split" "$0" "$1" >/dev/null
  223. else
  224. PREVIEW_MODE=1 $TERMINAL -e "$0" "$1" &
  225. fi