My build of nnn with minor changes
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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