preview-tui: mpv kitty backend and better previewer handling

This commit is contained in:
Luuk van Baal 2023-11-11 17:53:20 +01:00
parent 9ff81ca1f9
commit 3160442f0c
No known key found for this signature in database
GPG Key ID: E7D1BC845D39FA83
1 changed files with 48 additions and 43 deletions

View File

@ -21,8 +21,12 @@
# - man # - man
# - optional: bsdtar or atool for additional archive preview # - optional: bsdtar or atool for additional archive preview
# - optional: bat for code syntax highlighting # - optional: bat for code syntax highlighting
# - optional: ueberzug, kitty terminal, wezterm terminal, viu, catimg or chafa for images # - optional: ueberzug, kitty terminal, wezterm terminal, img2sixel, viu, catimg or chafa for images
# - optional: convert(ImageMagick) for playing gif preview (required for kitty image previews) # - optional: convert(ImageMagick) for playing gif preview (mandatory for kitty image previews)
# - optional: mpv for gif and video
# Also requires a terminal supporting the sixel (https://www.arewesixelyet.com/)
# or kitty (https://sw.kovidgoyal.net/kitty/graphics-protocol) video_output backends.
# Requires tmux compiled with `./configure --enable-sixel` if used.
# - optional: ffmpegthumbnailer for video thumbnails (https://github.com/dirkvdb/ffmpegthumbnailer) # - optional: ffmpegthumbnailer for video thumbnails (https://github.com/dirkvdb/ffmpegthumbnailer)
# - optional: ffmpeg for audio thumbnails # - optional: ffmpeg for audio thumbnails
# - optional: libreoffce for opendocument/officedocument preview # - optional: libreoffce for opendocument/officedocument preview
@ -40,11 +44,6 @@
# - optional: pistol file viewer (https://github.com/doronbehar/pistol). # - optional: pistol file viewer (https://github.com/doronbehar/pistol).
# 1. install pistol # 1. install pistol
# 2. set/export $NNN_PISTOL as 1 # 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: # Usage:
# You need to set a NNN_FIFO path and a key for the plugin with NNN_PLUG, # You need to set a NNN_FIFO path and a key for the plugin with NNN_PLUG,
@ -91,26 +90,31 @@
# Shell: Bash (for environment manipulation through arrays) # Shell: Bash (for environment manipulation through arrays)
# Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste, Mario Ortiz Manero, Luuk van Baal, @WanderLanz # Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste, Mario Ortiz Manero, Luuk van Baal, @WanderLanz
NNN_SPLIT=${NNN_SPLIT:-} # Set permanent split direction # Configurable environment variables:
NNN_TERMINAL=${NNN_TERMINAL:-} # Set external terminal to be used NNN_SPLIT=${NNN_SPLIT:-} # permanent split direction
NNN_SPLITSIZE=${NNN_SPLITSIZE:-50} # Set previewer split size percentage NNN_TERMINAL=${NNN_TERMINAL:-} # external terminal to be used
TMPDIR=${TMPDIR:-/tmp} NNN_SPLITSIZE=${NNN_SPLITSIZE:-50} # previewer split size percentage
TMPDIR=${TMPDIR:-/tmp} # location of temporary files
ENVVARS=(
"NNN_SCOPE=${NNN_SCOPE:-0}" # use scope
"NNN_PISTOL=${NNN_PISTOL:-0}" # use pistol
"NNN_ICONLOOKUP=${NNN_ICONLOOKUP:-0}" # use .iconlookup
"NNN_PAGER=${NNN_PAGER:-less -P?n -R -C}" # pager options
"NNN_BATTHEME=${NNN_BATTHEME:-ansi}" # bat theme
"NNN_BATSTYLE=${NNN_BATSTYLE:-numbers}" # bat style
"NNN_PREVIEWWIDTH=${NNN_PREVIEWWIDTH:-1920}" # width of generated preview images
"NNN_PREVIEWHEIGHT=${NNN_PREVIEWHEIGHT:-1080}" # height of generated preview images
"NNN_PREVIEWDIR=${NNN_PREVIEWDIR:-$TMPDIR/nnn/previews}" # location of generated preview images
"NNN_PREVIEWIMGPROG=${NNN_PREVIEWIMGPROG:-}" # program used to preview images
"NNN_PREVIEWVIDEO=${NNN_PREVIEWVIDEO:-}" # mpv backend used to preview video
)
# Non-configurable environment variables
NNN_PARENT=${NNN_FIFO#*.} NNN_PARENT=${NNN_FIFO#*.}
[ "$NNN_PARENT" -eq "$NNN_PARENT" ] 2>/dev/null || NNN_PARENT="" # Make empty if non-numeric [ "$NNN_PARENT" -eq "$NNN_PARENT" ] 2>/dev/null || NNN_PARENT="" # Make empty if non-numeric
ENVVARS=( ENVVARS+=(
"PWD=$PWD" "PWD=$PWD"
"PATH=$PATH" "PATH=$PATH"
"NNN_FIFO=$NNN_FIFO" "NNN_FIFO=$NNN_FIFO"
"NNN_SCOPE=${NNN_SCOPE:-0}"
"NNN_PISTOL=${NNN_PISTOL:-0}"
"NNN_ICONLOOKUP=${NNN_ICONLOOKUP:-0}"
"NNN_PAGER=${NNN_PAGER:-less -P?n -R -C}"
"NNN_BATTHEME=${NNN_BATTHEME:-ansi}"
"NNN_BATSTYLE=${NNN_BATSTYLE:-numbers}"
"NNN_PREVIEWWIDTH=${NNN_PREVIEWWIDTH:-1920}"
"NNN_PREVIEWHEIGHT=${NNN_PREVIEWHEIGHT:-1080}"
"NNN_PREVIEWDIR=${NNN_PREVIEWDIR:-$TMPDIR/nnn/previews}"
"NNN_PREVIEWIMGPROG=${NNN_PREVIEWIMGPROG:-}"
"FIFOPID=$TMPDIR/nnn-preview-tui-fifopid.$NNN_PARENT" "FIFOPID=$TMPDIR/nnn-preview-tui-fifopid.$NNN_PARENT"
"FIFOPATH=$TMPDIR/nnn-preview-tui-fifo.$NNN_PARENT" "FIFOPATH=$TMPDIR/nnn-preview-tui-fifo.$NNN_PARENT"
"PREVIEWPID=$TMPDIR/nnn-preview-tui-previewpid.$NNN_PARENT" "PREVIEWPID=$TMPDIR/nnn-preview-tui-previewpid.$NNN_PARENT"
@ -137,12 +141,16 @@ pidkill() {
start_preview() { start_preview() {
if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.'; then if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.'; then
NNN_TERMINAL=tmux NNN_TERMINAL=tmux
exists mpv && tmux display -p '#{client_termfeatures}' | grep -q 'sixel' && ENVVARS+=("NNN_PREVIEWVIDEO=sixel")
elif [ -n "$KITTY_LISTEN_ON" ]; then elif [ -n "$KITTY_LISTEN_ON" ]; then
NNN_TERMINAL=kitty NNN_TERMINAL=kitty
exists mpv && ENVVARS+=("NNN_PREVIEWVIDEO=kitty")
elif [ -n "$WEZTERM_PANE" ]; then elif [ -n "$WEZTERM_PANE" ]; then
NNN_TERMINAL=wezterm NNN_TERMINAL=wezterm
exists mpv && ENVVARS+=("NNN_PREVIEWVIDEO=kitty")
elif [ -z "$NNN_TERMINAL" ] && [ "$TERM_PROGRAM" = "iTerm.app" ]; then elif [ -z "$NNN_TERMINAL" ] && [ "$TERM_PROGRAM" = "iTerm.app" ]; then
NNN_TERMINAL=iterm NNN_TERMINAL=iterm
exists mpv && ENVVARS+=("NNN_PREVIEWVIDEO=sixel")
elif [ -n "$WT_SESSION" ]; then elif [ -n "$WT_SESSION" ]; then
NNN_TERMINAL=winterm NNN_TERMINAL=winterm
else else
@ -157,7 +165,7 @@ start_preview() {
ENVVARS+=("NNN_TERMINAL=$NNN_TERMINAL" "NNN_SPLIT=$NNN_SPLIT" "QLPATH=$2" "PREVIEW_MODE=1") ENVVARS+=("NNN_TERMINAL=$NNN_TERMINAL" "NNN_SPLIT=$NNN_SPLIT" "QLPATH=$2" "PREVIEW_MODE=1")
case "$NNN_TERMINAL" in case "$NNN_TERMINAL" in
iterm|winterm) # need run in separate shell command: escape iterm|winterm) # has to run in separate shell command: escape
ENVVARS=("${ENVVARS[@]/#/\\\"}") ENVVARS=("${ENVVARS[@]/#/\\\"}")
ENVVARS=("${ENVVARS[@]/%/\\\"}") ENVVARS=("${ENVVARS[@]/%/\\\"}")
command="$SHELL -c 'env ${ENVVARS[*]} \\\"$0\\\" \\\"$1\\\"'" ;; command="$SHELL -c 'env ${ENVVARS[*]} \\\"$0\\\" \\\"$1\\\"'" ;;
@ -371,6 +379,9 @@ preview_file() {
generate_preview() { generate_preview() {
if [ -n "$QLPATH" ] && stat "$3"; then if [ -n "$QLPATH" ] && stat "$3"; then
f="$(wslpath -w "$3")" && "$QLPATH" "$f" & f="$(wslpath -w "$3")" && "$QLPATH" "$f" &
elif [ -n "$NNN_PREVIEWVIDEO" ] && [[ "$4" == +(gif|video) ]]; then
[ "$4" = "video" ] && args=(--start=10% --length=4) || args=()
video_preview "$1" "$2" "$3" "${args[@]}" && return
elif [ ! -f "$NNN_PREVIEWDIR/$3.jpg" ] || [ -n "$(find -L "$3" -newer "$NNN_PREVIEWDIR/$3.jpg")" ]; then elif [ ! -f "$NNN_PREVIEWDIR/$3.jpg" ] || [ -n "$(find -L "$3" -newer "$NNN_PREVIEWDIR/$3.jpg")" ]; then
mkdir -p "$NNN_PREVIEWDIR/${3%/*}" mkdir -p "$NNN_PREVIEWDIR/${3%/*}"
case $4 in case $4 in
@ -394,12 +405,10 @@ generate_preview() {
done & done &
printf "%s" "$!" > "$PREVIEWPID" printf "%s" "$!" > "$PREVIEWPID"
return return
elif exists mpv; then elif [ -n "$NNN_PREVIEWVIDEO" ]; then
video_preview "$1" "$2" "$3" 1 video_preview "$1" "$2" "$3" && return
return
else else
image_preview "$1" "$2" "$3" image_preview "$1" "$2" "$3" && return
return
fi ;; fi ;;
image) if exists convert; then image) if exists convert; then
convert "$3" -flatten -resize "$NNN_PREVIEWWIDTH"x"$NNN_PREVIEWHEIGHT"\> "$NNN_PREVIEWDIR/$3.jpg" convert "$3" -flatten -resize "$NNN_PREVIEWWIDTH"x"$NNN_PREVIEWHEIGHT"\> "$NNN_PREVIEWDIR/$3.jpg"
@ -424,20 +433,20 @@ generate_preview() {
image_preview() { image_preview() {
clear clear
exec >/dev/tty exec >/dev/tty
if [ "$NNN_TERMINAL" = "kitty" ] && [ -z "$NNN_PREVIEWIMGPROG" ]; then if [ "$NNN_TERMINAL" = "kitty" ] && [[ "$NNN_PREVIEWIMGPROG" == +(|icat) ]]; then
kitty +kitten icat --silent --scale-up --place "$1"x"$2"@0x0 --transfer-mode=stream --stdin=no "$3" & kitty +kitten icat --silent --scale-up --place "$1"x"$2"@0x0 --transfer-mode=stream --stdin=no "$3" &
elif [ "$NNN_TERMINAL" = "wezterm" ] && [ -z "$NNN_PREVIEWIMGPROG" ]; then elif [ "$NNN_TERMINAL" = "wezterm" ] && [[ "$NNN_PREVIEWIMGPROG" == +(|imgcat) ]]; then
wezterm imgcat "$3" & wezterm imgcat "$3" &
elif exists ueberzug && { [ -z "$NNN_PREVIEWIMGPROG" ] || [ "$NNN_PREVIEWIMGPROG" = "ueberzug" ] ;}; then elif exists ueberzug && [[ "$NNN_PREVIEWIMGPROG" == +(|ueberzug) ]]; then
ueberzug_layer "$1" "$2" "$3" && return ueberzug_layer "$1" "$2" "$3" && return
elif exists catimg && { [ -z "$NNN_PREVIEWIMGPROG" ] || [ "$NNN_PREVIEWIMGPROG" = "catimg" ] ;}; then elif exists catimg && [[ "$NNN_PREVIEWIMGPROG" == +(|catimg) ]]; then
catimg "$3" & catimg "$3" &
elif exists viu && { [ -z "$NNN_PREVIEWIMGPROG" ] || [ "$NNN_PREVIEWIMGPROG" = "viu" ] ;}; then elif exists viu && [[ "$NNN_PREVIEWIMGPROG" == +(|viu) ]]; then
viu -t "$3" & viu -t "$3" &
elif exists chafa && { [ -z "$NNN_PREVIEWIMGPROG" ] || [ "$NNN_PREVIEWIMGPROG" = "chafa" ] ;}; then elif exists chafa && [[ "$NNN_PREVIEWIMGPROG" == +(|chafa) ]]; then
chafa "$3" & chafa "$3" &
elif exists img2sixel && { [ -z "$NNN_PREVIEWIMGPROG" ] || [ "$NNN_PREVIEWIMGPROG" = "img2sixel" ] ;}; then elif exists img2sixel && [[ "$NNN_PREVIEWIMGPROG" == +(|img2sixel) ]]; then
img2sixel -p16 -g "$3" & img2sixel -g "$3" &
else else
fifo_pager print_bin_info "$3" && return fifo_pager print_bin_info "$3" && return
fi fi
@ -447,12 +456,8 @@ image_preview() {
video_preview() { video_preview() {
clear clear
exec >/dev/tty exec >/dev/tty
if type mpv >/dev/null 2>&1 ; then if [ -n "$NNN_PREVIEWVIDEO" ]; then
if [ "$4" -eq 1 ]; then # not gif mpv --no-config --really-quiet --vo="$NNN_PREVIEWVIDEO" --profile=sw-fast --loop-file --no-audio "$4" "$3" &
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 else
ffmpegthumbnailer -m -s0 -i "$3" -o "$NNN_PREVIEWDIR/$3.jpg" || rm "$NNN_PREVIEWDIR/$3.jpg" & ffmpegthumbnailer -m -s0 -i "$3" -o "$NNN_PREVIEWDIR/$3.jpg" || rm "$NNN_PREVIEWDIR/$3.jpg" &
fi fi
@ -494,7 +499,7 @@ preview_fifo() {
} }
if [ "$PREVIEW_MODE" -eq 1 ] 2>/dev/null; then if [ "$PREVIEW_MODE" -eq 1 ] 2>/dev/null; then
if [ "$NNN_TERMINAL" != "kitty" ] && exists ueberzug; then if exists ueberzug && [ "$NNN_TERMINAL" != "kitty" ] && [[ "$NNN_PREVIEWIMGPROG" == +(|ueberzug) ]]; then
mkfifo "$FIFO_UEBERZUG" mkfifo "$FIFO_UEBERZUG"
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json & tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
fi fi