vifm: Extend nsxiv thumbnailer

Now works recursively, only looks for fitting format extensions and can
display video thumbnails as well.

All options are documented in `-h` help display - `-r` enables recursive
lookup for directory current file is in, `-m` enables displaying video
thumbnails (takes a little longer to load) and `-t` starts nsxiv in
thumbnail mode.
This commit is contained in:
Marty Oehme 2022-05-01 18:45:21 +02:00
parent 89c028c8d6
commit 4abfc41ca2
Signed by: Marty
GPG key ID: B7538B8F50A1C800
2 changed files with 120 additions and 2 deletions

View file

@ -0,0 +1,115 @@
#!/usr/bin/env bash
show_help() {
printf """
vifm-thumbnailer
Easily show images/thumbnails from the vifm file manager.
Usage: vifm-thumbnailer <current file>
Point it to a file whose directory you want thumbnailed.
If you point it to a directory itself, it will list its contents.
Options:
-h Display this help.
-r Show images recursively from current folder.
-t Open viewer in thumbnail mode.
-m Show thumbnails for video files in addition to image files.
Will take longer to load up and requires ffmpeg utilities.
Vifm:
If the file is on your path (e.g. in ~/.vifm/scripts) from within
vifm you can run the script by invoking it with:
\`:!vifm-thumbnailer \%%c\`
which will show all directory thumbnails with the current file
initially shown.
If you want to have vifm show a filtered view for selected files
afterwards add a \%%u to the command.
"""
}
TMPDIR="$(mktemp -d)"
valid_files_tmp="$TMPDIR/nsxiv_rifle_$$"
# get all files in directory
list_img_files() {
if exist fd; then
fd --max-depth="$maxdepth" -tf . "${1%/*}" | is_img_extension | sort | tee -a "$valid_files_tmp"
else
find -L "${1%/*}" -maxdepth "$maxdepth" -type f -print | is_img_extension | sort | tee -a "$valid_files_tmp"
fi
}
list_vid_files() {
# shellcheck disable=2016
find "${1%/*}" -mindepth 1 -maxdepth "$maxdepth" \( -type f -o -type l \) -printf "%P\0" |
xargs -r0 -n 20 -P 64 file -00 -L --mime-type -- |
xargs -r0 -n 2 sh -c '
case "$2" in
video/*) printf "%s\0" "$1";;
esac
' -- |
sort -z |
xargs -r0 -I FILE ffmpegthumbnailer -i FILE -o "$TMPDIR/FILE.jpg" -m -s 384
find -L "$TMPDIR/" -maxdepth "$maxdepth" -type f -print | sort | tee -a "$valid_files_tmp"
}
img_ext="jpe?g|png|gif|svg|webp|tiff|heif|avif|ico|bmp"
is_img_extension() {
grep -iE "\.($img_ext)$"
}
# TODO if marking video thumbnail - will use wrong temporary path on output, makes e.g. vifm %u not work
open() {
trap 'rm -rf -- $TMPDIR' TERM INT EXIT
current="$(list_img_files "$1" | grep -nF "$1")"
if [ "$videos" = true ]; then
list_vid_files "$1"
fi
[ "$thumbnails" = true ] && nsxiv_args="-t"
if [ -n "$current" ]; then
nsxiv "$nsxiv_args" -i -n "${current%%:*}" -o -- <"$valid_files_tmp"
else
# fallback incase file didn't have a valid extension, or we couldn't
# find it inside the list
nsxiv -o -i -- <"$valid_files_tmp"
fi
}
maxdepth=1
while getopts "mrt?h" opt; do
case "$opt" in
r)
maxdepth=100
;;
m)
videos=true
;;
t)
thumbnails=true
;;
h | \? | *)
show_help
exit 0
;;
esac
done
shift $((OPTIND - 1))
[ "$1" = '--' ] && shift
case "$1" in
"")
echo "Usage: ${0##*/} PICTURES" >&2
exit 1
;;
/*) open "$1" ;;
"~"/*) open "$HOME/${1#"~"/}" ;;
*) open "$PWD/$1" ;;
esac

View file

@ -290,7 +290,8 @@ nnoremap <space>F :grep<space>
" preview thumbnails of current folder " preview thumbnails of current folder
" select thumbnails with m/M in nsxiv " select thumbnails with m/M in nsxiv
" to generate a filtered view on them " to generate a filtered view on them
nnoremap <space>t :!nsxiv -t %u -o %d<cr> nnoremap <space>t :!vifm-thumbnailer -t %u %c<cr>
nnoremap <space>T :!vifm-thumbnailer -r -t %u %c<cr>
" allows preview to work for normal view and single pane view " allows preview to work for normal view and single pane view
noremap <silent> w : if &quickview && !layoutis('only') noremap <silent> w : if &quickview && !layoutis('only')
@ -318,7 +319,7 @@ set classify+=' ::*.bmp,,*.gif,,*.jpeg,,*.jpg,,*.ico,,*.png,,*.ppm,,*.svg,,*.
" audio " audio
set classify+=' ::*.aac,,*.anx,,*.asf,,*.au,,*.axa,,*.flac,,*.m2a,,*.m4a,,*.mid,,*.midi,,*.mp3,,*.mpc,,*.oga,,*.ogg,,*.ogx,,*.ra,,*.ram,,*.rm,,*.spx,,*.wav,,*.wma,,*.ac3::' set classify+=' ::*.aac,,*.anx,,*.asf,,*.au,,*.axa,,*.flac,,*.m2a,,*.m4a,,*.mid,,*.midi,,*.mp3,,*.mpc,,*.oga,,*.ogg,,*.ogx,,*.ra,,*.ram,,*.rm,,*.spx,,*.wav,,*.wma,,*.ac3::'
" media " media
set classify+=' ::*.avi,,*.ts,,*.axv,,*.divx,,*.m2v,,*.m4p,,*.m4v,,.mka,,*.mkv,,*.mov,,*.mp4,,*.flv,,*.mp4v,,*.mpeg,,*.mpg,,*.nuv,,*.ogv,,*.pbm,,*.pgm,,*.qt,,*.vob,,*.wmv,,*.xvid::' set classify+=' ::*.avi,,*.ts,,*.axv,,*.divx,,*.m2v,,*.m4p,,*.m4v,,*.mka,,*.mkv,,*.mov,,*.mp4,,*.flv,,*.mp4v,,*.mpeg,,*.mpg,,*.nuv,,*.ogv,,*.pbm,,*.pgm,,*.qt,,*.vob,,*.wmv,,*.xvid::'
" office files " office files
set classify+=' ::*.doc,,*.docx::, ::*.xls,,*.xls[mx]::, ::*.pptx,,*.ppt::' set classify+=' ::*.doc,,*.docx::, ::*.xls,,*.xls[mx]::, ::*.pptx,,*.ppt::'
@ -433,6 +434,8 @@ fileviewer *.gif
" Images " Images
filextype *.bmp,*.jpg,*.jpeg,*.png,*.gif,*.xpm filextype *.bmp,*.jpg,*.jpeg,*.png,*.gif,*.xpm
\ {View in nsxiv directory viewer}
\ vifm-thumbnailer %c,
\ {View in nsxiv} \ {View in nsxiv}
\ nsxiv %f, \ nsxiv %f,
\ {View in vimiv} \ {View in vimiv}