This repository has been archived on 2024-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/common/nnn/plugins/preview

159 lines
4.1 KiB
Plaintext
Raw Normal View History

2020-11-20 21:40:10 -06:00
#!/usr/bin/env sh
2020-11-22 00:21:37 -06:00
SPLIT="$SPLIT"
TERMINAL="$TERMINAL"
2020-11-20 21:40:10 -06:00
USE_SCOPE="${USE_SCOPE:-0}"
2020-11-23 10:03:57 -06:00
IN_KITTY=0
[ -z "$SSH_CONNECTION" ] && [ -n "$KITTY_WINDOW_ID" ] && kitty @ ls --no-response >/dev/null 2>&1 && IN_KITTY=1
2020-11-20 21:40:10 -06:00
if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.'; then
2020-11-22 00:21:37 -06:00
TERMINAL=tmux
2020-11-23 10:03:57 -06:00
elif [ "$IN_KITTY" == 1 ]; then
2020-11-22 00:21:37 -06:00
TERMINAL=kitty
2020-11-20 21:40:10 -06:00
else
2020-11-22 00:21:37 -06:00
TERMINAL="${TERMINAL:-xterm}"
2020-11-20 21:40:10 -06:00
fi
if [ -z "$SPLIT" ] && [ $(($(tput lines) * 2)) -gt "$(tput cols)" ]; then
2020-11-22 00:21:37 -06:00
SPLIT='h'
2020-11-20 21:40:10 -06:00
elif [ "$SPLIT" != 'h' ]; then
2020-11-22 00:21:37 -06:00
SPLIT='v'
2020-11-20 21:40:10 -06:00
fi
exists() {
2020-11-22 00:21:37 -06:00
which "$1" >/dev/null 2>&1
2020-11-20 21:40:10 -06:00
}
fifo_pager() {
2020-11-22 00:21:37 -06:00
cmd="$1"; shift
tmpfifopath="${TMPDIR:-/tmp}/nnn-preview-tui-fifo.$$"
mkfifo "$tmpfifopath" || return
$PAGER < "$tmpfifopath" &
(
exec > "$tmpfifopath"
"$cmd" "$@" &
)
rm "$tmpfifopath"
2020-11-20 21:40:10 -06:00
}
print_bin_info() {
2020-11-22 00:21:37 -06:00
printf -- "-------- \033[1;31mBinary file\033[0m --------\n"
if exists mediainfo; then
mediainfo "$1" 2>/dev/null
else
file -b "$1"
# TODO: hexyl?
fi
2020-11-20 21:40:10 -06:00
}
preview_file () {
2020-11-22 00:21:37 -06:00
kill %- %+ 2>/dev/null && wait %- %+ 2>/dev/null
clear
# Trying to use pistol if it's available.
# if [ "$USE_PISTOL" -ne 0 ] && exists pistol; then
# fifo_pager pistol "$1"
# return
# fi
# Detecting the exact type of the file: the encoding, mime type, and
# extension in lowercase.
encoding="$(file -Lb --mime-encoding -- "$1")"
mimetype="$(file -Lb --mime-type -- "$1")"
ext="${1##*.}"
if [ -n "$ext" ]; then
ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
fi
lines=$(($(tput lines)-1))
cols=$(tput cols)
# Otherwise, falling back to the defaults.
if [ -d "$1" ]; then
cd "$1" || return
2020-12-17 10:12:21 -06:00
if exists exa; then
fifo_pager exa --tree --level 3 --colour=always 2>/dev/null
elif exists tree; then
2020-11-22 00:21:37 -06:00
fifo_pager tree -L 3 -F
else
fifo_pager ls --color=always
fi
elif [ "$encoding" = "binary" ]; then
if [ "${mimetype%%/*}" = "image" ] ; then
2020-11-23 10:03:57 -06:00
if [ "$IN_KITTY" = "1" ]; then
2020-11-22 00:21:37 -06:00
# Kitty terminal users can use the native image preview method.
kitty +kitten icat --silent --transfer-mode=stream --stdin=no "$1" &
elif exists catimg; then
catimg "$1"
elif exists viu; then
viu -t "$1"
else
fifo_pager print_bin_info "$1"
fi
elif [ "$mimetype" = "application/zip" ] ; then
fifo_pager unzip -l "$1"
elif [ "$ext" = "gz" ] || [ "$ext" = "bz2" ] ; then
fifo_pager tar -tvf "$1"
else
fifo_pager print_bin_info "$1"
fi
elif [ "$mimetype" = "text/troff" ] ; then
fifo_pager man -Pcat -l "$1"
else
if exists bat; then
fifo_pager bat --terminal-width="$cols" --paging=never --decorations=always --color=always \
"$1" 2>/dev/null
else
$PAGER "$1" &
fi
fi
2020-11-20 21:40:10 -06:00
}
if [ "$PREVIEW_MODE" ] ; then
2020-11-22 00:21:37 -06:00
if [ ! -r "$NNN_FIFO" ] ; then
echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
read -r
exit 1
fi
preview_file "$1"
# use cat instead of 'exec <' to avoid issues with dash shell
# shellcheck disable=SC2002
cat "$NNN_FIFO" | \
while read -r selection ; do
preview_file "$selection"
done
# TODO: detect if file is opened and close preview until back in nnn
# 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
exit 0
2020-11-20 21:40:10 -06:00
fi
if [ "$TERMINAL" = "tmux" ]; then
2020-11-22 00:21:37 -06:00
# tmux splits are inverted
if [ "$SPLIT" = "v" ]; then SPLIT="h"; else SPLIT="v"; fi
tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1" -d"$SPLIT" "$0" "$1"
2020-11-20 21:40:10 -06:00
elif [ "$TERMINAL" = "kitty" ]; then
2020-11-22 00:21:37 -06:00
# Setting the layout for the new window. It will be restored after the
# script ends.
kitty @ goto-layout splits >/dev/null
# 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 "PREVIEW_MODE=1" --env "PAGER=$PAGER" \
--env "USE_SCOPE=$USE_SCOPE" --env "SPLIT=$SPLIT" \
--env "USE_PISTOL=$USE_PISTOL" \
--location "${SPLIT}split" "$0" "$1" >/dev/null
2020-11-20 21:40:10 -06:00
else
2020-11-22 00:21:37 -06:00
PREVIEW_MODE=1 $TERMINAL -e "$0" "$1" &
2020-11-20 21:40:10 -06:00
fi