2021-03-13 11:22:51 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
# Description: Terminal based file previewer
|
|
|
|
#
|
|
|
|
# Note: This plugin needs a "NNN_FIFO" to work. See man.
|
|
|
|
#
|
|
|
|
# Dependencies:
|
2021-04-22 12:15:42 +00:00
|
|
|
# - Supports 4 independent methods to preview with:
|
2021-03-13 11:22:51 +00:00
|
|
|
# - tmux (>=3.0), or
|
2021-04-27 06:26:52 +00:00
|
|
|
# - 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)
|
2021-03-13 11:22:51 +00:00
|
|
|
# - $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
|
2021-04-21 14:39:15 +00:00
|
|
|
# - optional: bsdtar or atool for additional archive preview
|
2021-03-13 11:22:51 +00:00
|
|
|
# - optional: bat for code syntax highlighting
|
2021-03-30 15:25:56 +00:00
|
|
|
# - optional: ueberzug, kitty terminal, viu or catimg for images
|
2021-03-14 06:53:03 +00:00
|
|
|
# - optional: convert(ImageMagick) for playing gif preview
|
2021-03-30 15:25:56 +00:00
|
|
|
# - optional: ffmpegthumbnailer for video thumbnails (https://github.com/dirkvdb/ffmpegthumbnailer)
|
2021-03-14 06:53:03 +00:00
|
|
|
# - optional: ffmpeg for audio thumbnails
|
|
|
|
# - optional: libreoffce for opendocument/officedocument preview
|
|
|
|
# - optional: pdftoppm(poppler) for pdf thumbnails
|
|
|
|
# - optional: gnome-epub-thumbnailer for epub thumbnails (https://gitlab.gnome.org/GNOME/gnome-epub-thumbnailer)
|
|
|
|
# - optional: fontpreview for font preview (https://github.com/sdushantha/fontpreview)
|
2021-03-20 18:31:46 +00:00
|
|
|
# - optional: glow or lowdown for markdown
|
|
|
|
# - optional: w3m or lynx or elinks for html
|
2021-03-30 15:25:56 +00:00
|
|
|
# - optional: set/export ICONLOOKUP as 1 to enable file icons in front of directory previews with .iconlookup
|
|
|
|
# Icons and colors are configureable in .iconlookup
|
2021-03-13 11:22:51 +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
|
|
|
|
# - optional: pistol file viewer (https://github.com/doronbehar/pistol).
|
|
|
|
# To use:
|
|
|
|
# 1. install pistol
|
|
|
|
# 2. set/export $USE_PISTOL as 1
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
2021-03-24 01:13:18 +00:00
|
|
|
# Then in `nnn`, launch the `preview-tui-ext` plugin.
|
2021-03-13 11:22:51 +00:00
|
|
|
#
|
|
|
|
# If you provide the same NNN_FIFO to all nnn instances, there will be a
|
|
|
|
# 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.
|
2021-03-13 11:22:51 +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
|
2021-04-27 06:26:52 +00:00
|
|
|
|
2021-03-13 11:22:51 +00:00
|
|
|
SPLIT="$SPLIT" # you can set a permanent split here
|
|
|
|
TERMINAL="$TERMINAL" # same goes for the terminal
|
|
|
|
USE_SCOPE="${USE_SCOPE:-0}"
|
|
|
|
USE_PISTOL="${USE_PISTOL:-0}"
|
2021-03-30 15:25:56 +00:00
|
|
|
ICONLOOKUP="${ICONLOOKUP:-0}"
|
2021-03-13 11:22:51 +00:00
|
|
|
PAGER="${PAGER:-less -P?n -R}"
|
|
|
|
TMPDIR="${TMPDIR:-/tmp}"
|
2021-05-01 22:54:42 +00:00
|
|
|
# Consider setting NNN_PREVIEWDIR to $XDG_CACHE_HOME/nnn/previews if you want to keep previews on disk between reboots
|
|
|
|
NNN_PREVIEWDIR="${NNN_PREVIEWDIR:-$TMPDIR/nnn/previews}"
|
2021-03-24 01:13:18 +00:00
|
|
|
NNN_PREVIEWWIDTH="${NNN_PREVIEWWIDTH:-1920}"
|
|
|
|
NNN_PREVIEWHEIGHT="${NNN_PREVIEWHEIGHT:-1080}"
|
2021-03-13 11:22:51 +00:00
|
|
|
|
2021-04-26 15:21:52 +00:00
|
|
|
startpreview() {
|
|
|
|
NUMPREVIEWTUI="$(($(find "$TMPDIR" -maxdepth 1 -name 'nnn-preview-tui-pagerpid*' 2>/dev/null | wc -l) + 1))"
|
|
|
|
PAGERPID="$TMPDIR/nnn-preview-tui-pagerpid.$NUMPREVIEWTUI"
|
2021-05-14 01:30:27 +00:00
|
|
|
IMGPID="$TMPDIR/nnn-preview-tui-gifpid.$NUMPREVIEWTUI"
|
2021-04-26 15:21:52 +00:00
|
|
|
CURSEL="$TMPDIR/nnn-preview-tui-selection.$NUMPREVIEWTUI"
|
|
|
|
FIFO_UEBERZUG="$TMPDIR/nnn-preview-tui-ueberzug-fifo.$NUMPREVIEWTUI"
|
2021-03-13 11:22:51 +00:00
|
|
|
|
2021-04-26 15:21:52 +00:00
|
|
|
[ "$PAGER" = "most" ] && PAGER="less -R"
|
2021-03-13 11:22:51 +00:00
|
|
|
|
2021-04-26 15:21:52 +00:00
|
|
|
if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.'; then
|
|
|
|
TERMINAL=tmux
|
2021-04-27 06:26:52 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
if [ "$TERMINAL" = "tmux" ]; then
|
|
|
|
# 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" -e "PAGER=$PAGER" \
|
|
|
|
-e "USE_SCOPE=$USE_SCOPE" -e "SPLIT=$SPLIT" -e "USE_PISTOL=$USE_PISTOL" \
|
2021-05-14 14:29:43 +00:00
|
|
|
-e "BAT_STYLE=${BAT_STYLE:-numbers}" -e "PAGERPID=$PAGERPID" \
|
|
|
|
-e "IMGPID=$IMGPID" -e "CURSEL=$CURSEL" -e "TMPDIR=$TMPDIR" \
|
2021-04-26 15:21:52 +00:00
|
|
|
-e "ICONLOOKUP=$ICONLOOKUP" -e "NNN_PREVIEWDIR=$NNN_PREVIEWDIR" \
|
|
|
|
-e "NNN_PREVIEWWIDTH=$NNN_PREVIEWWIDTH" -e "NNN_PREVIEWHEIGHT=$NNN_PREVIEWHEIGHT" \
|
|
|
|
-e "FIFO_UEBERZUG=$FIFO_UEBERZUG" -e "QLPATH=$2" -d"$SPLIT" "$0" "$1"
|
|
|
|
elif [ "$TERMINAL" = "kitty" ]; then
|
|
|
|
# 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 "TMPDIR=$TMPDIR" \
|
|
|
|
--env "USE_SCOPE=$USE_SCOPE" --env "SPLIT=$SPLIT" --env "TERMINAL=$TERMINAL"\
|
2021-05-14 14:29:43 +00:00
|
|
|
--env "USE_PISTOL=$USE_PISTOL" --env "BAT_STYLE=${BAT_STYLE:-numbers}" \
|
2021-05-14 17:16:30 +00:00
|
|
|
--env "PAGERPID=$PAGERPID" --env "IMGPID=$IMGPID" --env "FIFO_UEBERZUG=$FIFO_UEBERZUG" \
|
2021-04-26 15:21:52 +00:00
|
|
|
--env "ICONLOOKUP=$ICONLOOKUP" --env "NNN_PREVIEWDIR=$NNN_PREVIEWDIR" \
|
|
|
|
--env "NNN_PREVIEWWIDTH=$NNN_PREVIEWWIDTH" --env "NNN_PREVIEWHEIGHT=$NNN_PREVIEWHEIGHT" \
|
|
|
|
--env "CURSEL=$CURSEL" --location "${SPLIT}split" "$0" "$1" >/dev/null
|
|
|
|
elif [ -n "$2" ]; then
|
|
|
|
QUICKLOOK=1 QLPATH="$2" PREVIEW_MODE=1 "$0" "$1" >/dev/null &
|
|
|
|
else
|
2021-05-14 01:30:27 +00:00
|
|
|
PAGERPID="$PAGERPID" IMGPID="$IMGPID" CURSEL="$CURSEL" PREVIEW_MODE=1 \
|
2021-04-26 15:21:52 +00:00
|
|
|
FIFO_UEBERZUG="$FIFO_UEBERZUG" $TERMINAL -e "$0" "$1" 2>/dev/null &
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
togglepreview() {
|
|
|
|
if exists QuickLook.exe; then
|
|
|
|
QLPATH="QuickLook.exe"
|
|
|
|
elif exists Bridge.exe; then
|
|
|
|
QLPATH="Bridge.exe"
|
|
|
|
fi
|
|
|
|
if pkill -fx "cat $NNN_FIFO" >/dev/null; then
|
|
|
|
if [ -n "$QLPATH" ] && stat "$1" >/dev/null 2>&1; then
|
|
|
|
f="$(wslpath -w "$1" 2>&1)" && "$QLPATH" "$f" &
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
startpreview "$1" "$QLPATH"
|
|
|
|
fi
|
|
|
|
}
|
2021-03-13 11:22:51 +00:00
|
|
|
|
|
|
|
exists() {
|
2021-05-14 12:03:28 +00:00
|
|
|
type "$1" >/dev/null 2>&1
|
2021-03-13 11:22:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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" &
|
2021-04-27 06:26:52 +00:00
|
|
|
printf "%s" "$!" > "$PAGERPID"
|
2021-03-13 11:22:51 +00:00
|
|
|
|
|
|
|
(
|
|
|
|
exec > "$tmpfifopath"
|
2021-05-01 11:44:19 +00:00
|
|
|
if [ "$cmd" = "pager" ]; then
|
|
|
|
if exists bat; then
|
2021-05-14 14:29:43 +00:00
|
|
|
bat --terminal-width="$(tput cols)" --decorations=always --color=always \
|
|
|
|
--paging=never --style="${BAT_STYLE:-numbers}" "$@" 2>/dev/null &
|
2021-05-01 11:44:19 +00:00
|
|
|
else
|
|
|
|
$PAGER "$@" &
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
"$cmd" "$@" &
|
|
|
|
fi
|
2021-03-13 11:22:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
rm "$tmpfifopath"
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:16:30 +00:00
|
|
|
handle_mime() {
|
|
|
|
case "$2" in
|
|
|
|
image/jpg) image_preview "$cols" "$lines" "$1" ;;
|
|
|
|
image/gif) generate_preview "$cols" "$lines" "$1" "gif" ;;
|
|
|
|
image/*) generate_preview "$cols" "$lines" "$1" "image" ;;
|
|
|
|
video/*) generate_preview "$cols" "$lines" "$1" "video" ;;
|
|
|
|
audio/*) generate_preview "$cols" "$lines" "$1" "audio" ;;
|
|
|
|
application/font*|application/*opentype) generate_preview "$cols" "$lines" "$1" "font" ;;
|
|
|
|
*/*office*|*/*document*) generate_preview "$cols" "$lines" "$1" "office" ;;
|
|
|
|
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
|
|
|
|
epub) generate_preview "$cols" "$lines" "$1" "epub" ;;
|
|
|
|
pdf) generate_preview "$cols" "$lines" "$1" "pdf" ;;
|
|
|
|
gz|bz2) fifo_pager tar -tvf "$1" ;;
|
|
|
|
md) if exists glow; then
|
|
|
|
fifo_pager glow -s dark "$1"
|
|
|
|
elif exists lowdown; then
|
|
|
|
fifo_pager lowdown -Tterm "$1"
|
|
|
|
else
|
|
|
|
fifo_pager pager "$1"
|
|
|
|
fi ;;
|
|
|
|
htm|html|xhtml)
|
|
|
|
if exists w3m; then
|
|
|
|
fifo_pager w3m "$1"
|
|
|
|
elif exists lynx; then
|
|
|
|
fifo_pager lynx "$1"
|
|
|
|
elif exists elinks; then
|
|
|
|
fifo_pager elinks "$1"
|
|
|
|
else
|
|
|
|
fifo_pager pager "$1"
|
|
|
|
fi ;;
|
|
|
|
7z|a|ace|alz|arc|arj|bz|cab|cpio|deb|jar|lha|lz|lzh|lzma|lzo\
|
|
|
|
|rar|rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z)
|
|
|
|
if exists atool; then
|
|
|
|
fifo_pager atool -l "$1"
|
|
|
|
elif exists bsdtar; then
|
|
|
|
fifo_pager bsdtar -tvf "$1"
|
|
|
|
fi ;;
|
|
|
|
*) if [ "$3" = "bin" ]; then
|
|
|
|
fifo_pager print_bin_info "$1"
|
|
|
|
else
|
|
|
|
fifo_pager pager "$1"
|
|
|
|
fi ;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2021-03-24 01:13:18 +00:00
|
|
|
preview_file() {
|
2021-03-13 11:22:51 +00:00
|
|
|
clear
|
|
|
|
# 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
|
|
|
|
fifo_pager scope.sh "$1" "$cols" "$lines" "$(mktemp -d)" \
|
|
|
|
"True" 2>/dev/null
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2021-05-01 11:44:19 +00:00
|
|
|
# Use QuickLook if it's available.
|
|
|
|
if [ -n "$QUICKLOOK" ]; then
|
|
|
|
stat "$1" >/dev/null 2>&1 && f="$(wslpath -w "$1" 2>&1)" && "$QLPATH" "$f" &
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2021-03-13 11:22:51 +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")"
|
2021-03-13 11:22:51 +00:00
|
|
|
ext="${1##*.}"
|
2021-05-14 17:16:30 +00:00
|
|
|
[ -n "$ext" ] && ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
|
2021-03-22 00:54:29 +00:00
|
|
|
lines=$(tput lines)
|
2021-03-13 11:22:51 +00:00
|
|
|
cols=$(tput cols)
|
|
|
|
|
|
|
|
# Otherwise, falling back to the defaults.
|
2021-05-01 11:44:19 +00:00
|
|
|
if [ -d "$1" ]; then
|
2021-03-13 11:22:51 +00:00
|
|
|
cd "$1" || return
|
2021-04-21 12:44:38 +00:00
|
|
|
if [ "$ICONLOOKUP" -ne 0 ] && [ -f "$(dirname "$0")"/.iconlookup ]; then
|
2021-04-26 15:21:52 +00:00
|
|
|
[ "$SPLIT" = h ] && [ "$TERMINAL" != "kitty" ] && BSTR="\n"
|
|
|
|
[ "$SPLIT" = v ] && [ "$TERMINAL" = "kitty" ] && BSTR="\n"
|
2021-04-21 12:44:38 +00:00
|
|
|
# shellcheck disable=SC2012
|
|
|
|
ls -F --group-directories-first | head -n "$((lines - 3))" | "$(dirname "$0")"/.iconlookup -l "$cols" -B "$BSTR" -b " "
|
|
|
|
elif 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
|
2021-03-13 11:22:51 +00:00
|
|
|
elif exists exa; then
|
2021-04-21 12:44:38 +00:00
|
|
|
exa -G --group-directories-first --colour=always 2>/dev/null
|
2021-03-13 11:22:51 +00:00
|
|
|
else
|
2021-04-27 19:37:18 +00:00
|
|
|
fifo_pager ls -F --group-directories-first --color=always
|
2021-03-13 11:22:51 +00:00
|
|
|
fi
|
2021-04-09 10:13:32 +00:00
|
|
|
elif [ "${encoding#*)}" = "binary" ]; then
|
2021-05-14 17:16:30 +00:00
|
|
|
handle_mime "$1" "$mimetype" "$ext" "bin"
|
2021-05-01 11:44:19 +00:00
|
|
|
else
|
2021-05-14 17:16:30 +00:00
|
|
|
handle_mime "$1" "$mimetype" "$ext"
|
2021-03-13 11:22:51 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
generate_preview() {
|
2021-04-23 22:36:05 +00:00
|
|
|
if [ -n "$QLPATH" ] && stat "$3" >/dev/null 2>&1; then
|
|
|
|
f="$(wslpath -w "$3" 2>&1)" && "$QLPATH" "$f" &
|
2021-04-22 12:15:42 +00:00
|
|
|
elif [ ! -f "$NNN_PREVIEWDIR/$3.jpg" ] || [ -n "$(find -L "$3" -newer "$NNN_PREVIEWDIR/$3.jpg")" ]; then
|
2021-03-24 01:13:18 +00:00
|
|
|
mkdir -p "$NNN_PREVIEWDIR/${3%/*}"
|
2021-03-13 11:22:51 +00:00
|
|
|
case $4 in
|
2021-03-24 01:13:18 +00:00
|
|
|
audio) ffmpeg -i "$3" -filter_complex "scale=iw*min(1\,min($NNN_PREVIEWWIDTH/iw\,ih)):-1" "$NNN_PREVIEWDIR/$3.jpg" -y >/dev/null 2>&1 ;;
|
|
|
|
epub) gnome-epub-thumbnailer "$3" "$NNN_PREVIEWDIR/$3.jpg" >/dev/null 2>&1 ;;
|
|
|
|
font) fontpreview -i "$3" -o "$NNN_PREVIEWDIR/$3.jpg" >/dev/null 2>&1 ;;
|
2021-05-01 11:44:19 +00:00
|
|
|
gif) if exists ueberzug && exists convert && [ "$TERMINAL" != "kitty" ]; then
|
2021-03-24 01:13:18 +00:00
|
|
|
if [ ! -d "$NNN_PREVIEWDIR/$3" ]; then
|
|
|
|
mkdir -p "$NNN_PREVIEWDIR/$3"
|
|
|
|
convert -coalesce -resize "$NNN_PREVIEWWIDTH"x"$NNN_PREVIEWHEIGHT"\> "$3" "$NNN_PREVIEWDIR/$3/${3##*/}.jpg"
|
2021-03-13 11:22:51 +00:00
|
|
|
fi
|
|
|
|
while true; do
|
2021-03-24 01:13:18 +00:00
|
|
|
for frame in $(find "$NNN_PREVIEWDIR/$3"/*.jpg | sort -V); do
|
2021-03-14 06:53:03 +00:00
|
|
|
image_preview "$1" "$2" "$frame"
|
2021-03-13 11:22:51 +00:00
|
|
|
sleep 0.1
|
|
|
|
done
|
|
|
|
done &
|
2021-05-14 01:30:27 +00:00
|
|
|
printf "%s" "$!" > "$IMGPID"
|
2021-03-13 11:22:51 +00:00
|
|
|
return
|
|
|
|
else
|
2021-03-14 06:53:03 +00:00
|
|
|
image_preview "$1" "$2" "$3"
|
2021-03-13 11:22:51 +00:00
|
|
|
return
|
|
|
|
fi ;;
|
2021-03-24 01:13:18 +00:00
|
|
|
image) convert "$3" -flatten -resize "$NNN_PREVIEWWIDTH"x"$NNN_PREVIEWHEIGHT"\> "$NNN_PREVIEWDIR/$3.jpg" ;;
|
|
|
|
office) libreoffice --convert-to jpg "$3" --outdir "$NNN_PREVIEWDIR/${3%/*}" > /dev/null 2>&1
|
2021-04-27 06:26:52 +00:00
|
|
|
filename="$(printf "%s" "${3##*/}" | cut -d. -f1)"
|
2021-03-24 01:13:18 +00:00
|
|
|
mv "$NNN_PREVIEWDIR/${3%/*}/$filename.jpg" "$NNN_PREVIEWDIR/$3.jpg" ;;
|
|
|
|
pdf) pdftoppm -jpeg -f 1 -singlefile "$3" "$NNN_PREVIEWDIR/$3" >/dev/null 2>&1 ;;
|
2021-04-29 03:55:56 +00:00
|
|
|
video) ffmpegthumbnailer -s0 -i "$3" -o "$NNN_PREVIEWDIR/$3.jpg" >/dev/null 2>&1 || rm "$NNN_PREVIEWDIR/$3.jpg" ;;
|
2021-03-13 11:22:51 +00:00
|
|
|
esac
|
|
|
|
fi
|
2021-03-24 01:13:18 +00:00
|
|
|
if [ -f "$NNN_PREVIEWDIR/$3.jpg" ]; then
|
|
|
|
image_preview "$1" "$2" "$NNN_PREVIEWDIR/$3.jpg"
|
2021-03-21 03:49:19 +00:00
|
|
|
else
|
|
|
|
fifo_pager print_bin_info "$3"
|
|
|
|
fi
|
2021-03-13 11:22:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 06:53:03 +00:00
|
|
|
image_preview() {
|
2021-03-20 00:58:46 +00:00
|
|
|
clear
|
2021-03-14 06:53:03 +00:00
|
|
|
if [ "$TERMINAL" = "kitty" ]; then
|
2021-05-14 17:16:30 +00:00
|
|
|
# Kitty terminal users can use the native image preview method although one might consider
|
|
|
|
# installing ueberzug and moving the ueberzug clause to the top since it performs alot better.
|
2021-05-14 01:30:27 +00:00
|
|
|
kitty +kitten icat --silent --place "$1"x"$2"@0x0 --transfer-mode=stream --stdin=no "$3" &
|
|
|
|
printf "%s" "$!" > "$IMGPID"
|
2021-03-14 06:53:03 +00:00
|
|
|
elif exists ueberzug; then
|
|
|
|
ueberzug_layer "$1" "$2" "$3"
|
2021-03-13 11:22:51 +00:00
|
|
|
elif exists catimg; then
|
|
|
|
catimg "$3" &
|
2021-05-14 01:30:27 +00:00
|
|
|
printf "%s" "$!" > "$IMGPID"
|
2021-03-13 11:22:51 +00:00
|
|
|
elif exists viu; then
|
|
|
|
viu -t "$3" &
|
2021-05-14 01:30:27 +00:00
|
|
|
printf "%s" "$!" > "$IMGPID"
|
2021-03-14 06:53:03 +00:00
|
|
|
else
|
2021-03-21 01:41:14 +00:00
|
|
|
fifo_pager print_bin_info "$3"
|
2021-03-13 11:22:51 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
ueberzug_layer() {
|
2021-03-21 10:29:44 +00:00
|
|
|
printf '{"action": "add", "identifier": "nnn_ueberzug", "x": 0, "y": 0, "width": "%s", "height": "%s", "scaler": "fit_contain", "path": "%s"}\n' "$1" "$2" "$3" > "$FIFO_UEBERZUG"
|
2021-03-13 11:22:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ueberzug_remove() {
|
|
|
|
printf '{"action": "remove", "identifier": "nnn_ueberzug"}\n' > "$FIFO_UEBERZUG"
|
|
|
|
}
|
|
|
|
|
|
|
|
ueberzug_refresh() {
|
|
|
|
clear
|
2021-05-14 01:30:27 +00:00
|
|
|
kill "$(cat "$IMGPID" 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-13 11:22:51 +00:00
|
|
|
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
|
2021-04-27 06:26:52 +00:00
|
|
|
printf "\n" > "$NNN_FIFO"
|
2021-04-26 15:21:52 +00:00
|
|
|
preview_fifo 2>/dev/null &
|
2021-05-01 16:12:13 +00:00
|
|
|
preview_file "$(cat "$CURSEL")"
|
2021-03-13 11:22:51 +00:00
|
|
|
wait
|
|
|
|
}
|
|
|
|
|
|
|
|
preview_fifo() {
|
|
|
|
# use cat instead of 'exec <' to avoid issues with dash shell
|
|
|
|
# shellcheck disable=SC2002
|
|
|
|
cat "$NNN_FIFO" |\
|
2021-04-26 15:21:52 +00:00
|
|
|
while read -r selection; do
|
2021-05-14 01:30:27 +00:00
|
|
|
kill "$(cat "$IMGPID" 2>/dev/null)" >/dev/null 2>&1
|
2021-03-20 00:58:46 +00:00
|
|
|
kill "$(cat "$PAGERPID" 2>/dev/null)" >/dev/null 2>&1
|
2021-03-14 06:53:03 +00:00
|
|
|
[ "$TERMINAL" != "kitty" ] && exists ueberzug && ueberzug_remove
|
2021-03-13 11:22:51 +00:00
|
|
|
preview_file "$selection"
|
2021-04-27 06:26:52 +00:00
|
|
|
printf "%s" "$selection" > "$CURSEL"
|
2021-03-13 11:22:51 +00:00
|
|
|
done
|
2021-05-14 01:30:27 +00:00
|
|
|
kill "$(cat "$IMGPID" 2>/dev/null)" >/dev/null 2>&1
|
2021-05-01 16:12:13 +00:00
|
|
|
kill "$(cat "$PAGERPID" 2>/dev/null)" >/dev/null 2>&1
|
|
|
|
[ "$TERMINAL" != "kitty" ] && exists ueberzug && ueberzug_remove
|
2021-05-14 01:30:27 +00:00
|
|
|
rm "$PAGERPID" "$IMGPID" "$CURSEL" "$FIFO_UEBERZUG" >/dev/null 2>&1
|
2021-03-21 01:41:14 +00:00
|
|
|
pkill -P "$$" >/dev/null 2>&1
|
2021-03-13 11:22:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if [ "$PREVIEW_MODE" ]; then
|
2021-03-14 06:53:03 +00:00
|
|
|
if [ "$TERMINAL" != "kitty" ] && exists ueberzug; then
|
2021-03-13 11:22:51 +00:00
|
|
|
mkfifo "$FIFO_UEBERZUG"
|
2021-05-01 16:12:13 +00:00
|
|
|
trap 'ueberzug_refresh; rm "$FIFO_UEBERZUG"' WINCH
|
2021-03-13 11:22:51 +00:00
|
|
|
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
|
|
|
|
fi
|
2021-05-14 01:30:27 +00:00
|
|
|
trap 'rm "$PAGERPID" "$IMGPID" "$CURSEL" "$FIFO_UEBERZUG" 2>/dev/null' INT HUP EXIT
|
2021-03-13 11:22:51 +00:00
|
|
|
|
2021-05-01 16:12:13 +00:00
|
|
|
preview_file "$PWD/$1"
|
|
|
|
printf "%s" "$PWD/$1" > "$CURSEL" &
|
2021-04-26 15:21:52 +00:00
|
|
|
preview_fifo 2>/dev/null &
|
2021-03-13 11:22:51 +00:00
|
|
|
wait
|
2021-04-27 06:26:52 +00:00
|
|
|
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-ext." "$NNN_FIFO"
|
|
|
|
cfg=$(stty -g); stty raw -echo; head -c 1; stty "$cfg"
|
2021-04-27 06:26:52 +00:00
|
|
|
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-ext."
|
|
|
|
cfg=$(stty -g); stty raw -echo; head -c 1; stty "$cfg"
|
2021-04-27 06:26:52 +00:00
|
|
|
else
|
|
|
|
togglepreview "$1" &
|
|
|
|
fi
|
2021-03-13 11:22:51 +00:00
|
|
|
fi
|