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.
 
 
 
 
 
 

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