]> Sergey Matveev's repositories - nnn.git/commitdiff
Merge image plugins
authorLuuk van Baal <luukvbaal@gmail.com>
Sun, 30 May 2021 18:11:54 +0000 (20:11 +0200)
committerLuuk van Baal <luukvbaal@gmail.com>
Sun, 30 May 2021 18:11:54 +0000 (20:11 +0200)
README.md
plugins/README.md
plugins/imgthumb [deleted file]
plugins/imgview
plugins/vidthumb [deleted file]

index 2d96c2cc4da32ec64b5fa98e3d75a700913e3329..ba28ce1287e36f119be91a051a683fb508a428d7 100644 (file)
--- a/README.md
+++ b/README.md
@@ -83,7 +83,7 @@ It runs smoothly on the Pi, [Termux](https://www.youtube.com/embed/AbaauM7gUJw)
   - Open with desktop opener or specify a custom opener
   - File-specific colors (or minimal _dirs in context color_)
   - Icons (customize and compile-in)
-  - Plugins for image and video thumbnails
+  - Plugin for image, video and audio thumbnails
   - Create, list, extract, mount (FUSE based) archives
   - Option to open all text files in `$EDITOR`
 - Information
index e8aced3b2b291f0564bbb8f7f692a4290daf3461..eff3415274f8d360a7e48f95595d73682c5eb853 100644 (file)
@@ -35,9 +35,8 @@ Plugins extend the capabilities of `nnn`. They are _executable_ scripts (or bina
 | [gutenread](gutenread) | Browse, download, read from Project Gutenberg | sh | curl, unzip, w3m<br>[epr](https://github.com/wustho/epr) (optional) |
 | [hexview](hexview) | View a file in hex in `$PAGER` | sh | [hx](https://github.com/krpors/hx)/xxd |
 | [imgresize](imgresize) | Resize images in dir to screen resolution | sh | [imgp](https://github.com/jarun/imgp) |
-| [imgthumb](imgthumb) | View thumbnail of an image or dir of images | sh | [lsix](https://github.com/hackerb9/lsix) |
 | [imgur](imgur) | Upload an image to imgur (from [imgur-screenshot](https://github.com/jomo/imgur-screenshot)) | bash | - |
-| [imgview](imgview) | Browse images, set wallpaper, copy path ([config](https://wiki.archlinux.org/index.php/Sxiv#Assigning_keyboard_shortcuts)), [rename](https://github.com/jarun/nnn/wiki/Basic-use-cases#browse-rename-images)| sh | [imv](https://github.com/eXeC64/imv)/[sxiv](https://github.com/muennich/sxiv)/[viu](https://github.com/atanunq/viu), less|
+| [imgview](imgview) | View (thumbnail)images, set wallpaper, [rename](https://github.com/jarun/nnn/wiki/Basic-use-cases#browse-rename-images) and [more](https://wiki.archlinux.org/index.php/Sxiv#Assigning_keyboard_shortcuts)| sh | [imv](https://github.com/eXeC64/imv)/[sxiv](https://github.com/muennich/sxiv)/[viu](https://github.com/atanunq/viu)/<br>[catimg](https://github.com/posva/catimg)/[lsix](https://github.com/hackerb9/lsix)|
 | [ipinfo](ipinfo) | Fetch external IP address and whois information | sh | curl, whois |
 | [kdeconnect](kdeconnect) | Send selected files to an Android device [✓] | sh | kdeconnect-cli |
 | [launch](launch) | GUI application launcher | sh | fzf |
@@ -70,7 +69,6 @@ Plugins extend the capabilities of `nnn`. They are _executable_ scripts (or bina
 | [umounttree](umounttree) | Unmount a remote mountpoint from within | sh | fusermount |
 | [upgrade](upgrade) | Upgrade nnn manually on Debian 9 Stretch | sh | curl |
 | [upload](upload) | Upload to Firefox Send or ix.io (text) or file.io (bin) | sh | [ffsend](https://github.com/timvisee/ffsend), curl, jq, tr |
-| [vidthumb](vidthumb) | Show video thumbnails in terminal | sh | [ffmpegthumbnailer](https://github.com/dirkvdb/ffmpegthumbnailer),<br>[lsix](https://github.com/hackerb9/lsix) |
 | [wall](wall) | Set wallpaper or change colorscheme | sh | nitrogen/pywal |
 | [x2sel](x2sel) | Copy file list from system clipboard to selection | sh | _see in-file docs_ |
 | [xdgdefault](xdgdefault) | Set the default app for the hovered file type | sh | xdg-utils, fzf |
diff --git a/plugins/imgthumb b/plugins/imgthumb
deleted file mode 100755 (executable)
index a47f661..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/usr/bin/env sh
-
-# Description: View thumbnail of an image or a directory of images with lsix
-#
-# Dependencies: lsix
-#
-# Shell: POSIX compliant
-# Author: Arun Prakash Jana
-
-if [ -n "$1" ]; then
-    if [ -d "$1" ]; then
-        lsix "$1"/*
-    else
-        lsix "$1"
-    fi
-
-    printf "Press any key to exit..."
-    read -r _
-fi
index 667bcca71b3019b72659df4af93a78c59eb018dc..1e378dc81c038a8d7d2154638fd037b770f5fc16 100755 (executable)
 #!/usr/bin/env sh
 
-# Description: Open images in hovered  directory and thumbnails
-#              open hovered image in sxiv or viu and browse other images in the directory
+# Description: Open hovered or current directory in image viewer.
+#              Thumbnails for audio and video files are generated
+#              and deleted on exit. Supported image viewers open
+#              in thumbnail mode when the hovered entry is a directory.
 #
 # Dependencies:
 #   - imv (https://github.com/eXeC64/imv) or,
 #   - sxiv (https://github.com/muennich/sxiv) or,
-#   - viu (https://github.com/atanunq/viu), less
+#   - viu (https://github.com/atanunq/viu), or
+#   - catimg (https://github.com/posva/catimg), or
+#   - lsix (https://github.com/hackerb9/lsix)
 #
 # Shell: POSIX compliant
-# Author: Arun Prakash Jana
+# Author: Arun Prakash Jana, Luuk van Baal
 
-abspath() {
-    case "$1" in
-        /*) printf "%s\n" "$1";;
-        *)  printf "%s\n" "$PWD/$1";;
-    esac
-}
+target="$(readlink -f "$1")"
 
-listimages() {
-    find -L "$(dirname "$target")" -maxdepth 1 -type f -iregex \
-      '.*\(jpe?g\|bmp\|webp\|ico\|svg\|png\|gif\)$' -print0 | sort -z
+exit_prompt() {
+    [ -n "$1" ] && printf "%s\n" "$1"
+    printf "%s" "Press any key to exit..."
+    cfg=$(stty -g); stty raw -echo; head -c 1; stty "$cfg"
+    clear
+    exit
 }
 
-view_dir() {
-    target="$(abspath "$2")"
-    count="$(listimages | grep -a -m 1 -ZznF "$target" | cut -d: -f1)"
+nthumb_cleanup() {
+    [ -n "$!" ] && while [ -e /proc/"$!" ]; do sleep 1; done
+    if [ -f "$target" ]; then
+        rm -r "$(dirname "$target")"/.nthumbs
+    elif [ -d "$target" ]; then
+        rm -r "$target"/.nthumbs
+    fi &
+}
 
-    if [ -n "$count" ]; then
-        if [ "$1" = 'sxiv' ]; then
-            listimages | xargs -0 "$1" -an "$count" --
-        else
-            listimages | xargs -0 "$1" -n "$count" --
-        fi
+make_thumbs() {
+    if [ -d "$target" ]; then cd "$target" || exit_prompt; fi
+    if mkdir .nthumbs >/dev/null; then
+        thumbs=1
     else
-        shift
-        "$1" -- "$@" # fallback
+        exit_prompt
     fi
+    for file in *; do
+        case "$(file -bL --mime-type -- "$file")" in
+          image/*) ln -s "$PWD/$file" "$PWD/.nthumbs/$file" ;;
+          audio/*) ffmpeg -i "$file" ".nthumbs/${file%%.*}.jpg" -y >/dev/null 2>&1;;
+          video/*) ffmpegthumbnailer -i "$file" -o .nthumbs/"${file%%.*}".jpg 2> /dev/null ;;
+        esac
+    done
+    cd .nthumbs || return
 }
 
-if [ -z "$1" ] || ! [ -s "$1" ]; then
-    printf "empty file"
-    read -r _
-    exit 1
-fi
+view_dir() {
+  find . -maxdepth 0 -print0 | xargs -0 "$1" --
+}
 
 if uname | grep -q "Darwin"; then
-    if [ -f "$1" ]; then
-        open "$1" >/dev/null 2>&1 &
-    fi
-# `imvr` is often callable as `imv` on Linux distros
-# You might need to change the reference below
-elif type imvr >/dev/null 2>&1; then
-    if [ -f "$1" ]; then
-        view_dir imvr "$1" >/dev/null 2>&1 &
-    elif [ -d "$1" ] || [ -h "$1" ]; then
-        imvr "$1" >/dev/null 2>&1 &
+    [ -f "$1" ] && open "$1" >/dev/null 2>&1 &
+elif type lsix >/dev/null 2>&1; then
+    if [ -d "$target" ]; then
+        cd "$target" || exit_prompt
     fi
+    make_thumbs
+    clear
+    lsix
+    nthumb_cleanup
+    exit_prompt
 elif type sxiv >/dev/null 2>&1; then
-    if [ -f "$1" ]; then
-        view_dir sxiv "$1" >/dev/null 2>&1 &
-    elif [ -d "$1" ] || [ -h "$1" ]; then
-        sxiv -aqt "$1" >/dev/null 2>&1 &
+    make_thumbs
+    if [ -f "$target" ]; then
+        view_dir sxiv >/dev/null 2>&1 &
+    elif [ -d "$target" ]; then
+        sxiv -aqt . >/dev/null 2>&1 &
+    fi
+elif type imv >/dev/null 2>&1; then
+    make_thumbs
+    if [ -f "$target" ]; then
+        view_dir imv >/dev/null 2>&1 &
+    elif [ -d "$target" ]; then
+        imv . >/dev/null 2>&1 &
+    fi
+elif type imvr >/dev/null 2>&1; then
+    make_thumbs
+    if [ -f "$target" ]; then
+        view_dir imvr >/dev/null 2>&1 &
+    elif [ -d "$target" ]; then
+        imvr . >/dev/null 2>&1 &
     fi
 elif type viu >/dev/null 2>&1; then
-    viu -n "$1" | less -R
+    clear
+    viu -n "$1"
+    exit_prompt
+elif type catimg >/dev/null 2>&1; then
+    clear
+    catimg "$1"
+    exit_prompt
 else
-    printf "Please install imv/sxiv/viu and check their callable names match the plugin source"
-    read -r _
-    exit 2
+    exit_prompt "Please install sxiv/imv/viu/catimg/lsix."
 fi
+[ -n "$thumbs" ] && nthumb_cleanup &
diff --git a/plugins/vidthumb b/plugins/vidthumb
deleted file mode 100755 (executable)
index 590cf81..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-#!/usr/bin/env sh
-
-# Description: Generate video thumbnails and view them
-#
-# Dependencies:
-#   - ffmpegthumbnailer: https://github.com/dirkvdb/ffmpegthumbnailer
-#   - lsix: https://github.com/hackerb9/lsix
-#
-# Shell: POSIX compliant
-# Author: Arun Prakash Jana
-
-mkdir .nthumbs > /dev/null 2>&1
-
-for file in *; do
-    if [ -f "$file" ]; then
-        ffmpegthumbnailer -i "$file" -o .nthumbs/"${file%%.*}".jpg 2> /dev/null
-    fi
-done
-
-# render thumbnails in lsix
-lsix .nthumbs/*
-
-# remove the thumbnails
-rm -rf .nthumbs
-
-printf "Press any key to exit..."
-read -r _