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