My build of nnn with minor changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

preview-tui 6.1 KiB

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