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.
 
 
 
 
 
 

60 lines
1.6 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 set $TERMINAL), 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. # Shell: POSIX compliant
  21. # Authors: Todd Yamakawa, Léo Villeveygoux
  22. TERMINAL="${TERMINAL:-xterm}"
  23. if [ "$PREVIEW_MODE" ] ; then
  24. if [ ! -r "$NNN_FIFO" ] ; then
  25. echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
  26. read -r
  27. exit 1
  28. fi
  29. exec < "$NNN_FIFO"
  30. while read -r selection ; do
  31. clear
  32. lines=$(($(tput lines)-1))
  33. cols=$(tput cols)
  34. mime="$(file -b --mime-type "$selection")"
  35. if [ -d "$selection" ]; then
  36. # Print directory tree
  37. cd "$selection" && tree | head -n $lines | cut -c 1-"$cols"
  38. elif [ "${mime%%/*}" = "text" ] ; then
  39. # Print file head
  40. head -n $lines "$selection" | cut -c 1-"$cols"
  41. else
  42. # Binary file
  43. echo "-------- Binary file --------"
  44. file -b "$selection"
  45. fi
  46. done
  47. exit 0
  48. fi
  49. if [ -e "${TMUX%%,*}" ] && [ "$(tmux -V | cut -c6)" -eq 3 ] ; then
  50. tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1" -dh "$0"
  51. else
  52. PREVIEW_MODE=1 $TERMINAL -e "$0" &
  53. fi