My build of nnn with minor changes
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

120 wiersze
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,
  7. # file, stat, tree, man, tar, unzip, ...
  8. # ... add you own! (see examples in code)
  9. #
  10. # Usage:
  11. # You need to set a NNN_FIFO path and set a key for the plugin,
  12. # then start `nnn`:
  13. #
  14. # $ nnn -a
  15. #
  16. # or
  17. #
  18. # $ NNN_FIFO=/tmp/nnn.fifo nnn
  19. #
  20. # Then in `nnn`, launch the `preview-tui` plugin.
  21. #
  22. # If you provide the same NNN_FIFO to all nnn instances, there will be a
  23. # single common preview window. I you provide different FIFO path (e.g.
  24. # with -a), they will be independent.
  25. #
  26. # Configure SPLIT to either "h" or "v" to set a 'h'orizontal split or a
  27. # 'v'ertical split
  28. #
  29. # Shell: POSIX compliant
  30. # Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste
  31. TERMINAL="${TERMINAL:-xterm}"
  32. PAGER="${PAGER:-less -R}"
  33. fifo_pager() {
  34. cmd="$1"
  35. shift
  36. # We use a FIFO to access $PAGER PID in jobs control
  37. tmpfifopath="${TMPDIR:-/tmp}/nnn-preview-tui-fifo.$$"
  38. mkfifo "$tmpfifopath" || return
  39. $PAGER < "$tmpfifopath" &
  40. (
  41. exec > "$tmpfifopath"
  42. "$cmd" "$@" &
  43. )
  44. rm "$tmpfifopath"
  45. }
  46. preview_file () {
  47. kill %- %+ 2>/dev/null
  48. clear
  49. encoding="$(file -Lb --mime-encoding -- "$1")"
  50. # Detect mime type
  51. mimetype="$(file -Lb --mime-type -- "$1")"
  52. # Detect file extention - use if you need
  53. ext="${1##*.}"
  54. if [ -n "$ext" ]; then
  55. ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
  56. fi
  57. if [ -d "$1" ]; then
  58. # Print directory tree
  59. cd "$1" || return
  60. fifo_pager tree
  61. #elif [ "${mimetype%%/*}" = "image" ] ; then
  62. # catimg "$1"
  63. elif [ "$mimetype" = "text/troff" ] ; then
  64. fifo_pager man -Pcat -l "$1"
  65. elif [ "$mimetype" = "application/zip" ] ; then
  66. fifo_pager unzip -l "$1"
  67. elif [ "$ext" = "gz" ] || [ "$ext" = "bz2" ] ; then
  68. fifo_pager tar -tvf "$1"
  69. elif [ "$encoding" = "binary" ] ; then
  70. # Binary file: just print filetype info
  71. echo "-------- binary file --------"
  72. file -b "$1"
  73. echo
  74. stat "$1"
  75. else
  76. # Text file:
  77. $PAGER "$1" &
  78. fi
  79. }
  80. if [ "$PREVIEW_MODE" ] ; then
  81. if [ ! -r "$NNN_FIFO" ] ; then
  82. echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
  83. read -r
  84. exit 1
  85. fi
  86. preview_file "$1"
  87. exec < "$NNN_FIFO"
  88. while read -r selection ; do
  89. preview_file "$selection"
  90. done
  91. exit 0
  92. fi
  93. if [ -e "${TMUX%%,*}" ] && [ "$(tmux -V | cut -c6)" -eq 3 ] ; then
  94. if [ -z "$SPLIT" ] && [ $(($(tput lines) * 2)) -gt "$(tput cols)" ] ; then
  95. SPLIT='v'
  96. elif [ "$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