sistema_progs

Programas para customizar o meu entorno de traballo nos meus equipos persoais
Log | Files | Refs

preview-tui (19888B)


      1 #!/usr/bin/env sh
      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 5 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 #       - QuickLook on WSL (https://github.com/QL-Win/QuickLook), or
     12 #       - Windows Terminal (https://github.com/Microsoft/Terminal | https://aka.ms/terminal) with WSL, or
     13 #       - $TERMINAL set to a terminal (it's xterm by default).
     14 #   - less or $PAGER
     15 #   - tree or exa or ls
     16 #   - mediainfo or file
     17 #   - mktemp
     18 #   - unzip
     19 #   - tar
     20 #   - man
     21 #   - optional: bsdtar or atool for additional archive preview
     22 #   - optional: bat for code syntax highlighting
     23 #   - optional: ueberzug, kitty terminal, viu or catimg for images
     24 #   - optional: convert(ImageMagick) for playing gif preview
     25 #   - optional: ffmpegthumbnailer for video thumbnails (https://github.com/dirkvdb/ffmpegthumbnailer)
     26 #   - optional: ffmpeg for audio thumbnails
     27 #   - optional: libreoffce for opendocument/officedocument preview
     28 #   - optional: pdftoppm(poppler) for pdf thumbnails
     29 #   - optional: gnome-epub-thumbnailer for epub thumbnails (https://gitlab.gnome.org/GNOME/gnome-epub-thumbnailer)
     30 #   - optional: fontpreview for font preview (https://github.com/sdushantha/fontpreview)
     31 #   - optional: glow or lowdown for markdown
     32 #   - optional: w3m or lynx or elinks for html
     33 #   - optional: set/export ICONLOOKUP as 1 to enable file icons in front of directory previews with .iconlookup
     34 #       Icons and colors are configureable in .iconlookup
     35 #   - optional: scope.sh file viewer from ranger.
     36 #       1. drop scope.sh executable in $PATH
     37 #       2. set/export $USE_SCOPE as 1
     38 #   - optional: pistol file viewer (https://github.com/doronbehar/pistol).
     39 #       1. install pistol
     40 #       2. set/export $USE_PISTOL as 1
     41 #
     42 # Usage:
     43 #   You need to set a NNN_FIFO path and a key for the plugin with NNN_PLUG,
     44 #   then start `nnn`:
     45 #
     46 #     $ nnn -a
     47 #
     48 #   or
     49 #
     50 #     $ NNN_FIFO=/tmp/nnn.fifo nnn
     51 #
     52 #   Then launch the `preview-tui` plugin in `nnn`.
     53 #
     54 #   If you provide the same NNN_FIFO to all nnn instances, there will be a
     55 #   single common preview window. If you provide different FIFO path (e.g.
     56 #   with -a), they will be independent.
     57 #
     58 #   The previews will be shown in a tmux split. If that isn't possible, it
     59 #   will try to use a kitty terminal split. And as a final fallback, a
     60 #   different terminal window will be used ($TERMINAL).
     61 #
     62 #   Tmux and kitty users can configure $SPLIT to either "h" or "v" to set a
     63 #   'h'orizontal split or a 'v'ertical split (as in, the line that splits the
     64 #   windows will be horizontal or vertical).
     65 #
     66 #   Kitty users need `allow_remote_control` set to `yes`, and `listen_on` set
     67 #   to e.g. "unix:$TMPDIR/kitty". To customize the window split, `enabled_layouts`
     68 #   has to be set to `all` or `splits` (the former is the default value).
     69 #   This terminal is also able to show images without extra dependencies.
     70 #
     71 #   Iterm2 users are recommended to use viu to view images without getting pixelated.
     72 #
     73 #   Windows Terminal users can set "Profile termination behavior" under "Profile > Advanced" settings
     74 #   to automaticaly close pane on quit when exit code is 0.
     75 #
     76 # Shell: POSIX compliant
     77 # Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste, Mario Ortiz Manero, Luuk van Baal, @WanderLanz
     78 
     79 #SPLIT="$SPLIT"  # you can set a permanent split here
     80 #TERMINAL="$TERMINAL"  # same goes for the terminal
     81 DEBUG_LOG=0  # set to 1 to enable logging for debug purposes
     82 USE_SCOPE="${USE_SCOPE:-0}"
     83 USE_PISTOL="${USE_PISTOL:-0}"
     84 ICONLOOKUP="${ICONLOOKUP:-0}"
     85 PAGER="${PAGER:-less -P?n -R}"
     86 TMPDIR="${TMPDIR:-/tmp}"
     87 BAT_STYLE="${BAT_STYLE:-numbers}"
     88 BAT_THEME="${BAT_THEME:-ansi}"
     89 # Consider setting NNN_PREVIEWDIR to $XDG_CACHE_HOME/nnn/previews if you want to keep previews on disk between reboots
     90 NNN_PREVIEWDIR="${NNN_PREVIEWDIR:-$TMPDIR/nnn/previews}"
     91 NNN_PREVIEWWIDTH="${NNN_PREVIEWWIDTH:-1920}"
     92 NNN_PREVIEWHEIGHT="${NNN_PREVIEWHEIGHT:-1080}"
     93 NNN_PARENT="${NNN_FIFO#*.}"
     94 [ "$NNN_PARENT" -eq "$NNN_PARENT" ] 2>/dev/null || NNN_PARENT=""
     95 FIFOPID="$TMPDIR/nnn-preview-tui-fifopid.$NNN_PARENT"
     96 PREVIEWPID="$TMPDIR/nnn-preview-tui-pagerpid.$NNN_PARENT"
     97 CURSEL="$TMPDIR/nnn-preview-tui-selection.$NNN_PARENT"
     98 FIFO_UEBERZUG="$TMPDIR/nnn-preview-tui-ueberzug-fifo.$NNN_PARENT"
     99 
    100 if [ "$DEBUG_LOG" -eq 0 ]; then
    101     DEBUG_LOGFILE="/dev/null"
    102 else
    103     DEBUG_LOGFILE="${TMPDIR}/preview-tui-log"
    104 fi
    105 
    106 start_preview() {
    107     [ "$PAGER" = "most" ] && PAGER="less -R"
    108 
    109     if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.'; then
    110         TERMINAL=tmux
    111     elif [ -n "$KITTY_LISTEN_ON" ]; then
    112         TERMINAL=kitty
    113     elif [ -z "$TERMINAL" ] && [ "$TERM_PROGRAM" = "iTerm.app" ]; then
    114         TERMINAL=iterm
    115     elif [ -n "$WT_SESSION" ]; then
    116         TERMINAL=winterm
    117     else
    118         TERMINAL="${TERMINAL:-xterm}"
    119     fi
    120 
    121     if [ -z "$SPLIT" ] && [ $(($(tput lines <"$TTY") * 2)) -gt "$(tput cols <"$TTY")" ]; then
    122         SPLIT='h'
    123     elif [ "$SPLIT" != 'h' ]; then
    124         SPLIT='v'
    125     fi
    126 
    127     case "$TERMINAL" in
    128         tmux) # tmux splits are inverted
    129             if [ "$SPLIT" = "v" ]; then DSPLIT="h"; else DSPLIT="v"; fi
    130             tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1" -e TTY="$TTY" \
    131                 -e "CURSEL=$CURSEL" -e "TMPDIR=$TMPDIR" -e "FIFOPID=$FIFOPID" \
    132                 -e "BAT_STYLE=$BAT_STYLE" -e "BAT_THEME=$BAT_THEME" -e "PREVIEWPID=$PREVIEWPID" \
    133                 -e "PAGER=$PAGER" -e "ICONLOOKUP=$ICONLOOKUP" -e "NNN_PREVIEWWIDTH=$NNN_PREVIEWWIDTH" \
    134                 -e "USE_SCOPE=$USE_SCOPE" -e "SPLIT=$SPLIT" -e "USE_PISTOL=$USE_PISTOL" \
    135                 -e "NNN_PREVIEWDIR=$NNN_PREVIEWDIR" -e "NNN_PREVIEWHEIGHT=$NNN_PREVIEWHEIGHT" \
    136                 -e "FIFO_UEBERZUG=$FIFO_UEBERZUG" -e "QLPATH=$2" -d"$DSPLIT" "$0" "$1" ;;
    137         kitty) # Setting the layout for the new window. It will be restored after the script ends.
    138             kitty @ goto-layout splits
    139             # Trying to use kitty's integrated window management as the split window. All
    140             # environmental variables that will be used in the new window must be explicitly passed.
    141             kitty @ launch --no-response --title "nnn preview" --keep-focus \
    142                 --cwd "$PWD" --env "PATH=$PATH" --env "NNN_FIFO=$NNN_FIFO" \
    143                 --env "PREVIEW_MODE=1" --env "PAGER=$PAGER" --env "TMPDIR=$TMPDIR" \
    144                 --env "USE_SCOPE=$USE_SCOPE" --env "SPLIT=$SPLIT" --env "TERMINAL=$TERMINAL"\
    145                 --env "PREVIEWPID=$PREVIEWPID" --env "FIFO_UEBERZUG=$FIFO_UEBERZUG" \
    146                 --env "ICONLOOKUP=$ICONLOOKUP" --env "NNN_PREVIEWHEIGHT=$NNN_PREVIEWHEIGHT" \
    147                 --env "NNN_PREVIEWWIDTH=$NNN_PREVIEWWIDTH" --env "NNN_PREVIEWDIR=$NNN_PREVIEWDIR" \
    148                 --env "USE_PISTOL=$USE_PISTOL" --env "BAT_STYLE=$BAT_STYLE" \
    149                 --env "BAT_THEME=$BAT_THEME" --env "FIFOPID=$FIFOPID" --env TTY="$TTY" \
    150                 --env "CURSEL=$CURSEL" --location "${SPLIT}split" "$0" "$1" ;;
    151         iterm)
    152             command="$SHELL -c 'cd $PWD; \
    153                 PATH=\\\"$PATH\\\" NNN_FIFO=\\\"$NNN_FIFO\\\" PREVIEW_MODE=1 PAGER=\\\"$PAGER\\\" \
    154                 USE_SCOPE=\\\"$USE_SCOPE\\\" SPLIT=\\\"$SPLIT\\\" TERMINAL=\\\"$TERMINAL\\\" \
    155                 PREVIEWPID=\\\"$PREVIEWPID\\\" CURSEL=\\\"$CURSEL\\\" TMPDIR=\\\"$TMPDIR\\\" \
    156                 ICONLOOKUP=\\\"$ICONLOOKUP\\\" NNN_PREVIEWHEIGHT=\\\"$NNN_PREVIEWHEIGHT\\\" \
    157                 NNN_PREVIEWWIDTH=\\\"$NNN_PREVIEWWIDTH\\\" NNN_PREVIEWDIR=\\\"$NNN_PREVIEWDIR\\\" \
    158                 USE_PISTOL=\\\"$USE_PISTOL\\\" BAT_STYLE=\\\"$BAT_STYLE\\\" TTY=\\\"$TTY\\\" \
    159                 BAT_THEME=\\\"$BAT_THEME\\\" FIFOPID=\\\"$FIFOPID\\\" \\\"$0\\\" \\\"$1\\\"'"
    160             if [ "$SPLIT" = "h" ]; then split="horizontally"; else split="vertically"; fi
    161             osascript <<-EOF
    162             tell application "iTerm"
    163                 tell current session of current window
    164                     split $split with default profile command "$command"
    165                 end tell
    166             end tell
    167 EOF
    168             ;;
    169         winterm)
    170             if [ "$SPLIT" = "h" ]; then split="H"; else split="V"; fi
    171             cmd.exe /c wt -w 0 sp -$split bash -c "cd $PWD \; PATH='$PATH' NNN_FIFO=$NNN_FIFO \
    172                 PREVIEW_MODE=1 TTY=$TTY CURSEL=$CURSEL TMPDIR=$TMPDIR FIFOPID=$FIFOPID \
    173                 BAT_STYLE=$BAT_STYLE BAT_THEME=$BAT_THEME PREVIEWPID=$PREVIEWPID \
    174                 PAGER='$PAGER' ICONLOOKUP=$ICONLOOKUP NNN_PREVIEWWIDTH=$NNN_PREVIEWWIDTH \
    175                 USE_SCOPE=$USE_SCOPE SPLIT=$SPLIT USE_PISTOL=$USE_PISTOL \
    176                 NNN_PREVIEWDIR=$NNN_PREVIEWDIR NNN_PREVIEWHEIGHT=$NNN_PREVIEWHEIGHT \
    177                 FIFO_UEBERZUG=$FIFO_UEBERZUG QLPATH=$2 $0 $1" \; -w 0 mf previous
    178             ;;
    179         *)  if [ -n "$2" ]; then
    180                 QUICKLOOK=1 QLPATH="$2" PREVIEW_MODE=1 "$0" "$1" &
    181             else
    182                 PREVIEWPID="$PREVIEWPID" CURSEL="$CURSEL" PREVIEW_MODE=1 TTY="$TTY" \
    183                      FIFOPID="$FIFOPID" FIFO_UEBERZUG="$FIFO_UEBERZUG" $TERMINAL -e "$0" "$1" &
    184             fi ;;
    185     esac
    186 } >"$DEBUG_LOGFILE" 2>&1
    187 
    188 toggle_preview() {
    189     if exists QuickLook.exe; then
    190         QLPATH="QuickLook.exe"
    191     elif exists Bridge.exe; then
    192         QLPATH="Bridge.exe"
    193     fi
    194     if kill "$(cat "$FIFOPID")"; then
    195         [ -p "$NNN_PPIPE" ] && printf "0" > "$NNN_PPIPE"
    196         kill "$(cat "$PREVIEWPID")"
    197         pkill -f "tail --follow $FIFO_UEBERZUG"
    198         if [ -n "$QLPATH" ] && stat "$1"; then
    199             f="$(wslpath -w "$1")" && "$QLPATH" "$f" &
    200         fi
    201     else
    202         [ -p "$NNN_PPIPE" ] && printf "1" > "$NNN_PPIPE"
    203         start_preview "$1" "$QLPATH"
    204     fi
    205 } >"$DEBUG_LOGFILE" 2>&1
    206 
    207 exists() {
    208     type "$1" >/dev/null
    209 }
    210 
    211 fifo_pager() {
    212     cmd="$1"
    213     shift
    214 
    215     # We use a FIFO to access $PAGER PID in jobs control
    216     tmpfifopath="$TMPDIR/nnn-preview-tui-fifo.$$"
    217     mkfifo "$tmpfifopath" || return
    218 
    219     $PAGER < "$tmpfifopath" &
    220     printf "%s" "$!" > "$PREVIEWPID"
    221 
    222     (
    223         exec > "$tmpfifopath"
    224         if [ "$cmd" = "pager" ]; then
    225             if exists bat; then
    226                 bat --terminal-width="$(tput cols <"$TTY")" --decorations=always --color=always \
    227                     --paging=never --style="$BAT_STYLE" --theme="$BAT_THEME" "$@" &
    228             else
    229                 $PAGER "$@" &
    230             fi
    231         else
    232             "$cmd" "$@" &
    233         fi
    234     )
    235 
    236     rm "$tmpfifopath"
    237 } 2>"$DEBUG_LOGFILE"
    238 
    239 # Binary file: show file info inside the pager
    240 print_bin_info() {
    241     printf -- "-------- \033[1;31mBinary file\033[0m --------\n"
    242     if exists mediainfo; then
    243         mediainfo "$1"
    244     else
    245         file -b "$1"
    246     fi
    247 } 2>"$DEBUG_LOGFILE"
    248 
    249 handle_mime() {
    250     case "$2" in
    251         image/jpeg) image_preview "$cols" "$lines" "$1" ;;
    252         image/gif) generate_preview "$cols" "$lines" "$1" "gif" ;;
    253         image/*) generate_preview "$cols" "$lines" "$1" "image" ;;
    254         video/*) generate_preview "$cols" "$lines" "$1" "video" ;;
    255         audio/*) generate_preview "$cols" "$lines" "$1" "audio" ;;
    256         application/font*|application/*opentype|font/*) generate_preview "$cols" "$lines" "$1" "font" ;;
    257         */*office*|*/*document*) generate_preview "$cols" "$lines" "$1" "office" ;;
    258         application/zip) fifo_pager unzip -l "$1" ;;
    259         text/troff)
    260             if exists man; then
    261                 fifo_pager man -Pcat -l "$1"
    262             else
    263                 fifo_pager pager "$1"
    264             fi ;;
    265         *) handle_ext "$1" "$3" "$4" ;;
    266     esac
    267 }
    268 
    269 handle_ext() {
    270     case "$2" in
    271         epub) generate_preview "$cols" "$lines" "$1" "epub" ;;
    272         pdf) generate_preview "$cols" "$lines" "$1" "pdf" ;;
    273         gz|bz2) fifo_pager tar -tvf "$1" ;;
    274         md) if exists glow; then
    275                 fifo_pager glow -s dark "$1"
    276             elif exists lowdown; then
    277                 fifo_pager lowdown -Tterm "$1"
    278             else
    279                 fifo_pager pager "$1"
    280             fi ;;
    281         htm|html|xhtml)
    282             if exists w3m; then
    283                 fifo_pager w3m "$1"
    284             elif exists lynx; then
    285                 fifo_pager lynx "$1"
    286             elif exists elinks; then
    287                 fifo_pager elinks "$1"
    288             else
    289                 fifo_pager pager "$1"
    290             fi ;;
    291         7z|a|ace|alz|arc|arj|bz|cab|cpio|deb|jar|lha|lz|lzh|lzma|lzo\
    292         |rar|rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z)
    293             if exists atool; then
    294                 fifo_pager atool -l "$1"
    295             elif exists bsdtar; then
    296                 fifo_pager bsdtar -tvf "$1"
    297             fi ;;
    298         *) if [ "$3" = "bin" ]; then
    299                fifo_pager print_bin_info "$1"
    300            else
    301                fifo_pager pager "$1"
    302            fi ;;
    303     esac
    304 }
    305 
    306 preview_file() {
    307     clear
    308     # Trying to use pistol if it's available.
    309     if [ "$USE_PISTOL" -ne 0 ] && exists pistol; then
    310         fifo_pager pistol "$1"
    311         return
    312     fi
    313 
    314     # Trying to use scope.sh if it's available.
    315     if [ "$USE_SCOPE" -ne 0 ] && exists scope.sh; then
    316         fifo_pager scope.sh "$1" "$cols" "$lines" "$(mktemp -d)" "True"
    317         return
    318     fi
    319 
    320     # Use QuickLook if it's available.
    321     if [ -n "$QUICKLOOK" ]; then
    322         stat "$1" && f="$(wslpath -w "$1")" && "$QLPATH" "$f" &
    323         return
    324     fi
    325 
    326     # Detecting the exact type of the file: the encoding, mime type, and extension in lowercase.
    327     encoding="$(file -bL --mime-encoding -- "$1")"
    328     mimetype="$(file -bL --mime-type -- "$1")"
    329     ext="${1##*.}"
    330     [ -n "$ext" ] && ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
    331     lines=$(tput lines <"$TTY")
    332     cols=$(tput cols <"$TTY")
    333 
    334     # Otherwise, falling back to the defaults.
    335     if [ -d "$1" ]; then
    336         cd "$1" || return
    337         if [ "$ICONLOOKUP" -ne 0 ] && [ -f "$(dirname "$0")"/.iconlookup ]; then
    338             [ "$SPLIT" = v ] && BSTR="\n"
    339             # shellcheck disable=SC2012
    340             ls -F --group-directories-first | head -n "$((lines - 3))" | "$(dirname "$0")"/.iconlookup -l "$cols" -B "$BSTR" -b " "
    341         elif exists tree; then
    342             fifo_pager tree --filelimit "$(find . -maxdepth 1 | wc -l)" -L 3 -C -F --dirsfirst --noreport
    343         elif exists exa; then
    344             exa -G --group-directories-first --colour=always
    345         else
    346             fifo_pager ls -F --group-directories-first --color=always
    347         fi
    348     elif [ "${encoding#*)}" = "binary" ]; then
    349         handle_mime "$1" "$mimetype" "$ext" "bin"
    350     else
    351         handle_mime "$1" "$mimetype" "$ext"
    352     fi
    353 } 2>"$DEBUG_LOGFILE"
    354 
    355 generate_preview() {
    356   if [ -n "$QLPATH" ] && stat "$3"; then
    357         f="$(wslpath -w "$3")" && "$QLPATH" "$f" &
    358   elif [ ! -f "$NNN_PREVIEWDIR/$3.jpg" ] || [ -n "$(find -L "$3" -newer "$NNN_PREVIEWDIR/$3.jpg")" ]; then
    359         mkdir -p "$NNN_PREVIEWDIR/${3%/*}"
    360         case $4 in
    361             audio) ffmpeg -i "$3" -filter_complex "scale=iw*min(1\,min($NNN_PREVIEWWIDTH/iw\,ih)):-1" "$NNN_PREVIEWDIR/$3.jpg" -y ;;
    362             epub) gnome-epub-thumbnailer "$3" "$NNN_PREVIEWDIR/$3.jpg" ;;
    363             font) fontpreview -i "$3" -o "$NNN_PREVIEWDIR/$3.jpg" ;;
    364             gif) if [ -p "$FIFO_UEBERZUG" ] && exists convert; then
    365                     frameprefix="$NNN_PREVIEWDIR/$3/${3##*/}"
    366                     if [ ! -d "$NNN_PREVIEWDIR/$3" ]; then
    367                         mkdir -p "$NNN_PREVIEWDIR/$3"
    368                         convert -coalesce -resize "$NNN_PREVIEWWIDTH"x"$NNN_PREVIEWHEIGHT"\> "$3" "$frameprefix.jpg" ||
    369                         MAGICK_TMPDIR="/tmp" convert -coalesce -resize "$NNN_PREVIEWWIDTH"x"$NNN_PREVIEWHEIGHT"\> "$3" "$frameprefix.jpg"
    370                     fi
    371                     frames=$(($(find "$NNN_PREVIEWDIR/$3" | wc -l) - 2))
    372                     [ $frames -lt 0 ] && return
    373                     while true; do
    374                         for i in $(seq 0 $frames); do
    375                             image_preview "$1" "$2" "$frameprefix-$i.jpg"
    376                             sleep 0.1
    377                         done
    378                     done &
    379                     printf "%s" "$!" > "$PREVIEWPID"
    380                     return
    381                  else
    382                     exec >/dev/tty
    383                     image_preview "$1" "$2" "$3"
    384                     return
    385                  fi ;;
    386             image) if exists convert; then
    387                        convert "$3" -flatten -resize "$NNN_PREVIEWWIDTH"x"$NNN_PREVIEWHEIGHT"\> "$NNN_PREVIEWDIR/$3.jpg"
    388                    else
    389                        image_preview "$1" "$2" "$3" && return
    390                    fi ;;
    391             office) libreoffice --convert-to jpg "$3" --outdir "$NNN_PREVIEWDIR/${3%/*}"
    392                     filename="$(printf "%s" "${3##*/}" | cut -d. -f1)"
    393                     mv "$NNN_PREVIEWDIR/${3%/*}/$filename.jpg" "$NNN_PREVIEWDIR/$3.jpg" ;;
    394             pdf) pdftoppm -jpeg -f 1 -singlefile "$3" "$NNN_PREVIEWDIR/$3" ;;
    395             video) ffmpegthumbnailer -s0 -i "$3" -o "$NNN_PREVIEWDIR/$3.jpg" || rm "$NNN_PREVIEWDIR/$3.jpg" ;;
    396         esac
    397     fi >"$DEBUG_LOGFILE"
    398     if [ -f "$NNN_PREVIEWDIR/$3.jpg" ]; then
    399         image_preview "$1" "$2" "$NNN_PREVIEWDIR/$3.jpg"
    400     else
    401         fifo_pager print_bin_info "$3"
    402     fi
    403 } 2>"$DEBUG_LOGFILE"
    404 
    405 image_preview() {
    406     clear
    407     if [ "$TERMINAL" = "kitty" ]; then
    408         # Kitty terminal users can use the native image preview method
    409         kitty +kitten icat --silent --place "$1"x"$2"@0x0 --transfer-mode=stream --stdin=no "$3" &
    410     elif exists ueberzug; then
    411         ueberzug_layer "$1" "$2" "$3" && return
    412     elif exists catimg; then
    413         catimg "$3" &
    414     elif exists viu; then
    415         viu -t "$3" &
    416     else
    417         fifo_pager print_bin_info "$3" && return
    418     fi
    419     printf "%s" "$!" > "$PREVIEWPID"
    420 } 2>"$DEBUG_LOGFILE"
    421 
    422 ueberzug_layer() {
    423     printf '{"action": "add", "identifier": "nnn_ueberzug", "x": 0, "y": 0, "width": "%d", "height": "%d", "scaler": "fit_contain", "path": "%s"}\n' "$1" "$2" "$3" > "$FIFO_UEBERZUG"
    424 }
    425 
    426 ueberzug_remove() {
    427     printf '{"action": "remove", "identifier": "nnn_ueberzug"}\n' > "$FIFO_UEBERZUG"
    428 }
    429 
    430 winch_handler() {
    431     clear
    432     kill "$(cat "$PREVIEWPID")"
    433     if [ -p "$FIFO_UEBERZUG" ]; then
    434         pkill -f "tail --follow $FIFO_UEBERZUG"
    435         tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
    436     fi
    437     preview_file "$(cat "$CURSEL")"
    438 } 2>"$DEBUG_LOGFILE"
    439 
    440 preview_fifo() {
    441     while read -r selection; do
    442         if [ -n "$selection" ]; then
    443             kill "$(cat "$PREVIEWPID")"
    444             [ -p "$FIFO_UEBERZUG" ] && ueberzug_remove
    445             [ "$selection" = "close" ] && break
    446             preview_file "$selection"
    447             printf "%s" "$selection" > "$CURSEL"
    448         fi
    449     done < "$NNN_FIFO"
    450     sleep 0.1 # make sure potential preview by winch_handler is killed
    451     pkill -P "$$"
    452 } 2>"$DEBUG_LOGFILE"
    453 
    454 if [ "$PREVIEW_MODE" ]; then
    455     if [ "$TERMINAL" != "kitty" ] && exists ueberzug; then
    456         mkfifo "$FIFO_UEBERZUG"
    457         tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
    458     fi
    459 
    460     preview_file "$PWD/$1"
    461     preview_fifo &
    462     printf "%s" "$!" > "$FIFOPID"
    463     printf "%s" "$PWD/$1" > "$CURSEL"
    464     trap 'winch_handler; wait' WINCH
    465     trap 'rm "$PREVIEWPID" "$CURSEL" "$FIFO_UEBERZUG" "$FIFOPID" 2>/dev/null' INT HUP EXIT
    466     wait "$!" 2>/dev/null
    467     exit 0
    468 else
    469     if [ ! -r "$NNN_FIFO" ]; then
    470         clear
    471         printf "No FIFO available! (\$NNN_FIFO='%s')\nPlease read Usage in preview-tui." "$NNN_FIFO"
    472         cfg=$(stty -g); stty raw -echo; head -c 1; stty "$cfg"
    473     elif [ "$KITTY_WINDOW_ID" ] && [ -z "$TMUX" ] && [ -z "$KITTY_LISTEN_ON" ]; then
    474         clear
    475         printf "\$KITTY_LISTEN_ON not set!\nPlease read Usage in preview-tui."
    476         cfg=$(stty -g); stty raw -echo; head -c 1; stty "$cfg"
    477     else
    478         TTY="$(tty)"
    479         TTY="$TTY" toggle_preview "$1" &
    480     fi
    481 fi