My build of nnn with minor changes
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

100 řádky
2.3 KiB

  1. #!/usr/bin/env sh
  2. # Description: Text based file previewer
  3. #
  4. # Note: This plugin needs a "NNN_FIFO" to work.
  5. #
  6. # Dependencies: tmux (>=3.0) or xterm or $TERMINAL, less or $PAGER, file, tree
  7. #
  8. # Usage:
  9. # You need to set a NNN_FIFO path and set a key for the plugin,
  10. # then start `nnn`:
  11. #
  12. # $ NNN_FIFO=/tmp/nnn.fifo nnn
  13. #
  14. # Then in `nnn`, launch the `preview-tui` plugin.
  15. #
  16. # If you provide the same NNN_FIFO to all nnn instances, there will be a
  17. # single common preview window. I you provide different FIFO path, they
  18. # will be independent.
  19. #
  20. # Configure SPLIT to either "h" or "v" to set a 'h'orizontal split or a
  21. # 'v'ertical split
  22. #
  23. # Shell: POSIX compliant
  24. # Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste
  25. TERMINAL="${TERMINAL:-xterm}"
  26. PAGER="${PAGER:-less}"
  27. SPLIT=
  28. lines=$(($(tput lines)-1))
  29. cols=$(tput cols)
  30. preview_file () {
  31. kill "$(jobs -p)" 2>/dev/null
  32. clear
  33. encoding="$(file -b --mime-encoding "$1")"
  34. if [ -d "$1" ]; then
  35. # Print directory tree
  36. cd "$1" || return
  37. # we use a FIFO to access less PID
  38. tmpfifopath="${TMPDIR:-/tmp}/nnn-preview-tui-fifo.$$"
  39. mkfifo "$tmpfifopath" || return
  40. $PAGER < "$tmpfifopath" &
  41. (
  42. exec > "$tmpfifopath"
  43. tree&
  44. )
  45. rm "$tmpfifopath"
  46. elif [ "$encoding" = "binary" ] ; then
  47. # Binary file: just print filetype info
  48. echo "-------- binary file --------"
  49. file -b "$1"
  50. echo "-------- stat --------"
  51. stat "$1"
  52. else
  53. # Text file:
  54. $PAGER "$1" &
  55. fi
  56. }
  57. if [ "$PREVIEW_MODE" ] ; then
  58. if [ ! -r "$NNN_FIFO" ] ; then
  59. echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
  60. read -r
  61. exit 1
  62. fi
  63. preview_file "$1"
  64. exec < "$NNN_FIFO"
  65. while read -r selection ; do
  66. preview_file "$selection"
  67. done
  68. exit 0
  69. fi
  70. if [ -e "${TMUX%%,*}" ] && [ "$(tmux -V | cut -c6)" -eq 3 ] ; then
  71. if [ -z "$SPLIT" ]; then
  72. if [ "$(( lines * 2 ))" -gt "$cols" ]; then
  73. SPLIT='v'
  74. else
  75. SPLIT='h'
  76. fi
  77. elif [ "$SPLIT" != "h" ] && [ "$SPLIT" != "v" ] ; then
  78. SPLIT='h'
  79. fi
  80. tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1" -d"$SPLIT" "$0" "$1"
  81. else
  82. PREVIEW_MODE=1 $TERMINAL -e "$0" "$1" &
  83. fi