2020-05-05 23:08:10 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
2020-06-09 01:09:35 +00:00
|
|
|
# Description: Terminal based file previewer
|
2020-05-05 23:08:10 +00:00
|
|
|
#
|
2020-06-09 01:09:35 +00:00
|
|
|
# Note: This plugin needs a "NNN_FIFO" to work. See man.
|
2021-03-20 00:58:46 +00:00
|
|
|
# For a more extended version of this script with additional optional dependencies, see preview-tui-ext.
|
2020-05-05 23:08:10 +00:00
|
|
|
#
|
2020-06-09 01:09:35 +00:00
|
|
|
# Dependencies:
|
|
|
|
# - Supports 3 independent methods to preview with:
|
|
|
|
# - tmux (>=3.0), or
|
|
|
|
# - kitty with allow_remote_control on, or
|
|
|
|
# - $TERMINAL set to a terminal (it's xterm by default).
|
|
|
|
# - less or $PAGER
|
|
|
|
# - tree or exa or ls
|
|
|
|
# - mediainfo or file
|
2020-06-20 13:39:32 +00:00
|
|
|
# - mktemp
|
2020-06-09 01:09:35 +00:00
|
|
|
# - unzip
|
|
|
|
# - tar
|
|
|
|
# - man
|
|
|
|
# - optional: bat for code syntax highlighting
|
2021-03-14 06:53:03 +00:00
|
|
|
# - optional: ueberzug, kitty terminal, viu or catimg for images.
|
2020-06-09 01:09:35 +00:00
|
|
|
# - 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-05 23:08:10 +00:00
|
|
|
#
|
2020-05-06 05:12:29 +00:00
|
|
|
# Usage:
|
2020-06-09 01:09:35 +00:00
|
|
|
# You need to set a NNN_FIFO path and a key for the plugin with NNN_PLUG,
|
2020-05-05 23:08:10 +00:00
|
|
|
# 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-07-14 23:19:13 +00:00
|
|
|
# single common preview window. If you provide different FIFO path (e.g.
|
2020-06-02 08:56:50 +00:00
|
|
|
# with -a), they will be independent.
|
2020-05-05 23:08:10 +00:00
|
|
|
#
|
2020-06-09 01:09:35 +00:00
|
|
|
# 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
|
2020-06-09 14:57:28 +00:00
|
|
|
# 'h'orizontal split or a 'v'ertical split (as in, the line that splits the
|
|
|
|
# windows will be horizontal or vertical).
|
2020-06-09 01:09:35 +00:00
|
|
|
#
|
|
|
|
# Kitty users need `allow_remote_control` set to `yes`. 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-06-01 08:42:34 +00:00
|
|
|
#
|
2020-05-06 05:12:29 +00:00
|
|
|
# Shell: POSIX compliant
|
2021-03-20 00:58:46 +00:00
|
|
|
# Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste, Mario Ortiz Manero, Luuk van Baal
|
2020-05-05 23:08:10 +00:00
|
|
|
|
2020-06-09 14:57:28 +00:00
|
|
|
SPLIT="$SPLIT" # you can set a permanent split here
|
|
|
|
TERMINAL="$TERMINAL" # same goes for the terminal
|
2020-06-09 01:09:35 +00:00
|
|
|
USE_SCOPE="${USE_SCOPE:-0}"
|
2020-06-10 00:41:57 +00:00
|
|
|
USE_PISTOL="${USE_PISTOL:-0}"
|
2021-03-20 00:58:46 +00:00
|
|
|
PAGER="${PAGER:-less -P?n -R}"
|
2021-03-14 06:53:03 +00:00
|
|
|
TMPDIR="${TMPDIR:-/tmp}"
|
2021-03-20 00:58:46 +00:00
|
|
|
NUMPREVIEWTUI="$(($(find "$TMPDIR" -maxdepth 1 -name 'nnn-preview-tui-pagerpid*' 2>/dev/null | wc -l) + 1))"
|
|
|
|
PAGERPID="$TMPDIR/nnn-preview-tui-pagerpid.$NUMPREVIEWTUI"
|
|
|
|
GIFPID="$TMPDIR/nnn-preview-tui-gifpid.$NUMPREVIEWTUI"
|
2021-03-21 01:41:14 +00:00
|
|
|
CURSEL="$TMPDIR/nnn-preview-tui-selection.$NUMPREVIEWTUI"
|
2021-03-20 00:58:46 +00:00
|
|
|
FIFO_UEBERZUG="$TMPDIR/nnn-preview-tui-ueberzug-fifo.$NUMPREVIEWTUI"
|
|
|
|
|
2020-08-15 02:03:16 +00:00
|
|
|
[ "$PAGER" = "most" ] && PAGER="less -R"
|
2020-06-02 08:56:50 +00:00
|
|
|
|
2020-06-09 01:09:35 +00:00
|
|
|
if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.'; then
|
|
|
|
TERMINAL=tmux
|
|
|
|
elif [ -n "$KITTY_WINDOW_ID" ] && kitty @ ls >/dev/null 2>&1; then
|
|
|
|
TERMINAL=kitty
|
|
|
|
else
|
|
|
|
TERMINAL="${TERMINAL:-xterm}"
|
|
|
|
fi
|
|
|
|
|
2020-06-09 14:57:28 +00:00
|
|
|
if [ -z "$SPLIT" ] && [ $(($(tput lines) * 2)) -gt "$(tput cols)" ]; then
|
2020-06-09 01:09:35 +00:00
|
|
|
SPLIT='h'
|
2020-06-09 14:57:28 +00:00
|
|
|
elif [ "$SPLIT" != 'h' ]; then
|
|
|
|
SPLIT='v'
|
2020-06-09 01:09:35 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
exists() {
|
2020-08-29 22:56:55 +00:00
|
|
|
which "$1" >/dev/null 2>&1
|
2020-06-09 01:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 08:56:50 +00:00
|
|
|
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-06-09 01:09:35 +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
|
|
|
|
mediainfo "$1" 2>/dev/null
|
|
|
|
else
|
|
|
|
file -b "$1"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-05-06 17:25:14 +00:00
|
|
|
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
|
|
|
|
|
2020-06-09 01:09:35 +00:00
|
|
|
# Trying to use scope.sh if it's available.
|
|
|
|
if [ "$USE_SCOPE" -ne 0 ] && exists scope.sh; then
|
|
|
|
fifo_pager scope.sh "$1" "$cols" "$lines" "$(mktemp -d)" \
|
|
|
|
"True" 2>/dev/null
|
|
|
|
return
|
|
|
|
fi
|
2020-05-06 17:25:14 +00:00
|
|
|
|
2020-06-10 07:50:38 +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")"
|
2020-06-10 07:50:38 +00:00
|
|
|
ext="${1##*.}"
|
|
|
|
if [ -n "$ext" ]; then
|
|
|
|
ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
|
|
|
|
fi
|
|
|
|
lines=$(($(tput lines)-1))
|
|
|
|
cols=$(tput cols)
|
|
|
|
|
2020-06-09 01:09:35 +00:00
|
|
|
# Otherwise, falling back to the defaults.
|
2020-05-27 23:34:53 +00:00
|
|
|
if [ -d "$1" ]; then
|
|
|
|
cd "$1" || return
|
2020-06-09 01:09:35 +00:00
|
|
|
if exists tree; then
|
2021-03-20 00:58:46 +00:00
|
|
|
fifo_pager tree -L 1 --dirsfirst -F -C
|
2020-06-09 01:09:35 +00:00
|
|
|
elif exists exa; then
|
|
|
|
fifo_pager exa -G --colour=always 2>/dev/null
|
|
|
|
else
|
|
|
|
fifo_pager ls --color=always
|
|
|
|
fi
|
|
|
|
elif [ "$encoding" = "binary" ]; then
|
2021-03-14 06:53:03 +00:00
|
|
|
if [ "${mimetype%%/*}" = "image" ]; then
|
|
|
|
image_preview "$cols" "$lines" "$1"
|
|
|
|
elif [ "$mimetype" = "application/zip" ]; then
|
2020-06-09 01:09:35 +00:00
|
|
|
fifo_pager unzip -l "$1"
|
2021-03-14 06:53:03 +00:00
|
|
|
elif [ "$ext" = "gz" ] || [ "$ext" = "bz2" ]; then
|
2020-06-09 01:09:35 +00:00
|
|
|
fifo_pager tar -tvf "$1"
|
|
|
|
else
|
|
|
|
fifo_pager print_bin_info "$1"
|
|
|
|
fi
|
2021-03-14 06:53:03 +00:00
|
|
|
elif [ "$mimetype" = "text/troff" ]; then
|
2020-06-02 08:56:50 +00:00
|
|
|
fifo_pager man -Pcat -l "$1"
|
2020-05-27 23:34:53 +00:00
|
|
|
else
|
2020-06-09 01:09:35 +00:00
|
|
|
if exists bat; then
|
2020-07-08 15:23:35 +00:00
|
|
|
fifo_pager bat --terminal-width="$cols" --paging=never --decorations=always --color=always \
|
2020-06-09 01:09:35 +00:00
|
|
|
"$1" 2>/dev/null
|
|
|
|
else
|
|
|
|
$PAGER "$1" &
|
|
|
|
fi
|
2020-05-27 23:34:53 +00:00
|
|
|
fi
|
2020-05-06 17:25:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 06:53:03 +00:00
|
|
|
image_preview() {
|
|
|
|
if [ "$TERMINAL" = "kitty" ]; then
|
|
|
|
# Kitty terminal users can use the native image preview method.
|
|
|
|
kitty +kitten icat --silent --place "$1"x"$2"@0x0 --transfer-mode=stream --stdin=no \
|
|
|
|
"$3"
|
|
|
|
elif exists ueberzug; then
|
|
|
|
ueberzug_layer "$1" "$2" "$3"
|
|
|
|
elif exists catimg; then
|
|
|
|
catimg "$3" &
|
2021-03-20 00:58:46 +00:00
|
|
|
echo "$!" > "$GIFPID"
|
2021-03-14 06:53:03 +00:00
|
|
|
elif exists viu; then
|
|
|
|
viu -t "$3" &
|
2021-03-20 00:58:46 +00:00
|
|
|
echo "$!" > "$GIFPID"
|
2021-03-14 06:53:03 +00:00
|
|
|
else
|
2021-03-21 01:41:14 +00:00
|
|
|
fifo_pager print_bin_info "$3"
|
2020-05-06 04:24:31 +00:00
|
|
|
fi
|
2021-03-14 06:53:03 +00:00
|
|
|
}
|
2020-05-06 04:24:31 +00:00
|
|
|
|
2021-03-14 06:53:03 +00:00
|
|
|
ueberzug_layer() {
|
|
|
|
printf '{"action": "add", "identifier": "nnn_ueberzug", "x": 0, "y": 0, "width": "%s", "height": "%s", "path": "%s"}\n' "$1" "$2" "$3" > "$FIFO_UEBERZUG"
|
|
|
|
}
|
|
|
|
|
|
|
|
ueberzug_remove() {
|
|
|
|
printf '{"action": "remove", "identifier": "nnn_ueberzug"}\n' > "$FIFO_UEBERZUG"
|
|
|
|
}
|
2020-05-06 17:25:14 +00:00
|
|
|
|
2021-03-14 06:53:03 +00:00
|
|
|
ueberzug_refresh() {
|
|
|
|
clear
|
2021-03-21 01:41:14 +00:00
|
|
|
kill "$(cat "$GIFPID" 2>/dev/null)" >/dev/null 2>&1
|
2021-03-20 00:58:46 +00:00
|
|
|
pkill -P "$$" >/dev/null 2>&1
|
|
|
|
pkill -f -n preview-tui-ext >/dev/null 2>&1
|
2021-03-14 06:53:03 +00:00
|
|
|
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
|
2021-03-21 01:41:14 +00:00
|
|
|
preview_file "$(cat "$CURSEL")"
|
|
|
|
echo > "$NNN_FIFO"
|
2021-03-14 06:53:03 +00:00
|
|
|
preview_fifo &
|
|
|
|
wait
|
|
|
|
}
|
2021-03-14 14:28:50 +00:00
|
|
|
if [ "$TERMINAL" != "kitty" ] && [ "$PREVIEW_MODE" ] && exists ueberzug; then
|
|
|
|
trap 'ueberzug_refresh' WINCH
|
2021-03-21 01:41:14 +00:00
|
|
|
trap 'rm "$FIFO_UEBERZUG" "$PAGERPID" "$GIFPID" "$CURSEL"' INT HUP EXIT
|
2021-03-14 14:28:50 +00:00
|
|
|
fi
|
2021-03-14 06:53:03 +00:00
|
|
|
|
|
|
|
preview_fifo() {
|
2020-06-09 01:09:35 +00:00
|
|
|
# use cat instead of 'exec <' to avoid issues with dash shell
|
|
|
|
# shellcheck disable=SC2002
|
|
|
|
cat "$NNN_FIFO" |\
|
2021-03-14 06:53:03 +00:00
|
|
|
while read -r selection; do
|
2021-03-20 00:58:46 +00:00
|
|
|
kill "$(cat "$GIFPID" 2>/dev/null)" >/dev/null 2>&1
|
|
|
|
kill "$(cat "$PAGERPID" 2>/dev/null)" >/dev/null 2>&1
|
2021-03-14 06:53:03 +00:00
|
|
|
[ "$TERMINAL" != "kitty" ] && exists ueberzug && ueberzug_remove
|
2020-05-06 17:25:14 +00:00
|
|
|
preview_file "$selection"
|
2021-03-21 01:41:14 +00:00
|
|
|
echo "$selection" > "$CURSEL"
|
2020-05-05 23:08:10 +00:00
|
|
|
done
|
2021-03-14 06:53:03 +00:00
|
|
|
[ "$TERMINAL" != "kitty" ] && exists ueberzug && rm "$FIFO_UEBERZUG"
|
2021-03-21 01:41:14 +00:00
|
|
|
rm "$PAGERPID" "$GIFPID" "$CURSEL" >/dev/null 2>&1
|
|
|
|
pkill -P "$$" >/dev/null 2>&1
|
2021-03-14 06:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if [ "$PREVIEW_MODE" ]; then
|
|
|
|
if [ ! -r "$NNN_FIFO" ]; then
|
|
|
|
echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
|
|
|
|
read -r
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$TERMINAL" != "kitty" ] && exists ueberzug; then
|
|
|
|
FIFO_UEBERZUG="$TMPDIR/nnn-ueberzug-fifo.$$"
|
|
|
|
mkfifo "$FIFO_UEBERZUG"
|
|
|
|
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
|
|
|
|
fi
|
|
|
|
|
|
|
|
preview_file "$1"
|
|
|
|
preview_fifo &
|
|
|
|
wait
|
2020-06-10 00:26:15 +00:00
|
|
|
|
|
|
|
# Restoring the previous layout for kitty users. This will only work for
|
|
|
|
# kitty >= 0.18.0.
|
|
|
|
if [ "$TERMINAL" = "kitty" ]; then
|
|
|
|
kitty @ last-used-layout --no-response >/dev/null 2>&1
|
|
|
|
fi
|
|
|
|
|
2020-05-05 23:08:10 +00:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2020-06-09 01:09:35 +00:00
|
|
|
if [ "$TERMINAL" = "tmux" ]; then
|
|
|
|
# tmux splits are inverted
|
|
|
|
if [ "$SPLIT" = "v" ]; then SPLIT="h"; else SPLIT="v"; 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-06-09 01:09:35 +00:00
|
|
|
elif [ "$TERMINAL" = "kitty" ]; then
|
2020-06-10 00:26:15 +00:00
|
|
|
# Setting the layout for the new window. It will be restored after the
|
|
|
|
# script ends.
|
2020-06-09 01:09:35 +00:00
|
|
|
kitty @ goto-layout splits >/dev/null
|
|
|
|
|
|
|
|
# Trying to use kitty's integrated window management as the split window.
|
2020-06-09 23:58:35 +00:00
|
|
|
# All environmental variables that will be used in the new window must
|
|
|
|
# be explicitly passed.
|
2020-06-09 01:09:35 +00:00
|
|
|
kitty @ launch --no-response --title "nnn preview" --keep-focus \
|
2020-06-09 23:58:35 +00:00
|
|
|
--cwd "$PWD" --env "PATH=$PATH" --env "NNN_FIFO=$NNN_FIFO" \
|
|
|
|
--env "PREVIEW_MODE=1" --env "PAGER=$PAGER" \
|
|
|
|
--env "USE_SCOPE=$USE_SCOPE" --env "SPLIT=$SPLIT" \
|
2020-06-10 00:41:57 +00:00
|
|
|
--env "USE_PISTOL=$USE_PISTOL" \
|
2020-06-09 01:09:35 +00:00
|
|
|
--location "${SPLIT}split" "$0" "$1" >/dev/null
|
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
|