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