|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- #!/usr/bin/env sh
-
- # Description: Text based file previewer
- #
- # Note: This plugin needs a "NNN_FIFO" to work.
- #
- # Dependencies: tmux (>=3.0) or xterm (or set $TERMINAL), file, tree
- #
- # Usage:
- # You need to set a NNN_FIFO path and set a key for the plugin,
- # then start `nnn`:
- #
- # $ NNN_FIFO=/tmp/nnn.fifo nnn
- #
- # Then in `nnn`, launch the `preview-tui` plugin.
- #
- # If you provide the same NNN_FIFO to all nnn instances, there will be a
- # single common preview window. I you provide different FIFO path, they
- # will be independent.
- #
- # Shell: POSIX compliant
- # Authors: Todd Yamakawa, Léo Villeveygoux
-
- TERMINAL="${TERMINAL:-xterm}"
-
- if [ "$PREVIEW_MODE" ] ; then
- if [ ! -r "$NNN_FIFO" ] ; then
- echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
- read -r
- exit 1
- fi
-
- exec < "$NNN_FIFO"
- while read -r selection ; do
- clear
- lines=$(($(tput lines)-1))
- cols=$(tput cols)
- mime="$(file -b --mime-type "$selection")"
-
- if [ -d "$selection" ]; then
- # Print directory tree
- cd "$selection" && tree | head -n $lines | cut -c 1-"$cols"
- elif [ "${mime%%/*}" = "text" ] ; then
- # Print file head
- head -n $lines "$selection" | cut -c 1-"$cols"
- else
- # Binary file
- echo "-------- Binary file --------"
- file -b "$selection"
- fi
- done
- exit 0
- fi
-
- if [ -e "${TMUX%%,*}" ] && [ "$(tmux -V | cut -c6)" -eq 3 ] ; then
- tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1" -dh "$0"
- else
- PREVIEW_MODE=1 $TERMINAL -e "$0" &
- fi
|