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.
 
 
 
 
 
 

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