]> Sergey Matveev's repositories - nnn.git/commitdiff
preview-tui: tmux sixel support for img, gif & vid preview
authorAbhinav Lakhani <3398abhinav@gmail.com>
Sun, 15 Oct 2023 06:27:46 +0000 (00:27 -0600)
committerAbhinav Lakhani <3398abhinav@gmail.com>
Sun, 12 Nov 2023 21:40:31 +0000 (14:40 -0700)
plugins/preview-tui

index 3c616455927afba0f71b599456784c63bedbb9e7..5f4fa60d46cd4f24a0c6e37f2f24341078226b15 100755 (executable)
 #   - optional: pistol file viewer (https://github.com/doronbehar/pistol).
 #       1. install pistol
 #       2. set/export $NNN_PISTOL as 1
+#   - optional: sixel
+#       - terminal with sixel support. ex. foot, wezterm, iterm2, macterm, mintty, etc. (check https://www.arewesixelyet.com/)
+#       - tmux with sixel support (should be compiled with `./configure --enable-sixel`)
+#       dependencies: mpv, img2sixel
+#
 #
 # Usage:
 #   You need to set a NNN_FIFO path and a key for the plugin with NNN_PLUG,
@@ -162,7 +167,7 @@ start_preview() {
         tmux) # tmux splits are inverted
             ENVVARS=("${ENVVARS[@]/#/-e}")
             if [ "$NNN_SPLIT" = "v" ]; then split="h"; else split="v"; fi
-            tmux split-window "${ENVVARS[@]}" -d"$split" -p"$NNN_SPLITSIZE" "$0" "$1" ;;
+            tmux split-window -l"$NNN_SPLITSIZE"% "${ENVVARS[@]}" -d"$split" -p"$NNN_SPLITSIZE" "$0" "$1" ;;
         kitty) # Setting the layout for the new window. It will be restored after the script ends.
             ENVVARS=("${ENVVARS[@]/#/--env=}")
             kitty @ goto-layout splits
@@ -389,6 +394,9 @@ generate_preview() {
                     done &
                     printf "%s" "$!" > "$PREVIEWPID"
                     return
+                 elif exists mpv; then
+                    video_preview "$1" "$2" "$3" 1
+                    return
                  else
                     image_preview "$1" "$2" "$3"
                     return
@@ -403,7 +411,7 @@ generate_preview() {
                     mv "$NNN_PREVIEWDIR/${3%/*}/$filename.jpg" "$NNN_PREVIEWDIR/$3.jpg" ;;
             pdf) pdftoppm -jpeg -f 1 -singlefile "$3" "$NNN_PREVIEWDIR/$3" ;;
             djvu) ddjvu -format=ppm -page=1 "$3" "$NNN_PREVIEWDIR/$3.jpg" ;;
-            video) ffmpegthumbnailer -m -s0 -i "$3" -o "$NNN_PREVIEWDIR/$3.jpg" || rm "$NNN_PREVIEWDIR/$3.jpg" ;;
+            video) video_preview "$1" "$2" "$3" && return ;;
         esac
     fi
     if [ -f "$NNN_PREVIEWDIR/$3.jpg" ]; then
@@ -428,12 +436,29 @@ image_preview() {
         viu -t "$3" &
     elif exists chafa    && { [ -z "$NNN_PREVIEWIMGPROG" ] || [ "$NNN_PREVIEWIMGPROG" = "chafa" ] ;}; then
         chafa "$3" &
+    elif exists img2sixel && { [ -z "$NNN_PREVIEWIMGPROG" ] || [ "$NNN_PREVIEWIMGPROG" = "img2sixel" ] ;}; then
+        img2sixel -p16 -g "$3" &
     else
         fifo_pager print_bin_info "$3" && return
     fi
     printf "%s" "$!" > "$PREVIEWPID"
 }
 
+video_preview() {
+    clear
+    exec >/dev/tty
+    if type mpv >/dev/null 2>&1 ; then
+        if [ "$4" -eq 1 ]; then # not gif
+            mpv --no-config --really-quiet --vo=sixel --profile=sw-fast --loop-file --mute=yes --start="10%" --length=4 "$3" &
+        else
+            mpv --no-config --really-quiet --vo=sixel --profile=sw-fast --loop-file --mute=yes "$3" &
+        fi
+    else
+        ffmpegthumbnailer -m -s0 -i "$3" -o "$NNN_PREVIEWDIR/$3.jpg" || rm "$NNN_PREVIEWDIR/$3.jpg" &
+    fi
+    printf "%s" "$!" > "$PREVIEWPID"
+}
+
 ueberzug_layer() {
     [ -f "$POSOFFSET" ] && read -r x y < "$POSOFFSET"
     printf '{"action": "add", "identifier": "nnn_ueberzug", "x": %d, "y": %d, "width": "%d", "height": "%d", "scaler": "fit_contain", "path": "%s"}\n'\