commit f90e567a17cfdf5109a3a97eb60be6608a690921
parent 638676a6894fa1ad0065254a272d529c7e59c496
Author: luukvbaal <31730729+luukvbaal@users.noreply.github.com>
Date: Tue, 23 Mar 2021 18:13:18 -0700
cache and constraint preview dimensions (#910)
* cache and constraint preview dimensions
* cache subdirectory
* optional caching
* revert to ext for pdf/epub
Diffstat:
1 file changed, 32 insertions(+), 32 deletions(-)
diff --git a/plugins/preview-tui-ext b/plugins/preview-tui-ext
@@ -47,7 +47,7 @@
#
# $ NNN_FIFO=/tmp/nnn.fifo nnn
#
-# Then in `nnn`, launch the `preview-tui` plugin.
+# Then in `nnn`, launch the `preview-tui-ext` plugin.
#
# 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.
@@ -76,6 +76,10 @@ USE_PISTOL="${USE_PISTOL:-0}"
PAGER="${PAGER:-less -P?n -R}"
ARCHIVES="$(echo "$NNN_ARCHIVE" | sed 's/.*(\(.*\)).*/\1/;s/|/ /g')"
TMPDIR="${TMPDIR:-/tmp}"
+# Consider setting NNN_PREVIEWDIR to $XDG_CACHE_DIR if you want to keep previews on disk between reboots
+NNN_PREVIEWDIR="${NNN_PREVIEWDIR:-$TMPDIR}/nnn/previews"
+NNN_PREVIEWWIDTH="${NNN_PREVIEWWIDTH:-1920}"
+NNN_PREVIEWHEIGHT="${NNN_PREVIEWHEIGHT:-1080}"
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"
@@ -131,7 +135,7 @@ print_bin_info() {
fi
}
-preview_file () {
+preview_file() {
clear
# Trying to use pistol if it's available.
if [ "$USE_PISTOL" -ne 0 ] && exists pistol; then
@@ -168,12 +172,14 @@ preview_file () {
fifo_pager ls --color=always
fi
elif [ "$encoding" = "binary" ]; then
- if [ "$ext" = "gif" ]; then
- generate_preview "$cols" "$lines" "$1" "gif"
- elif [ "${mimetype#*/}" = "x-xcf" ]; then
- generate_preview "$cols" "$lines" "$1" "xcf"
- elif [ "${mimetype%%/*}" = "image" ]; then
- image_preview "$cols" "$lines" "$1"
+ if [ "${mimetype%%/*}" = "image" ]; then
+ if [ "${mimetype#*/}" = "gif" ] && exists convert; then
+ generate_preview "$cols" "$lines" "$1" "gif"
+ elif [ ! "${mimetype#*/}" = "jpeg" ]; then
+ generate_preview "$cols" "$lines" "$1" "image"
+ else
+ image_preview "$cols" "$lines" "$1"
+ fi
elif [ "${mimetype%%/*}" = "audio" ] && exists ffmpeg; then
generate_preview "$cols" "$lines" "$1" "audio"
elif [ "${mimetype%%/*}" = "video" ] && exists ffmpegthumbnailer; then
@@ -226,19 +232,19 @@ preview_file () {
}
generate_preview() {
- if [ ! -f "$TMPDIR/$3.jpg" ]; then
- mkdir -p "$TMPDIR/${3%/*}"
+ if [ ! -f "$NNN_PREVIEWDIR/$3.jpg" ] || [ -n "$(find -L "$3" -newer "$NNN_PREVIEWDIR/$3.jpg")" ]; then
+ mkdir -p "$NNN_PREVIEWDIR/${3%/*}"
case $4 in
- audio) ffmpeg -i "$3" "$TMPDIR/$3.jpg" -y >/dev/null 2>&1 ;;
- epub) gnome-epub-thumbnailer "$3" "$TMPDIR/$3.jpg" >/dev/null 2>&1 ;;
- font) fontpreview -i "$3" -o "$TMPDIR/$3.jpg" >/dev/null 2>&1 ;;
+ 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 ;;
gif) if exists ueberzug && exists convert || [ "$TERMINAL" = "kitty" ]; then
- if [ ! -d "$TMPDIR/$3" ]; then
- mkdir -p "$TMPDIR/$3"
- convert -coalesce "$3" "$TMPDIR/$3/${3##*/}.jpg"
+ 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"
fi
while true; do
- for frame in $(find "$TMPDIR/$3"/*.jpg | sort -V); do
+ for frame in $(find "$NNN_PREVIEWDIR/$3"/*.jpg | sort -V); do
image_preview "$1" "$2" "$frame"
sleep 0.1
done
@@ -250,20 +256,16 @@ generate_preview() {
image_preview "$1" "$2" "$3"
return
fi ;;
- office) libreoffice --convert-to jpg "$3" --outdir "$TMPDIR/${3%/*}" > /dev/null 2>&1
+ 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
filename="$(echo "${3##*/}" | cut -d. -f1)"
- mv "$TMPDIR/${3%/*}/$filename.jpg" "$TMPDIR/$3.jpg" ;;
- pdf) pdftoppm -jpeg -f 1 -singlefile "$3" "$TMPDIR/$3" >/dev/null 2>&1 ;;
- video) if ! ffmpegthumbnailer -i "$3" -o "$TMPDIR/$3.jpg" -s 0 -q 10 >/dev/null 2>&1; then
- rm "$TMPDIR/$3.jpg"
- fifo_pager print_bin_info "$3"
- return
- fi ;;
- xcf) convert -flatten "$3" "$TMPDIR/$3.jpg" ;;
+ mv "$NNN_PREVIEWDIR/${3%/*}/$filename.jpg" "$NNN_PREVIEWDIR/$3.jpg" ;;
+ pdf) pdftoppm -jpeg -f 1 -singlefile "$3" "$NNN_PREVIEWDIR/$3" >/dev/null 2>&1 ;;
+ video) ffmpegthumbnailer -m -s0 -i "$3" -o "$NNN_PREVIEWDIR/$3.jpg" >/dev/null 2>&1 || rm "$NNN_PREVIEWDIR/$3.jpg" ;;
esac
fi
- if [ -f "$TMPDIR/$3.jpg" ]; then
- image_preview "$1" "$2" "$TMPDIR/$3.jpg"
+ if [ -f "$NNN_PREVIEWDIR/$3.jpg" ]; then
+ image_preview "$1" "$2" "$NNN_PREVIEWDIR/$3.jpg"
else
fifo_pager print_bin_info "$3"
fi
@@ -307,10 +309,6 @@ ueberzug_refresh() {
preview_fifo &
wait
}
-if [ "$TERMINAL" != "kitty" ] && [ "$PREVIEW_MODE" ] && exists ueberzug; then
- trap 'ueberzug_refresh' WINCH
- trap 'rm "$FIFO_UEBERZUG" "$PAGERPID" "$GIFPID" "$CURSEL"' INT HUP EXIT
-fi
preview_fifo() {
# use cat instead of 'exec <' to avoid issues with dash shell
@@ -338,6 +336,8 @@ if [ "$PREVIEW_MODE" ]; then
if [ "$TERMINAL" != "kitty" ] && exists ueberzug; then
mkfifo "$FIFO_UEBERZUG"
+ trap 'ueberzug_refresh' WINCH
+ trap 'rm "$FIFO_UEBERZUG" "$PAGERPID" "$GIFPID" "$CURSEL"' INT HUP EXIT
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
fi