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.
 
 
 
 
 
 

114 lines
2.7 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. beginswith() {
  31. case $1 in "$2"*) true;; *) false;; esac;
  32. }
  33. preview_file () {
  34. kill "$(jobs -p)" 2>/dev/null
  35. clear
  36. encoding="$(file -b --mime-encoding "$1")"
  37. mimetype="$(file --dereference --brief --mime-type -- "$1")"
  38. ext="${1##*.}"
  39. if ! [ -z "$ext" ]; then
  40. ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
  41. fi
  42. if [ -d "$1" ]; then
  43. # Print directory tree
  44. cd "$1" || return
  45. # we use a FIFO to access less PID
  46. tmpfifopath="${TMPDIR:-/tmp}/nnn-preview-tui-fifo.$$"
  47. mkfifo "$tmpfifopath" || return
  48. $PAGER < "$tmpfifopath" &
  49. (
  50. exec > "$tmpfifopath"
  51. tree&
  52. )
  53. rm "$tmpfifopath"
  54. elif beginswith "$mimetype" "image/" ; then
  55. viu "$1" | head -n "$lines"
  56. elif beginswith "$mimetype" "text/troff" ; then
  57. man -l "$1" &
  58. elif [ "$encoding" = "binary" ] ; then
  59. # Binary file: just print filetype info
  60. echo "-------- binary file --------"
  61. file -b "$1"
  62. echo "-------- stat --------"
  63. stat "$1"
  64. else
  65. # Text file:
  66. $PAGER "$1" &
  67. fi
  68. }
  69. if [ "$PREVIEW_MODE" ] ; then
  70. if [ ! -r "$NNN_FIFO" ] ; then
  71. echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
  72. read -r
  73. exit 1
  74. fi
  75. preview_file "$1"
  76. exec < "$NNN_FIFO"
  77. while read -r selection ; do
  78. preview_file "$selection"
  79. done
  80. exit 0
  81. fi
  82. if [ -e "${TMUX%%,*}" ] && [ "$(tmux -V | cut -c6)" -eq 3 ] ; then
  83. if [ -z "$SPLIT" ]; then
  84. if [ "$(( lines * 2 ))" -gt "$cols" ]; then
  85. SPLIT='v'
  86. else
  87. SPLIT='h'
  88. fi
  89. elif [ "$SPLIT" != "h" ] && [ "$SPLIT" != "v" ] ; then
  90. SPLIT='h'
  91. fi
  92. tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1" -d"$SPLIT" "$0" "$1"
  93. else
  94. PREVIEW_MODE=1 $TERMINAL -e "$0" "$1" &
  95. fi