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.
 
 
 
 
 
 

80 lines
1.8 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. # Shell: POSIX compliant
  21. # Authors: Todd Yamakawa, Léo Villeveygoux
  22. TERMINAL="${TERMINAL:-xterm}"
  23. PAGER="${PAGER:-less}"
  24. preview_file () {
  25. kill %- %+ 2>/dev/null
  26. clear
  27. # prevent shell pipe reuse
  28. tmpfifopath="${TMPDIR:-/tmp}/nnn-preview-tui-fifo.$$"
  29. mkfifo "$tmpfifopath" || return
  30. encoding="$(file -b --mime-encoding "$1")"
  31. $PAGER < "$tmpfifopath" &
  32. (
  33. exec > "$tmpfifopath"
  34. if [ -d "$1" ]; then
  35. # Print directory tree
  36. cd "$1" && tree
  37. elif [ "$encoding" = "binary" ] ; then
  38. # Binary file: just print filetype info
  39. echo "-------- Binary file --------"
  40. file -b "$1"
  41. else
  42. # Text file:
  43. cat "$1"
  44. fi &
  45. )
  46. rm "$tmpfifopath"
  47. }
  48. if [ "$PREVIEW_MODE" ] ; then
  49. if [ ! -r "$NNN_FIFO" ] ; then
  50. echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
  51. read -r
  52. exit 1
  53. fi
  54. preview_file "$1"
  55. exec < "$NNN_FIFO"
  56. while read -r selection ; do
  57. preview_file "$selection"
  58. done
  59. exit 0
  60. fi
  61. if [ -e "${TMUX%%,*}" ] && [ "$(tmux -V | cut -c6)" -eq 3 ] ; then
  62. tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1" -dh "$0" "$1"
  63. else
  64. PREVIEW_MODE=1 $TERMINAL -e "$0" "$1" &
  65. fi