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.
 
 
 
 
 
 

232 lines
7.0 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. If 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. [ "$PAGER" = "most" ] && PAGER="less -R"
  66. if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.'; then
  67. TERMINAL=tmux
  68. elif [ -n "$KITTY_WINDOW_ID" ] && kitty @ ls >/dev/null 2>&1; then
  69. TERMINAL=kitty
  70. else
  71. TERMINAL="${TERMINAL:-xterm}"
  72. fi
  73. if [ -z "$SPLIT" ] && [ $(($(tput lines) * 2)) -gt "$(tput cols)" ]; then
  74. SPLIT='h'
  75. elif [ "$SPLIT" != 'h' ]; then
  76. SPLIT='v'
  77. fi
  78. exists() {
  79. which "$1" >/dev/null 2>&1
  80. }
  81. fifo_pager() {
  82. cmd="$1"
  83. shift
  84. # We use a FIFO to access $PAGER PID in jobs control
  85. tmpfifopath="${TMPDIR:-/tmp}/nnn-preview-tui-fifo.$$"
  86. mkfifo "$tmpfifopath" || return
  87. $PAGER < "$tmpfifopath" &
  88. (
  89. exec > "$tmpfifopath"
  90. "$cmd" "$@" &
  91. )
  92. rm "$tmpfifopath"
  93. }
  94. # Binary file: show file info inside the pager
  95. print_bin_info() {
  96. printf -- "-------- \033[1;31mBinary file\033[0m --------\n"
  97. if exists mediainfo; then
  98. mediainfo "$1" 2>/dev/null
  99. else
  100. file -b "$1"
  101. fi
  102. }
  103. preview_file () {
  104. kill %- %+ 2>/dev/null && wait %- %+ 2>/dev/null
  105. clear
  106. # Trying to use pistol if it's available.
  107. if [ "$USE_PISTOL" -ne 0 ] && exists pistol; then
  108. fifo_pager pistol "$1"
  109. return
  110. fi
  111. # Trying to use scope.sh if it's available.
  112. if [ "$USE_SCOPE" -ne 0 ] && exists scope.sh; then
  113. fifo_pager scope.sh "$1" "$cols" "$lines" "$(mktemp -d)" \
  114. "True" 2>/dev/null
  115. return
  116. fi
  117. # Detecting the exact type of the file: the encoding, mime type, and
  118. # extension in lowercase.
  119. encoding="$(file -Lb --mime-encoding -- "$1")"
  120. mimetype="$(file -Lb --mime-type -- "$1")"
  121. ext="${1##*.}"
  122. if [ -n "$ext" ]; then
  123. ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
  124. fi
  125. lines=$(($(tput lines)-1))
  126. cols=$(tput cols)
  127. # Otherwise, falling back to the defaults.
  128. if [ -d "$1" ]; then
  129. cd "$1" || return
  130. if exists tree; then
  131. fifo_pager tree -L 3 -F
  132. elif exists exa; then
  133. fifo_pager exa -G --colour=always 2>/dev/null
  134. else
  135. fifo_pager ls --color=always
  136. fi
  137. elif [ "$encoding" = "binary" ]; then
  138. if [ "${mimetype%%/*}" = "image" ] ; then
  139. if [ "$TERMINAL" = "kitty" ]; then
  140. # Kitty terminal users can use the native image preview method.
  141. kitty +kitten icat --silent --transfer-mode=stream --stdin=no \
  142. "$1" &
  143. elif exists catimg; then
  144. catimg "$1"
  145. elif exists viu; then
  146. viu -t "$1"
  147. else
  148. fifo_pager print_bin_info "$1"
  149. fi
  150. elif [ "$mimetype" = "application/zip" ] ; then
  151. fifo_pager unzip -l "$1"
  152. elif [ "$ext" = "gz" ] || [ "$ext" = "bz2" ] ; then
  153. fifo_pager tar -tvf "$1"
  154. else
  155. fifo_pager print_bin_info "$1"
  156. fi
  157. elif [ "$mimetype" = "text/troff" ] ; then
  158. fifo_pager man -Pcat -l "$1"
  159. else
  160. if exists bat; then
  161. fifo_pager bat --terminal-width="$cols" --paging=never --decorations=always --color=always \
  162. "$1" 2>/dev/null
  163. else
  164. $PAGER "$1" &
  165. fi
  166. fi
  167. }
  168. if [ "$PREVIEW_MODE" ] ; then
  169. if [ ! -r "$NNN_FIFO" ] ; then
  170. echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
  171. read -r
  172. exit 1
  173. fi
  174. preview_file "$1"
  175. # use cat instead of 'exec <' to avoid issues with dash shell
  176. # shellcheck disable=SC2002
  177. cat "$NNN_FIFO" |\
  178. while read -r selection ; do
  179. preview_file "$selection"
  180. done
  181. # Restoring the previous layout for kitty users. This will only work for
  182. # kitty >= 0.18.0.
  183. if [ "$TERMINAL" = "kitty" ]; then
  184. kitty @ last-used-layout --no-response >/dev/null 2>&1
  185. fi
  186. exit 0
  187. fi
  188. if [ "$TERMINAL" = "tmux" ]; then
  189. # tmux splits are inverted
  190. if [ "$SPLIT" = "v" ]; then SPLIT="h"; else SPLIT="v"; fi
  191. tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1" -d"$SPLIT" "$0" "$1"
  192. elif [ "$TERMINAL" = "kitty" ]; then
  193. # Setting the layout for the new window. It will be restored after the
  194. # script ends.
  195. kitty @ goto-layout splits >/dev/null
  196. # Trying to use kitty's integrated window management as the split window.
  197. # All environmental variables that will be used in the new window must
  198. # be explicitly passed.
  199. kitty @ launch --no-response --title "nnn preview" --keep-focus \
  200. --cwd "$PWD" --env "PATH=$PATH" --env "NNN_FIFO=$NNN_FIFO" \
  201. --env "PREVIEW_MODE=1" --env "PAGER=$PAGER" \
  202. --env "USE_SCOPE=$USE_SCOPE" --env "SPLIT=$SPLIT" \
  203. --env "USE_PISTOL=$USE_PISTOL" \
  204. --location "${SPLIT}split" "$0" "$1" >/dev/null
  205. else
  206. PREVIEW_MODE=1 $TERMINAL -e "$0" "$1" &
  207. fi