nnn/plugins/preview-tui

325 lines
11 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env sh
# Description: Terminal based file previewer
#
# Note: This plugin needs a "NNN_FIFO" to work. See man.
# For a more extended version of this script with additional optional dependencies, see preview-tui-ext.
#
# Dependencies:
2021-04-22 12:15:42 +00:00
# - Supports 4 independent methods to preview with:
# - tmux (>=3.0), or
# - kitty with allow_remote_control and listen_on set in kitty.conf, or
2021-04-22 12:15:42 +00:00
# - QuickLook on WSL (https://github.com/QL-Win/QuickLook)
# - $TERMINAL set to a terminal (it's xterm by default).
# - less or $PAGER
# - tree or exa or ls
# - mediainfo or file
# - mktemp
# - unzip
# - tar
# - man
# - optional: bat for code syntax highlighting
# - optional: ueberzug, kitty terminal, viu or catimg for images.
# - optional: scope.sh file viewer from ranger.
# To use:
# 1. drop scope.sh executable in $PATH
# 2. set/export $USE_SCOPE as 1
2020-06-10 00:41:57 +00:00
# - optional: pistol file viewer (https://github.com/doronbehar/pistol).
# To use:
# 1. install pistol
# 2. set/export $USE_PISTOL as 1
#
2020-05-06 05:12:29 +00:00
# Usage:
# You need to set a NNN_FIFO path and a key for the plugin with NNN_PLUG,
# then start `nnn`:
#
# $ nnn -a
#
# or
#
# $ 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-07-14 23:19:13 +00:00
# single common preview window. If you provide different FIFO path (e.g.
# with -a), they will be independent.
#
# The previews will be shown in a tmux split. If that isn't possible, it
# will try to use a kitty terminal split. And as a final fallback, a
# different terminal window will be used ($TERMINAL).
#
# Tmux and kitty users can configure $SPLIT to either "h" or "v" to set a
# 'h'orizontal split or a 'v'ertical split (as in, the line that splits the
# windows will be horizontal or vertical).
#
2021-04-30 13:29:11 +00:00
# Kitty users need `allow_remote_control` set to `yes`, and `listen_on` set
# to e.g. "$TMPDIR/kitty". To customize the window split, `enabled_layouts`
# has to be set to `all` or `splits` (the former is the default value).
# This terminal is also able to show images without extra dependencies.
#
2020-05-06 05:12:29 +00:00
# Shell: POSIX compliant
# Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste, Mario Ortiz Manero, Luuk van Baal
2021-05-16 00:11:05 +00:00
#SPLIT="$SPLIT" # you can set a permanent split here
#TERMINAL="$TERMINAL" # same goes for the terminal
USE_SCOPE="${USE_SCOPE:-0}"
2020-06-10 00:41:57 +00:00
USE_PISTOL="${USE_PISTOL:-0}"
PAGER="${PAGER:-less -P?n -R}"
TMPDIR="${TMPDIR:-/tmp}"
2021-05-16 00:11:05 +00:00
BAT_STYLE="${BAT_STYLE:-numbers}"
NNN_PARENT="${NNN_FIFO#*.}"
FIFOPID="$TMPDIR/nnn-preview-tui-fifopid.$NNN_PARENT"
PAGERPID="$TMPDIR/nnn-preview-tui-pagerpid.$NNN_PARENT"
IMGPID="$TMPDIR/nnn-preview-tui-imgpid.$NNN_PARENT"
CURSEL="$TMPDIR/nnn-preview-tui-selection.$NNN_PARENT"
FIFO_UEBERZUG="$TMPDIR/nnn-preview-tui-ueberzug-fifo.$NNN_PARENT"
2021-05-16 00:11:05 +00:00
start_preview() {
2021-04-26 15:21:52 +00:00
[ "$PAGER" = "most" ] && PAGER="less -R"
2021-04-26 15:21:52 +00:00
if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.'; then
TERMINAL=tmux
elif [ -n "$KITTY_LISTEN_ON" ]; then
2021-04-26 15:21:52 +00:00
TERMINAL=kitty
else
TERMINAL="${TERMINAL:-xterm}"
fi
if [ -z "$SPLIT" ] && [ $(($(tput lines) * 2)) -gt "$(tput cols)" ]; then
SPLIT='h'
elif [ "$SPLIT" != 'h' ]; then
SPLIT='v'
fi
2021-05-16 00:11:05 +00:00
case "$TERMINAL" in
tmux) # tmux splits are inverted
if [ "$SPLIT" = "v" ]; then DSPLIT="h"; else DSPLIT="v"; fi
tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1" -e "PAGER=$PAGER" \
-e "USE_SCOPE=$USE_SCOPE" -e "SPLIT=$SPLIT" -e "USE_PISTOL=$USE_PISTOL" \
-e "BAT_STYLE=$BAT_STYLE" -e "PAGERPID=$PAGERPID" -e "FIFOPID=$FIFOPID" \
-e "IMGPID=$IMGPID" -e "CURSEL=$CURSEL" -e "TMPDIR=$TMPDIR" \
-e "FIFO_UEBERZUG=$FIFO_UEBERZUG" -e "QLPATH=$QLPATH" -d"$DSPLIT" "$0" "$1" ;;
kitty) # Setting the layout for the new window. It will be restored after the script ends.
kitty @ goto-layout splits
# Trying to use kitty's integrated window management as the split window. All
# environmental variables that will be used in the new window must be explicitly passed.
kitty @ launch --no-response --title "nnn preview" --keep-focus \
--cwd "$PWD" --env "PATH=$PATH" --env "NNN_FIFO=$NNN_FIFO" --env "FIFOPID=$FIFOPID" \
--env "PREVIEW_MODE=1" --env "PAGER=$PAGER" --env "TMPDIR=$TMPDIR" \
--env "USE_SCOPE=$USE_SCOPE" --env "SPLIT=$SPLIT" --env "TERMINAL=$TERMINAL"\
--env "USE_PISTOL=$USE_PISTOL" --env "BAT_STYLE=${BAT_STYLE:-numbers}" \
--env "PAGERPID=$PAGERPID" --env "IMGPID=$IMGPID" --env "FIFO_UEBERZUG=$FIFO_UEBERZUG" \
--env "CURSEL=$CURSEL" --location "${SPLIT}split" "$0" "$1" ;;
*) if [ -n "$2" ]; then
QUICKLOOK=1 QLPATH="$2" PREVIEW_MODE=1 "$0" "$1" &
else
PAGERPID="$PAGERPID" IMGPID="$IMGPID" CURSEL="$CURSEL" PREVIEW_MODE=1 \
FIFOPID="$FIFOPID" FIFO_UEBERZUG="$FIFO_UEBERZUG" $TERMINAL -e "$0" "$1" &
fi ;;
esac
} >/dev/null 2>&1
2021-04-26 15:21:52 +00:00
2021-05-16 00:11:05 +00:00
toggle_preview() {
2021-04-26 15:21:52 +00:00
if exists QuickLook.exe; then
QLPATH="QuickLook.exe"
elif exists Bridge.exe; then
QLPATH="Bridge.exe"
fi
2021-05-16 00:11:05 +00:00
if pkill -P "$(cat "$FIFOPID")"; then
if [ -n "$QLPATH" ] && stat "$1"; then
f="$(wslpath -w "$1")" && "$QLPATH" "$f" &
2021-04-26 15:21:52 +00:00
fi
else
2021-05-16 00:11:05 +00:00
start_preview "$1" "$QLPATH"
2021-04-26 15:21:52 +00:00
fi
2021-05-16 00:11:05 +00:00
} >/dev/null 2>&1
exists() {
2021-05-16 00:11:05 +00:00
type "$1" >/dev/null
}
fifo_pager() {
cmd="$1"
shift
# We use a FIFO to access $PAGER PID in jobs control
tmpfifopath="$TMPDIR/nnn-preview-tui-fifo.$$"
mkfifo "$tmpfifopath" || return
$PAGER < "$tmpfifopath" &
printf "%s" "$!" > "$PAGERPID"
(
exec > "$tmpfifopath"
if [ "$cmd" = "pager" ]; then
if exists bat; then
bat --terminal-width="$(tput cols)" --decorations=always --color=always \
2021-05-16 00:11:05 +00:00
--paging=never --style="$BAT_STYLE" "$@" &
else
$PAGER "$@" &
fi
else
"$cmd" "$@" &
fi
)
rm "$tmpfifopath"
2021-05-16 00:11:05 +00:00
} 2>/dev/null
2020-06-01 13:52:16 +00:00
# Binary file: show file info inside the pager
print_bin_info() {
printf -- "-------- \033[1;31mBinary file\033[0m --------\n"
if exists mediainfo; then
2021-05-16 00:11:05 +00:00
mediainfo "$1"
else
file -b "$1"
fi
2021-05-16 00:11:05 +00:00
} 2>/dev/null
handle_mime() {
case "$2" in
image/*) image_preview "$cols" "$lines" "$1" ;;
application/zip) fifo_pager unzip -l "$1" ;;
text/troff)
if exists man; then
fifo_pager man -Pcat -l "$1"
else
fifo_pager pager "$1"
fi ;;
*) handle_ext "$1" "$3" "$4" ;;
esac
}
handle_ext() {
case "$2" in
gz|bz2) fifo_pager tar -tvf "$1" ;;
*) if [ "$3" = "bin" ]; then
fifo_pager print_bin_info "$1"
else
fifo_pager pager "$1"
fi ;;
esac
}
preview_file () {
clear
2020-06-10 00:41:57 +00:00
# Trying to use pistol if it's available.
if [ "$USE_PISTOL" -ne 0 ] && exists pistol; then
fifo_pager pistol "$1"
return
fi
# Trying to use scope.sh if it's available.
if [ "$USE_SCOPE" -ne 0 ] && exists scope.sh; then
2021-05-16 00:11:05 +00:00
fifo_pager scope.sh "$1" "$cols" "$lines" "$(mktemp -d)" "True"
return
fi
# Use QuickLook if it's available.
if [ -n "$QUICKLOOK" ]; then
2021-05-16 00:11:05 +00:00
stat "$1" && f="$(wslpath -w "$1")" && "$QLPATH" "$f" &
return
fi
2021-05-16 00:11:05 +00:00
# Detecting the exact type of the file: the encoding, mime type, and extension in lowercase.
2021-03-21 14:03:43 +00:00
encoding="$(file -bL --mime-encoding -- "$1")"
mimetype="$(file -bL --mime-type -- "$1")"
ext="${1##*.}"
2021-05-16 00:11:05 +00:00
[ -n "$ext" ] && ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
lines=$(tput lines)
cols=$(tput cols)
# Otherwise, falling back to the defaults.
if [ -d "$1" ]; then
cd "$1" || return
if exists tree; then
2021-04-27 19:28:38 +00:00
fifo_pager tree --filelimit "$(find . -maxdepth 1 | wc -l)" -L 3 -C -F --dirsfirst --noreport
elif exists exa; then
2021-05-16 00:11:05 +00:00
exa -G --group-directories-first --colour=always
else
2021-05-16 00:11:05 +00:00
fifo_pager ls -F --group-directories-first --color=always
fi
2021-04-26 15:21:52 +00:00
elif [ "${encoding#*)}" = "binary" ]; then
handle_mime "$1" "$mimetype" "$ext" "bin"
else
handle_mime "$1" "$mimetype" "$ext"
fi
2021-05-16 00:11:05 +00:00
} 2>/dev/null
image_preview() {
if [ "$TERMINAL" = "kitty" ]; then
2021-05-16 00:11:05 +00:00
# Kitty terminal users can use the native image preview method
2021-05-14 01:30:27 +00:00
kitty +kitten icat --silent --place "$1"x"$2"@0x0 --transfer-mode=stream --stdin=no "$3" &
2021-04-23 22:36:05 +00:00
elif [ -n "$QLPATH" ] && stat "$3" >/dev/null 2>&1; then
2021-05-16 00:11:05 +00:00
f="$(wslpath -w "$3")" && "$QLPATH" "$f" && return
elif exists ueberzug; then
2021-05-16 00:11:05 +00:00
ueberzug_layer "$1" "$2" "$3" && return
elif exists catimg; then
catimg "$3" &
elif exists viu; then
viu -t "$3" &
else
2021-05-16 00:11:05 +00:00
fifo_pager print_bin_info "$3" && return
fi
2021-05-16 00:11:05 +00:00
printf "%s" "$!" > "$IMGPID"
} 2>/dev/null
ueberzug_layer() {
2021-05-16 00:11:05 +00:00
printf '{"action": "add", "identifier": "nnn_ueberzug", "x": 0, "y": 0, "width": "%d", "height": "%d", "scaler": "fit_contain", "path": "%s"}\n' "$1" "$2" "$3" > "$FIFO_UEBERZUG"
}
ueberzug_remove() {
printf '{"action": "remove", "identifier": "nnn_ueberzug"}\n' > "$FIFO_UEBERZUG"
}
ueberzug_refresh() {
clear
2021-05-16 00:11:05 +00:00
kill "$(cat "$IMGPID")"
kill "$(cat "$PAGERPID")"
pkill -P "$$"
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
2021-05-16 00:11:05 +00:00
cat "$CURSEL" > "$NNN_FIFO"
preview_fifo &
2021-05-01 16:12:13 +00:00
preview_file "$(cat "$CURSEL")"
2021-05-16 00:11:05 +00:00
wait "$!"
} 2>/dev/null
preview_fifo() {
2021-05-16 00:11:05 +00:00
printf "%s" "$$" > "$FIFOPID"
while read -r selection; do
2021-05-16 00:11:05 +00:00
kill "$(cat "$IMGPID")"
kill "$(cat "$PAGERPID")"
[ "$TERMINAL" != "kitty" ] && exists ueberzug && ueberzug_remove
preview_file "$selection"
printf "%s" "$selection" > "$CURSEL"
2021-05-16 00:11:05 +00:00
done < "$NNN_FIFO"
} 2>/dev/null
if [ "$PREVIEW_MODE" ]; then
if [ "$TERMINAL" != "kitty" ] && exists ueberzug; then
mkfifo "$FIFO_UEBERZUG"
2021-05-16 00:11:05 +00:00
trap 'ueberzug_refresh' WINCH
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
fi
2021-05-16 00:11:05 +00:00
printf "%s" "$PWD/$1" > "$CURSEL"
2021-05-01 16:12:13 +00:00
preview_file "$PWD/$1"
2021-05-16 00:11:05 +00:00
preview_fifo &
wait "$!"
pkill -P "$$"
2021-05-16 11:02:12 +00:00
rm "$PAGERPID" "$IMGPID" "$CURSEL" "$FIFO_UEBERZUG" "$FIFOPID" 2>/dev/null
else
if [ ! -r "$NNN_FIFO" ]; then
clear
2021-04-30 13:29:11 +00:00
printf "No FIFO available! (\$NNN_FIFO='%s')\nPlease read Usage in preview-tui." "$NNN_FIFO"
cfg=$(stty -g); stty raw -echo; head -c 1; stty "$cfg"
elif [ "$KITTY_WINDOW_ID" ] && [ -z "$KITTY_LISTEN_ON" ]; then
clear
2021-04-30 13:29:11 +00:00
printf "\$KITTY_LISTEN_ON not set!\nPlease read Usage in preview-tui."
cfg=$(stty -g); stty raw -echo; head -c 1; stty "$cfg"
else
2021-05-16 00:11:05 +00:00
toggle_preview "$1" &
fi
fi