2020-05-05 23:08:10 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
# Description: Text based file previewer
|
|
|
|
#
|
|
|
|
# Note: This plugin needs a "NNN_FIFO" to work.
|
|
|
|
#
|
2020-06-02 08:56:50 +00:00
|
|
|
# Dependencies: tmux (>=3.0) or xterm or $TERMINAL, less or $PAGER,
|
|
|
|
# file, stat, tree, man, tar, unzip, ...
|
|
|
|
# ... add you own! (see examples in code)
|
2020-05-05 23:08:10 +00:00
|
|
|
#
|
2020-05-06 05:12:29 +00:00
|
|
|
# Usage:
|
2020-05-05 23:08:10 +00:00
|
|
|
# You need to set a NNN_FIFO path and set a key for the plugin,
|
|
|
|
# then start `nnn`:
|
|
|
|
#
|
2020-06-02 08:56:50 +00:00
|
|
|
# $ nnn -a
|
|
|
|
#
|
|
|
|
# or
|
|
|
|
#
|
2020-05-05 23:08:10 +00:00
|
|
|
# $ 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
|
2020-06-02 08:56:50 +00:00
|
|
|
# single common preview window. I you provide different FIFO path (e.g.
|
|
|
|
# with -a), they will be independent.
|
2020-05-05 23:08:10 +00:00
|
|
|
#
|
2020-06-01 08:42:34 +00:00
|
|
|
# Configure SPLIT to either "h" or "v" to set a 'h'orizontal split or a
|
|
|
|
# 'v'ertical split
|
|
|
|
#
|
2020-05-06 05:12:29 +00:00
|
|
|
# Shell: POSIX compliant
|
2020-06-01 08:42:34 +00:00
|
|
|
# Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste
|
2020-05-05 23:08:10 +00:00
|
|
|
|
|
|
|
TERMINAL="${TERMINAL:-xterm}"
|
2020-06-02 08:56:50 +00:00
|
|
|
PAGER="${PAGER:-less -R}"
|
|
|
|
|
|
|
|
fifo_pager() {
|
|
|
|
cmd="$1"
|
|
|
|
shift
|
|
|
|
|
|
|
|
# We use a FIFO to access $PAGER PID in jobs control
|
|
|
|
tmpfifopath="${TMPDIR:-/tmp}/nnn-preview-tui-fifo.$$"
|
|
|
|
mkfifo "$tmpfifopath" || return
|
2020-06-01 08:42:34 +00:00
|
|
|
|
2020-06-02 08:56:50 +00:00
|
|
|
$PAGER < "$tmpfifopath" &
|
2020-05-05 23:08:10 +00:00
|
|
|
|
2020-06-02 08:56:50 +00:00
|
|
|
(
|
|
|
|
exec > "$tmpfifopath"
|
|
|
|
"$cmd" "$@" &
|
|
|
|
)
|
|
|
|
|
|
|
|
rm "$tmpfifopath"
|
2020-06-01 13:52:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 17:25:14 +00:00
|
|
|
preview_file () {
|
2020-06-04 02:53:04 +00:00
|
|
|
kill %- %+ 2>/dev/null && wait %- %+ 2>/dev/null
|
2020-05-06 17:25:14 +00:00
|
|
|
clear
|
2020-05-27 18:02:22 +00:00
|
|
|
|
2020-06-02 08:56:50 +00:00
|
|
|
encoding="$(file -Lb --mime-encoding -- "$1")"
|
2020-06-02 00:22:47 +00:00
|
|
|
|
|
|
|
# Detect mime type
|
2020-06-02 08:56:50 +00:00
|
|
|
mimetype="$(file -Lb --mime-type -- "$1")"
|
2020-06-01 13:52:16 +00:00
|
|
|
|
2020-06-04 02:53:04 +00:00
|
|
|
# Detect file extention
|
2020-06-01 13:52:16 +00:00
|
|
|
ext="${1##*.}"
|
2020-06-02 08:56:50 +00:00
|
|
|
if [ -n "$ext" ]; then
|
2020-06-01 13:52:16 +00:00
|
|
|
ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
|
|
|
|
fi
|
2020-05-06 17:25:14 +00:00
|
|
|
|
2020-05-27 23:34:53 +00:00
|
|
|
if [ -d "$1" ]; then
|
|
|
|
# Print directory tree
|
|
|
|
cd "$1" || return
|
2020-06-02 08:56:50 +00:00
|
|
|
fifo_pager tree
|
|
|
|
#elif [ "${mimetype%%/*}" = "image" ] ; then
|
|
|
|
# catimg "$1"
|
|
|
|
elif [ "$mimetype" = "text/troff" ] ; then
|
|
|
|
fifo_pager man -Pcat -l "$1"
|
|
|
|
elif [ "$mimetype" = "application/zip" ] ; then
|
|
|
|
fifo_pager unzip -l "$1"
|
2020-06-02 00:22:47 +00:00
|
|
|
elif [ "$ext" = "gz" ] || [ "$ext" = "bz2" ] ; then
|
2020-06-02 08:56:50 +00:00
|
|
|
fifo_pager tar -tvf "$1"
|
2020-05-27 23:34:53 +00:00
|
|
|
elif [ "$encoding" = "binary" ] ; then
|
|
|
|
# Binary file: just print filetype info
|
2020-06-01 08:42:34 +00:00
|
|
|
echo "-------- binary file --------"
|
2020-05-27 23:34:53 +00:00
|
|
|
file -b "$1"
|
2020-06-02 08:56:50 +00:00
|
|
|
echo
|
2020-06-01 08:42:34 +00:00
|
|
|
stat "$1"
|
2020-05-27 23:34:53 +00:00
|
|
|
else
|
|
|
|
# Text file:
|
|
|
|
$PAGER "$1" &
|
|
|
|
fi
|
2020-05-06 17:25:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 23:08:10 +00:00
|
|
|
if [ "$PREVIEW_MODE" ] ; then
|
2020-05-06 04:24:31 +00:00
|
|
|
if [ ! -r "$NNN_FIFO" ] ; then
|
|
|
|
echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
|
|
|
|
read -r
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-05-06 17:25:14 +00:00
|
|
|
preview_file "$1"
|
|
|
|
|
2020-05-05 23:08:10 +00:00
|
|
|
exec < "$NNN_FIFO"
|
|
|
|
while read -r selection ; do
|
2020-05-06 17:25:14 +00:00
|
|
|
preview_file "$selection"
|
2020-05-05 23:08:10 +00:00
|
|
|
done
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2020-06-03 22:11:42 +00:00
|
|
|
if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.' ; then
|
2020-06-02 08:56:50 +00:00
|
|
|
if [ -z "$SPLIT" ] && [ $(($(tput lines) * 2)) -gt "$(tput cols)" ] ; then
|
|
|
|
SPLIT='v'
|
|
|
|
elif [ "$SPLIT" != 'v' ] ; then
|
2020-06-01 09:47:40 +00:00
|
|
|
SPLIT='h'
|
2020-06-01 08:42:34 +00:00
|
|
|
fi
|
2020-06-01 09:47:40 +00:00
|
|
|
|
|
|
|
tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1" -d"$SPLIT" "$0" "$1"
|
2020-05-05 23:08:10 +00:00
|
|
|
else
|
2020-05-06 17:25:14 +00:00
|
|
|
PREVIEW_MODE=1 $TERMINAL -e "$0" "$1" &
|
2020-05-05 23:08:10 +00:00
|
|
|
fi
|