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.
 
 
 
 
 
 

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