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.

preview-tui 5.9 KiB

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