2020-05-06 02:08:10 +03:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
# Description: Text based file previewer
|
|
|
|
#
|
|
|
|
# Note: This plugin needs a "NNN_FIFO" to work.
|
|
|
|
#
|
2020-05-27 21:02:22 +03:00
|
|
|
# Dependencies: tmux (>=3.0) or xterm or $TERMINAL, less or $PAGER, file, tree
|
2020-05-06 02:08:10 +03:00
|
|
|
#
|
2020-05-06 08:12:29 +03:00
|
|
|
# Usage:
|
2020-05-06 02:08:10 +03:00
|
|
|
# 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.
|
|
|
|
#
|
2020-05-06 08:12:29 +03:00
|
|
|
# Shell: POSIX compliant
|
|
|
|
# Authors: Todd Yamakawa, Léo Villeveygoux
|
2020-05-06 02:08:10 +03:00
|
|
|
|
|
|
|
TERMINAL="${TERMINAL:-xterm}"
|
2020-05-27 21:02:22 +03:00
|
|
|
PAGER="${PAGER:-less}"
|
2020-05-06 02:08:10 +03:00
|
|
|
|
2020-05-06 20:25:14 +03:00
|
|
|
preview_file () {
|
2020-05-28 02:34:53 +03:00
|
|
|
kill "$(jobs -p)" 2>/dev/null
|
2020-05-06 20:25:14 +03:00
|
|
|
clear
|
2020-05-27 21:02:22 +03:00
|
|
|
|
2020-05-22 05:48:00 +03:00
|
|
|
encoding="$(file -b --mime-encoding "$1")"
|
2020-05-06 20:25:14 +03:00
|
|
|
|
2020-05-28 02:34:53 +03:00
|
|
|
if [ -d "$1" ]; then
|
|
|
|
# Print directory tree
|
|
|
|
|
|
|
|
cd "$1" || return
|
2020-05-27 21:02:22 +03:00
|
|
|
|
2020-05-28 02:34:53 +03:00
|
|
|
# we use a FIFO to access less PID
|
|
|
|
tmpfifopath="${TMPDIR:-/tmp}/nnn-preview-tui-fifo.$$"
|
|
|
|
mkfifo "$tmpfifopath" || return
|
2020-05-27 21:02:22 +03:00
|
|
|
|
2020-05-28 02:34:53 +03:00
|
|
|
$PAGER < "$tmpfifopath" &
|
2020-05-27 21:02:22 +03:00
|
|
|
|
2020-05-28 02:34:53 +03:00
|
|
|
(
|
|
|
|
exec > "$tmpfifopath"
|
|
|
|
tree&
|
|
|
|
)
|
2020-05-27 21:02:22 +03:00
|
|
|
|
2020-05-28 02:34:53 +03:00
|
|
|
rm "$tmpfifopath"
|
|
|
|
elif [ "$encoding" = "binary" ] ; then
|
|
|
|
# Binary file: just print filetype info
|
|
|
|
echo "-------- Binary file --------"
|
|
|
|
file -b "$1"
|
|
|
|
else
|
|
|
|
# Text file:
|
|
|
|
$PAGER "$1" &
|
|
|
|
fi
|
2020-05-06 20:25:14 +03:00
|
|
|
}
|
|
|
|
|
2020-05-06 02:08:10 +03:00
|
|
|
if [ "$PREVIEW_MODE" ] ; then
|
2020-05-06 07:24:31 +03:00
|
|
|
if [ ! -r "$NNN_FIFO" ] ; then
|
|
|
|
echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
|
|
|
|
read -r
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-05-06 20:25:14 +03:00
|
|
|
preview_file "$1"
|
|
|
|
|
2020-05-06 02:08:10 +03:00
|
|
|
exec < "$NNN_FIFO"
|
|
|
|
while read -r selection ; do
|
2020-05-06 20:25:14 +03:00
|
|
|
preview_file "$selection"
|
2020-05-06 02:08:10 +03:00
|
|
|
done
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2020-05-06 07:24:31 +03:00
|
|
|
if [ -e "${TMUX%%,*}" ] && [ "$(tmux -V | cut -c6)" -eq 3 ] ; then
|
2020-05-06 20:25:14 +03:00
|
|
|
tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1" -dh "$0" "$1"
|
2020-05-06 02:08:10 +03:00
|
|
|
else
|
2020-05-06 20:25:14 +03:00
|
|
|
PREVIEW_MODE=1 $TERMINAL -e "$0" "$1" &
|
2020-05-06 02:08:10 +03:00
|
|
|
fi
|