1
1
mirror of https://github.com/jarun/nnn.git synced 2024-11-12 21:41:22 +03:00
nnn/plugins/preview-tui

100 lines
2.3 KiB
Plaintext
Raw Normal View History

#!/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 $TERMINAL, less or $PAGER, file, tree
#
2020-05-06 08:12:29 +03:00
# 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.
#
# Configure SPLIT to either "h" or "v" to set a 'h'orizontal split or a
# 'v'ertical split
#
2020-05-06 08:12:29 +03:00
# Shell: POSIX compliant
# Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste
TERMINAL="${TERMINAL:-xterm}"
PAGER="${PAGER:-less}"
SPLIT=
lines=$(($(tput lines)-1))
cols=$(tput cols)
preview_file () {
kill "$(jobs -p)" 2>/dev/null
clear
encoding="$(file -b --mime-encoding "$1")"
if [ -d "$1" ]; then
# Print directory tree
cd "$1" || return
# we use a FIFO to access less PID
tmpfifopath="${TMPDIR:-/tmp}/nnn-preview-tui-fifo.$$"
mkfifo "$tmpfifopath" || return
$PAGER < "$tmpfifopath" &
(
exec > "$tmpfifopath"
tree&
)
rm "$tmpfifopath"
elif [ "$encoding" = "binary" ] ; then
# Binary file: just print filetype info
echo "-------- binary file --------"
file -b "$1"
2020-06-01 12:49:24 +03:00
echo "-------- stat --------"
stat "$1"
else
# Text file:
$PAGER "$1" &
fi
}
if [ "$PREVIEW_MODE" ] ; then
if [ ! -r "$NNN_FIFO" ] ; then
echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
read -r
exit 1
fi
preview_file "$1"
exec < "$NNN_FIFO"
while read -r selection ; do
preview_file "$selection"
done
exit 0
fi
if [ -e "${TMUX%%,*}" ] && [ "$(tmux -V | cut -c6)" -eq 3 ] ; then
if [ -z "$SPLIT" ]; then
if [ "$(( lines * 2 ))" -gt "$cols" ]; then
2020-06-01 12:47:40 +03:00
SPLIT='v'
else
2020-06-01 12:47:40 +03:00
SPLIT='h'
fi
2020-06-01 12:47:40 +03:00
elif [ "$SPLIT" != "h" ] && [ "$SPLIT" != "v" ] ; then
SPLIT='h'
fi
2020-06-01 12:47:40 +03:00
tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1" -d"$SPLIT" "$0" "$1"
else
PREVIEW_MODE=1 $TERMINAL -e "$0" "$1" &
fi