preview-tui (23174B)
1 #!/usr/bin/env bash 2 3 # Description: Terminal based file previewer 4 # 5 # Note: This plugin needs a "NNN_FIFO" to work. See man. 6 # 7 # Dependencies: 8 # - Supports 7 independent methods to preview with: 9 # - tmux (>=3.0), or 10 # - kitty with allow_remote_control and listen_on set in kitty.conf, or 11 # - wezterm (https://wezfurlong.org/wezterm), or 12 # - QuickLook on WSL (https://github.com/QL-Win/QuickLook), or 13 # - Windows Terminal (https://github.com/Microsoft/Terminal | https://aka.ms/terminal) with WSL, or 14 # - zellij (https://github.com/zellij-org/zellij) or 15 # - $NNN_TERMINAL set to a terminal (it's xterm by default). 16 # - less or $NNN_PAGER 17 # - tree or exa/eza or (GNU) ls 18 # - mediainfo or file 19 # - mktemp 20 # - unzip 21 # - tar 22 # - man 23 # - optional: bsdtar or atool for additional archive preview 24 # - optional: bat for code syntax highlighting 25 # - optional: ueberzug, kitty terminal, wezterm terminal, img2sixel, viu, catimg, timg or chafa for images 26 # - optional: convert(ImageMagick) for playing gif preview (mandatory for kitty image previews) 27 # - optional: mpv for gif and video 28 # Also requires a terminal supporting the sixel (https://www.arewesixelyet.com/) 29 # or kitty (https://sw.kovidgoyal.net/kitty/graphics-protocol) video_output backends. 30 # Requires tmux compiled with `./configure --enable-sixel` if used. 31 # - optional: ffmpegthumbnailer for video thumbnails (https://github.com/dirkvdb/ffmpegthumbnailer) 32 # - optional: ffmpeg for audio thumbnails 33 # - optional: libreoffce for opendocument/officedocument preview 34 # - optional: pdftoppm(poppler) for pdf thumbnails 35 # - optional: gnome-epub-thumbnailer for epub thumbnails (https://gitlab.gnome.org/GNOME/gnome-epub-thumbnailer) 36 # - optional: fontpreview for font preview (https://github.com/sdushantha/fontpreview) 37 # - optional: djvulibre for djvu 38 # - optional: glow or lowdown for markdown 39 # - optional: w3m or lynx or elinks for html 40 # - optional: set/export NNN_ICONLOOKUP as 1 to enable file icons in front of directory previews with .iconlookup 41 # Icons and colors are configurable in .iconlookup 42 # - optional: scope.sh file viewer from ranger. 43 # 1. drop scope.sh executable in $PATH 44 # 2. set/export $NNN_SCOPE as 1 45 # - optional: pistol file viewer (https://github.com/doronbehar/pistol). 46 # 1. install pistol 47 # 2. set/export $NNN_PISTOL as 1 48 # - optional: librsvg for rsvg-convert 49 # 50 # Usage: 51 # You need to set a NNN_FIFO path and a key for the plugin with NNN_PLUG, 52 # then start `nnn`: 53 # 54 # $ nnn -a 55 # 56 # or 57 # 58 # $ NNN_FIFO=/tmp/nnn.fifo nnn 59 # 60 # Then launch the `preview-tui` plugin in `nnn`. 61 # 62 # If you provide the same NNN_FIFO to all nnn instances, there will be a 63 # single common preview window. If you provide different FIFO path (e.g. 64 # with -a), they will be independent. 65 # 66 # The previews will be shown in a tmux split. If that isn't possible, it 67 # will try to use a kitty terminal split. And as a final fallback, a 68 # different terminal window will be used ($NNN_TERMINAL). 69 # 70 # Kitty users need something similar to the following in their kitty.conf: 71 # - `allow_remote_control yes` 72 # - `listen_on unix:$TMPDIR/kitty` 73 # - `enabled_layouts splits` (optional) 74 # With ImageMagick installed, this terminal can use the icat kitten to display images. 75 # Refer to kitty documentation for further details. 76 # 77 # Users with both tmux and kitty can leverage image previews inside tmux with kitty's icat kitten 78 # - setup kitty as stated above 79 # - tmux >= v3.3a required 80 # - add the following to your tmux.conf: 81 # - `set -g allow-passthrough on` 82 # 83 # Wezterm should work out of the box. If `NNN_PREVIEWIMGPROG` is not specified it will use 84 # built in iTerm2 image protocol. 85 # 86 # Note that GNU ls is used for its `--group-directories-first` flag. 87 # On MacOS this may be installed with `brew install coreutils`, or the flag can be removed. 88 # iTerm2 users are recommended to use viu to view images without getting pixelated. 89 # 90 # Windows Terminal users can set "Profile termination behavior" under "Profile > Advanced" settings 91 # to automatically close pane on quit when exit code is 0. 92 # 93 # When specifying a different terminal, additional arguments are supported. In particular, you can 94 # append a specific title to the terminal and set it to "nofocus" in your WM config. 95 # E.g for alacritty and i3, you can set $NNN_TERMINAL to 'alacritty --title preview-tui' and add 96 # 'no_focus [title="preview-tui"]' to your i3 config file. 97 # 98 # Shell: Bash (for environment manipulation through arrays) 99 # Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste, Mario Ortiz Manero, Luuk van Baal, @WanderLanz, @flipflop133 100 101 # Configurable environment variables: 102 NNN_SPLIT=${NNN_SPLIT:-} # permanent split direction 103 NNN_TERMINAL=${NNN_TERMINAL:-} # external terminal to be used 104 NNN_SPLITSIZE=${NNN_SPLITSIZE:-50} # previewer split size percentage 105 TMPDIR=${TMPDIR:-/tmp} # location of temporary files 106 ENVVARS=( 107 "NNN_SCOPE=${NNN_SCOPE:-0}" # use scope 108 "NNN_PISTOL=${NNN_PISTOL:-0}" # use pistol 109 "NNN_ICONLOOKUP=${NNN_ICONLOOKUP:-0}" # use .iconlookup 110 "NNN_PAGER=${NNN_PAGER:-less -P?n -R -C}" # pager options 111 "NNN_BATTHEME=${NNN_BATTHEME:-ansi}" # bat theme 112 "NNN_BATSTYLE=${NNN_BATSTYLE:-numbers}" # bat style 113 "NNN_PREVIEWWIDTH=${NNN_PREVIEWWIDTH:-1920}" # width of generated preview images 114 "NNN_PREVIEWHEIGHT=${NNN_PREVIEWHEIGHT:-1080}" # height of generated preview images 115 "NNN_PREVIEWDIR=${NNN_PREVIEWDIR:-$TMPDIR/nnn/previews}" # location of generated preview images 116 "NNN_PREVIEWIMGPROG=${NNN_PREVIEWIMGPROG:-}" # command used to preview images 117 "NNN_PREVIEWVIDEO=${NNN_PREVIEWVIDEO:-}" # mpv backend used to preview video 118 ) 119 # Non-configurable environment variables 120 NNN_PARENT=${NNN_FIFO#*.} 121 [ "$NNN_PARENT" -eq "$NNN_PARENT" ] 2>/dev/null || NNN_PARENT="" # Make empty if non-numeric 122 ENVVARS+=( 123 "PWD=$PWD" 124 "PATH=$PATH" 125 "NNN_FIFO=$NNN_FIFO" 126 "FIFOPID=$TMPDIR/nnn-preview-tui-fifopid.$NNN_PARENT" 127 "FIFOPATH=$TMPDIR/nnn-preview-tui-fifo.$NNN_PARENT" 128 "PREVIEWPID=$TMPDIR/nnn-preview-tui-previewpid.$NNN_PARENT" 129 "CURSEL=$TMPDIR/nnn-preview-tui-selection.$NNN_PARENT" 130 "FIFO_UEBERZUG=$TMPDIR/nnn-preview-tui-ueberzug-fifo.$NNN_PARENT" 131 "POSOFFSET=$TMPDIR/nnn-preview-tui-posoffset" 132 ) 133 134 trap '' PIPE 135 exists() { type "$1" >/dev/null 2>&1 ;} 136 pkill() { command pkill "$@" >/dev/null 2>&1 ;} 137 prompt() { clear; printf "%b" "$@"; cfg=$(stty -g); stty raw -echo; head -c 1; stty "$cfg" ;} 138 pidkill() { 139 if [ -f "$1" ]; then 140 PID="$(cat "$1" 2>/dev/null)" || return 1 141 kill "$PID" >/dev/null 2>&1 142 RET=$? 143 wait "$PID" 2>/dev/null 144 return $RET 145 fi 146 return 1 147 } 148 149 start_preview() { 150 if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.'; then 151 NNN_TERMINAL=tmux 152 exists mpv && tmux display -p '#{client_termfeatures}' | grep -q 'sixel' && ENVVARS+=("NNN_PREVIEWVIDEO=sixel") 153 elif [ -n "$KITTY_LISTEN_ON" ]; then 154 NNN_TERMINAL=kitty 155 exists mpv && ENVVARS+=("NNN_PREVIEWVIDEO=kitty") 156 elif [ -n "$WEZTERM_PANE" ]; then 157 NNN_TERMINAL=wezterm 158 exists mpv && ENVVARS+=("NNN_PREVIEWVIDEO=kitty") 159 elif [ -z "$NNN_TERMINAL" ] && [ "$TERM_PROGRAM" = "iTerm.app" ]; then 160 NNN_TERMINAL=iterm 161 exists mpv && ENVVARS+=("NNN_PREVIEWVIDEO=sixel") 162 elif [ -n "$WT_SESSION" ]; then 163 NNN_TERMINAL=winterm 164 elif [ -n "$ZELLIJ" ]; then 165 NNN_TERMINAL=zellij 166 else 167 NNN_TERMINAL="${NNN_TERMINAL:-xterm}" 168 fi 169 170 if [ -z "$NNN_SPLIT" ] && [ $(($(tput lines) * 2)) -gt "$(tput cols)" ]; then 171 NNN_SPLIT='h' 172 elif [ "$NNN_SPLIT" != 'h' ]; then 173 NNN_SPLIT='v' 174 fi 175 176 ENVVARS+=("NNN_TERMINAL=$NNN_TERMINAL" "NNN_SPLIT=$NNN_SPLIT" "QLPATH=$2" "PREVIEW_MODE=1") 177 case "$NNN_TERMINAL" in 178 iterm|winterm) # has to run in separate shell command: escape 179 ENVVARS=("${ENVVARS[@]/#/\\\"}") 180 ENVVARS=("${ENVVARS[@]/%/\\\"}") 181 command="$SHELL -c 'env ${ENVVARS[*]} \\\"$0\\\" \\\"$1\\\"'" ;; 182 esac 183 184 case "$NNN_TERMINAL" in 185 tmux) # tmux splits are inverted 186 ENVVARS=("${ENVVARS[@]/#/-e}") 187 if [ "$NNN_SPLIT" = "v" ]; then split="h"; else split="v"; fi 188 tmux split-window -l"$NNN_SPLITSIZE"% "${ENVVARS[@]}" -d"$split" -p"$NNN_SPLITSIZE" "$0" "$1" ;; 189 kitty) # Setting the layout for the new window. It will be restored after the script ends. 190 ENVVARS=("${ENVVARS[@]/#/--env=}") 191 kitty @ goto-layout splits 192 # Trying to use kitty's integrated window management as the split window. 193 kitty @ launch --no-response --title "preview-tui" --keep-focus \ 194 --cwd "$PWD" "${ENVVARS[@]}" --location "${NNN_SPLIT}split" "$0" "$1" ;; 195 wezterm) 196 export "${ENVVARS[@]}" 197 if [ "$NNN_SPLIT" = "v" ]; then split="--horizontal"; else split="--bottom"; fi 198 wezterm cli split-pane --cwd "$PWD" $split --percent "$NNN_SPLITSIZE" "$0" "$1" >/dev/null 199 wezterm cli activate-pane-direction Prev ;; 200 iterm) 201 if [ "$NNN_SPLIT" = "h" ]; then split="horizontally"; else split="vertically"; fi 202 osascript <<-EOF 203 tell application "iTerm" 204 tell current session of current window 205 split $split with default profile command "$command" 206 end tell 207 end tell 208 EOF 209 ;; 210 winterm) 211 if [ "$NNN_SPLIT" = "h" ]; then split="H"; else split="V"; fi 212 wt -w 0 sp -$split -s"0.$NNN_SPLITSIZE" "$command" \; -w 0 mf previous 2>/dev/null ;; 213 zellij) 214 case "$NNN_SPLIT" in 215 h) 216 direction="down" ;; 217 *) 218 direction=right ;; 219 esac 220 zellij run --name "nnn preview" --close-on-exit \ 221 --direction "$direction" -- env "${ENVVARS[@]}" "$0" "$1" & 222 (sleep 0.2 && zellij action focus-previous-pane) & 223 ;; 224 *) if [ -n "$2" ]; then 225 env "${ENVVARS[@]}" QUICKLOOK=1 "$0" "$1" & 226 else 227 # shellcheck disable=SC2086 # (allow arguments) 228 env "${ENVVARS[@]}" $NNN_TERMINAL -e "$0" "$1" & 229 fi ;; 230 esac 231 } 232 233 toggle_preview() { 234 export "${ENVVARS[@]}" 235 if exists QuickLook.exe; then 236 QLPATH="QuickLook.exe" 237 elif exists Bridge.exe; then 238 QLPATH="Bridge.exe" 239 fi 240 if pidkill "$FIFOPID"; then 241 [ -p "$NNN_PPIPE" ] && printf "0" > "$NNN_PPIPE" 242 pidkill "$PREVIEWPID" 243 pkill -f "tail --follow $FIFO_UEBERZUG" 244 if [ -n "$QLPATH" ] && stat "$1"; then 245 f="$(wslpath -w "$1")" && "$QLPATH" "$f" & 246 fi 247 else 248 [ -p "$NNN_PPIPE" ] && printf "1" > "$NNN_PPIPE" 249 start_preview "$1" "$QLPATH" 250 fi 251 } 252 253 fifo_pager() { 254 cmd="$1" 255 shift 256 257 # We use a FIFO to access $NNN_PAGER PID in jobs control 258 mkfifo "$FIFOPATH" || return 259 260 $NNN_PAGER < "$FIFOPATH" & 261 printf "%s" "$!" > "$PREVIEWPID" 262 263 ( 264 exec > "$FIFOPATH" 265 if [ "$cmd" = "pager" ]; then 266 if exists bat; then 267 bat --terminal-width="$cols" --decorations=always --color=always \ 268 --paging=never --style="$NNN_BATSTYLE" --theme="$NNN_BATTHEME" "$@" & 269 else 270 $NNN_PAGER "$@" & 271 fi 272 else 273 "$cmd" "$@" & 274 fi 275 ) 276 277 rm -- "$FIFOPATH" 278 } 279 280 # Binary file: show file info inside the pager 281 print_bin_info() { 282 printf -- "-------- \033[1;31mBinary file\033[0m --------\n" 283 if exists mediainfo; then 284 mediainfo "$1" 285 else 286 file -b "$1" 287 fi 288 } 289 290 handle_mime() { 291 case "$2" in 292 image/jpeg) image_preview "$cols" "$lines" "$1" ;; 293 image/gif) generate_preview "$cols" "$lines" "$1" "gif" ;; 294 image/vnd.djvu) generate_preview "$cols" "$lines" "$1" "djvu" ;; 295 image/*) generate_preview "$cols" "$lines" "$1" "image" ;; 296 video/*) generate_preview "$cols" "$lines" "$1" "video" ;; 297 audio/*) generate_preview "$cols" "$lines" "$1" "audio" ;; 298 application/font*|application/*opentype|font/*) generate_preview "$cols" "$lines" "$1" "font" ;; 299 */*office*|*/*document*|*/*msword|*/*ms-excel) generate_preview "$cols" "$lines" "$1" "office" ;; 300 application/zip) fifo_pager unzip -l "$1" ;; 301 text/troff) 302 if exists man; then 303 fifo_pager man -Pcat -l "$1" 304 else 305 fifo_pager pager "$1" 306 fi ;; 307 *) handle_ext "$1" "$3" "$4" ;; 308 esac 309 } 310 311 handle_ext() { 312 case "$2" in 313 epub) generate_preview "$cols" "$lines" "$1" "epub" ;; 314 pdf) generate_preview "$cols" "$lines" "$1" "pdf" ;; 315 gz|bz2) fifo_pager tar -tvf "$1" ;; 316 md) if exists glow; then 317 fifo_pager glow -s dark "$1" 318 elif exists lowdown; then 319 fifo_pager lowdown -Tterm --term-width="$cols" --term-column="$cols" "$1" 320 else 321 fifo_pager pager "$1" 322 fi ;; 323 htm|html|xhtml) 324 if exists w3m; then 325 fifo_pager w3m "$1" 326 elif exists lynx; then 327 fifo_pager lynx "$1" 328 elif exists elinks; then 329 fifo_pager elinks "$1" 330 else 331 fifo_pager pager "$1" 332 fi ;; 333 7z|a|ace|alz|arc|arj|bz|cab|cpio|deb|jar|lha|lz|lzh|lzma|lzo\ 334 |rar|rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z) 335 if exists atool; then 336 fifo_pager atool -l "$1" 337 elif exists bsdtar; then 338 fifo_pager bsdtar -tvf "$1" 339 fi ;; 340 json) 341 if exists jq; then 342 fifo_pager jq --color-output '.' "$1" 343 else 344 fifo_pager pager "$1" 345 fi ;; 346 *) if [ "$3" = "bin" ]; then 347 fifo_pager print_bin_info "$1" 348 else 349 fifo_pager pager "$1" 350 fi ;; 351 esac 352 } 353 354 preview_file() { 355 clear 356 # Trying to use pistol if it's available. 357 if [ "$NNN_PISTOL" -ne 0 ] && exists pistol; then 358 fifo_pager pistol "$1" 359 return 360 fi 361 362 # Trying to use scope.sh if it's available. 363 if [ "$NNN_SCOPE" -ne 0 ] && exists scope.sh; then 364 fifo_pager scope.sh "$1" "$cols" "$lines" "$(mktemp -d)" "True" 365 return 366 fi 367 368 # Use QuickLook if it's available. 369 if [ -n "$QUICKLOOK" ]; then 370 stat "$1" && f="$(wslpath -w "$1")" && "$QLPATH" "$f" & 371 return 372 fi 373 374 # Detecting the exact type of the file: the encoding, mime type, and extension in lowercase. 375 encoding="$(file -bL --mime-encoding -- "$1")" 376 mimetype="$(file -bL --mime-type -- "$1")" 377 ext="${1##*.}" 378 [ -n "$ext" ] && ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')" 379 lines=$(tput lines) 380 cols=$(tput cols) 381 382 # Otherwise, falling back to the defaults. 383 if [ -d "$1" ]; then 384 cd "$1" || return 385 if [ "$NNN_ICONLOOKUP" -ne 0 ] && [ -f "$(dirname "$0")"/.iconlookup ]; then 386 [ "$NNN_SPLIT" = v ] && BSTR="\n" 387 # shellcheck disable=SC2012 388 ls -F --group-directories-first | head -n "$((lines - 3))" | "$(dirname "$0")"/.iconlookup -l "$cols" -B "$BSTR" -b " " 389 elif exists tree; then 390 fifo_pager tree --filelimit "$(find . -maxdepth 1 | wc -l)" -L 3 -C -F --dirsfirst --noreport 391 elif exists exa; then 392 fifo_pager exa -T --group-directories-first --colour=always -L 3 . 393 elif exists eza; then # eza is a community fork of exa (exa is unmaintained) 394 fifo_pager eza -T --group-directories-first --colour=always -L 3 . 395 else 396 fifo_pager ls -F --group-directories-first --color=always 397 fi 398 cd .. 399 elif [ "${encoding#*)}" = "binary" ]; then 400 handle_mime "$1" "$mimetype" "$ext" "bin" 401 else 402 handle_mime "$1" "$mimetype" "$ext" 403 fi 404 } 405 406 generate_preview() { 407 if [ -n "$QLPATH" ] && stat "$3"; then 408 f="$(wslpath -w "$3")" && "$QLPATH" "$f" & 409 elif [ -n "$NNN_PREVIEWVIDEO" ] && [[ "$4" == +(gif|video) ]]; then 410 [ "$4" = "video" ] && args=(--start=10% --length=4) || args=() 411 video_preview "$1" "$2" "$3" "${args[@]}" && return 412 elif [ ! -f "$NNN_PREVIEWDIR/$3.jpg" ] || [ -n "$(find -L "$3" -newer "$NNN_PREVIEWDIR/$3.jpg")" ]; then 413 mkdir -p "$NNN_PREVIEWDIR/${3%/*}" 414 case $4 in 415 audio) ffmpeg -i "$3" -filter_complex "scale=iw*min(1\,min($NNN_PREVIEWWIDTH/iw\,ih)):-1" "$NNN_PREVIEWDIR/$3.jpg" -y ;; 416 epub) gnome-epub-thumbnailer "$3" "$NNN_PREVIEWDIR/$3.jpg" ;; 417 font) fontpreview -i "$3" -o "$NNN_PREVIEWDIR/$3.jpg" ;; 418 gif) if [ -p "$FIFO_UEBERZUG" ] && exists convert; then 419 frameprefix="$NNN_PREVIEWDIR/$3/${3##*/}" 420 if [ ! -d "$NNN_PREVIEWDIR/$3" ]; then 421 mkdir -p "$NNN_PREVIEWDIR/$3" 422 convert -coalesce -resize "$NNN_PREVIEWWIDTH"x"$NNN_PREVIEWHEIGHT"\> "$3" "$frameprefix.jpg" || 423 MAGICK_TMPDIR="/tmp" convert -coalesce -resize "$NNN_PREVIEWWIDTH"x"$NNN_PREVIEWHEIGHT"\> "$3" "$frameprefix.jpg" 424 fi 425 frames=$(($(find "$NNN_PREVIEWDIR/$3" | wc -l) - 2)) 426 [ $frames -lt 0 ] && return 427 while true; do 428 for i in $(seq 0 $frames); do 429 image_preview "$1" "$2" "$frameprefix-$i.jpg" 430 sleep 0.1 431 done 432 done & 433 printf "%s" "$!" > "$PREVIEWPID" 434 return 435 elif [ -n "$NNN_PREVIEWVIDEO" ]; then 436 video_preview "$1" "$2" "$3" && return 437 else 438 image_preview "$1" "$2" "$3" && return 439 fi ;; 440 image) if exists rsvg-convert && [[ "${3##*.}" == "svg" ]]; then 441 rsvg-convert -a -w "$NNN_PREVIEWWIDTH" -h "$NNN_PREVIEWHEIGHT" -f png -o "$NNN_PREVIEWDIR/$3.png" "$3" 442 elif exists convert; then 443 convert "$3" -flatten -resize "$NNN_PREVIEWWIDTH"x"$NNN_PREVIEWHEIGHT"\> "$NNN_PREVIEWDIR/$3.jpg" 444 else 445 image_preview "$1" "$2" "$3" && return 446 fi ;; 447 office) libreoffice --convert-to jpg "$3" --outdir "$NNN_PREVIEWDIR/${3%/*}" 448 filename="$(printf "%s" "${3##*/}" | cut -d. -f1)" 449 mv -- "$NNN_PREVIEWDIR/${3%/*}/$filename.jpg" "$NNN_PREVIEWDIR/$3.jpg" ;; 450 pdf) pdftoppm -jpeg -f 1 -singlefile "$3" "$NNN_PREVIEWDIR/$3" ;; 451 djvu) ddjvu -format=ppm -page=1 "$3" "$NNN_PREVIEWDIR/$3.jpg" ;; 452 video) video_preview "$1" "$2" "$3" && return ;; 453 esac 454 fi 455 if [ -f "$NNN_PREVIEWDIR/$3.jpg" ]; then 456 image_preview "$1" "$2" "$NNN_PREVIEWDIR/$3.jpg" 457 elif [[ "${3##*.}" == "svg" ]] && [ -f "$NNN_PREVIEWDIR/$3.png" ]; then 458 image_preview "$1" "$2" "$NNN_PREVIEWDIR/$3.png" 459 else 460 fifo_pager print_bin_info "$3" 461 fi 462 } >/dev/null 2>&1 463 464 image_preview() { 465 clear 466 exec >/dev/tty 467 if [ "$NNN_TERMINAL" = "kitty" ] && [[ "$NNN_PREVIEWIMGPROG" == +(|icat) ]]; then 468 kitty +kitten icat --silent --scale-up --place "$1"x"$2"@0x0 --transfer-mode=stream --stdin=no "$3" & 469 elif [ "$NNN_TERMINAL" = "tmux" ] && [[ -n "$KITTY_LISTEN_ON" ]] && [[ "$NNN_PREVIEWIMGPROG" == +(|icat) ]]; then 470 kitty +kitten icat --silent --scale-up --place "$(($1 - 1))x$(($2 - 1))"@0x0 --transfer-mode=memory --stdin=no "$3" & 471 elif [ "$NNN_TERMINAL" = "wezterm" ] && [[ "$NNN_PREVIEWIMGPROG" == +(|imgcat) ]]; then 472 wezterm imgcat "$3" & 473 elif exists ueberzug && [[ "$NNN_PREVIEWIMGPROG" == +(|ueberzug) ]]; then 474 ueberzug_layer "$1" "$2" "$3" && return 475 elif exists "${NNN_PREVIEWIMGPROG%% *}"; then # can include command flags; only check first word 476 $NNN_PREVIEWIMGPROG "$3" & 477 else 478 fifo_pager print_bin_info "$3" && return 479 fi 480 printf "%s" "$!" > "$PREVIEWPID" 481 } 482 483 video_preview() { 484 clear 485 exec >/dev/tty 486 if [ -n "$NNN_PREVIEWVIDEO" ]; then 487 mpv --no-config --really-quiet --vo="$NNN_PREVIEWVIDEO" --profile=sw-fast --loop-file --no-audio "$4" "$3" & 488 else 489 ffmpegthumbnailer -m -s0 -i "$3" -o "$NNN_PREVIEWDIR/$3.jpg" || rm -- "$NNN_PREVIEWDIR/$3.jpg" & 490 fi 491 printf "%s" "$!" > "$PREVIEWPID" 492 } 493 494 ueberzug_layer() { 495 [ -f "$POSOFFSET" ] && read -r x y < "$POSOFFSET" 496 printf '{"action": "add", "identifier": "nnn_ueberzug", "x": %d, "y": %d, "width": "%d", "height": "%d", "scaler": "fit_contain", "path": "%s"}\n'\ 497 "${x:-0}" "${y:-0}" "$1" "$2" "$3" > "$FIFO_UEBERZUG" 498 } 499 500 ueberzug_remove() { 501 printf '{"action": "remove", "identifier": "nnn_ueberzug"}\n' > "$FIFO_UEBERZUG" 502 } 503 504 winch_handler() { 505 clear 506 pidkill "$PREVIEWPID" 507 if [ -p "$FIFO_UEBERZUG" ]; then 508 pkill -f "tail --follow $FIFO_UEBERZUG" 509 tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json & 510 fi 511 preview_file "$(cat "$CURSEL")" 512 } 513 514 preview_fifo() { 515 while read -r selection; do 516 if [ -n "$selection" ]; then 517 pidkill "$PREVIEWPID" 518 [ -p "$FIFO_UEBERZUG" ] && ueberzug_remove 519 [ -n "$KITTY_LISTEN_ON" ] && kitty +kitten icat --clear --stdin=no >/dev/tty 2>/dev/null 520 [ "$selection" = "close" ] && break 521 preview_file "$selection" 522 printf "%s" "$selection" > "$CURSEL" 523 fi 524 done < "$NNN_FIFO" 525 sleep 0.1 # make sure potential preview by winch_handler is killed 526 pkill -P "$$" 527 } 528 529 if [ "$PREVIEW_MODE" -eq 1 ] 2>/dev/null; then 530 if exists ueberzug && [ "$NNN_TERMINAL" != "kitty" ] && [[ "$NNN_PREVIEWIMGPROG" == +(|ueberzug) ]]; then 531 mkfifo "$FIFO_UEBERZUG" 532 tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json & 533 fi 534 535 preview_file "$PWD/$1" 536 preview_fifo & WAITPID=$! 537 printf "%s" "$!" > "$FIFOPID" 538 printf "%s" "$PWD/$1" > "$CURSEL" 539 trap 'winch_handler' WINCH 540 trap 'rm -- "$PREVIEWPID" "$CURSEL" "$FIFO_UEBERZUG" "$FIFOPID" "$POSOFFSET" 2>/dev/null' INT HUP EXIT 541 while kill -s 0 $WAITPID; do 542 wait $WAITPID 2>/dev/null 543 done 544 exit 0 545 else 546 if [ ! -r "$NNN_FIFO" ]; then 547 prompt "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')\nPlease read Usage in '$0'." 548 elif [ "$KITTY_WINDOW_ID" ] && [ -z "$TMUX" ] && [ -z "$KITTY_LISTEN_ON" ]; then 549 prompt "\$KITTY_LISTEN_ON not set!\nPlease read Usage in '$0'." 550 else 551 toggle_preview "$1" & 552 fi 553 fi