Closed ahloiscreamo closed 3 years ago
Can you please confirm that you're not only on the latest version of nnn
, but also using the latest versions of the plugin? These do not automatically update when you update nnn
. You would have to run ~/.config/nnn/plugins/getplugs
.
Yes i also update all the plugin just after updating nnn.
i updated the plugin without changing any value, in this case the preview-tui
~
❯ nnn
~/.config/nnn/plugins
❯ cat preview-tui
#!/usr/bin/env sh
# Description: Terminal based file previewer
#
# Note: This plugin needs a "NNN_FIFO" to work. See man.
#
# Dependencies:
# - Supports 4 independent methods to preview with:
# - tmux (>=3.0), or
# - kitty with allow_remote_control and listen_on set in kitty.conf, or
# - QuickLook on WSL (https://github.com/QL-Win/QuickLook)
# - $TERMINAL set to a terminal (it's xterm by default).
# - less or $PAGER
# - tree or exa or ls
# - mediainfo or file
# - mktemp
# - unzip
# - tar
# - man
# - optional: bsdtar or atool for additional archive preview
# - optional: bat for code syntax highlighting
# - optional: ueberzug, kitty terminal, viu or catimg for images
# - optional: convert(ImageMagick) for playing gif preview
# - optional: ffmpegthumbnailer for video thumbnails (https://github.com/dirkvdb/ffmpegthumbnailer)
# - optional: ffmpeg for audio thumbnails
# - optional: libreoffce for opendocument/officedocument preview
# - optional: pdftoppm(poppler) for pdf thumbnails
# - optional: gnome-epub-thumbnailer for epub thumbnails (https://gitlab.gnome.org/GNOME/gnome-epub-thumbnailer)
# - optional: fontpreview for font preview (https://github.com/sdushantha/fontpreview)
# - optional: glow or lowdown for markdown
# - optional: w3m or lynx or elinks for html
# - optional: set/export ICONLOOKUP as 1 to enable file icons in front of directory previews with .iconlookup
# Icons and colors are configureable in .iconlookup
# - optional: scope.sh file viewer from ranger.
# 1. drop scope.sh executable in $PATH
# 2. set/export $USE_SCOPE as 1
# - optional: pistol file viewer (https://github.com/doronbehar/pistol).
# 1. install pistol
# 2. set/export $USE_PISTOL as 1
#
# Usage:
# You need to set a NNN_FIFO path and a key for the plugin with NNN_PLUG,
# then start `nnn`:
#
# $ nnn -a
#
# or
#
# $ NNN_FIFO=/tmp/nnn.fifo nnn
#
# Then launch the `preview-tui` plugin in `nnn`.
#
# If you provide the same NNN_FIFO to all nnn instances, there will be a
# single common preview window. If you provide different FIFO path (e.g.
# with -a), they will be independent.
#
# The previews will be shown in a tmux split. If that isn't possible, it
# will try to use a kitty terminal split. And as a final fallback, a
# different terminal window will be used ($TERMINAL).
#
# Tmux and kitty users can configure $SPLIT to either "h" or "v" to set a
# 'h'orizontal split or a 'v'ertical split (as in, the line that splits the
# windows will be horizontal or vertical).
#
# Kitty users need `allow_remote_control` set to `yes`, and `listen_on` set
# to e.g. "$TMPDIR/kitty". To customize the window split, `enabled_layouts`
# has to be set to `all` or `splits` (the former is the default value).
# This terminal is also able to show images without extra dependencies.
#
# Shell: POSIX compliant
# Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste, Mario Ortiz Manero, Luuk van Baal
#SPLIT="$SPLIT" # you can set a permanent split here
#TERMINAL="$TERMINAL" # same goes for the terminal
USE_SCOPE="${USE_SCOPE:-0}"
USE_PISTOL="${USE_PISTOL:-0}"
ICONLOOKUP="${ICONLOOKUP:-0}"
PAGER="${PAGER:-less -P?n -R}"
TMPDIR="${TMPDIR:-/tmp}"
BAT_STYLE="${BAT_STYLE:-numbers}"
# Consider setting NNN_PREVIEWDIR to $XDG_CACHE_HOME/nnn/previews if you want to keep previews on disk between reboots
NNN_PREVIEWDIR="${NNN_PREVIEWDIR:-$TMPDIR/nnn/previews}"
NNN_PREVIEWWIDTH="${NNN_PREVIEWWIDTH:-1920}"
NNN_PREVIEWHEIGHT="${NNN_PREVIEWHEIGHT:-1080}"
NNN_PARENT="${NNN_FIFO#*.}"
FIFOPID="$TMPDIR/nnn-preview-tui-fifopid.$NNN_PARENT"
PAGERPID="$TMPDIR/nnn-preview-tui-pagerpid.$NNN_PARENT"
IMGPID="$TMPDIR/nnn-preview-tui-imgpid.$NNN_PARENT"
CURSEL="$TMPDIR/nnn-preview-tui-selection.$NNN_PARENT"
FIFO_UEBERZUG="$TMPDIR/nnn-preview-tui-ueberzug-fifo.$NNN_PARENT"
start_preview() {
[ "$PAGER" = "most" ] && PAGER="less -R"
if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.'; then
TERMINAL=tmux
elif [ -n "$KITTY_LISTEN_ON" ]; then
TERMINAL=kitty
else
TERMINAL="${TERMINAL:-xterm}"
fi
if [ -z "$SPLIT" ] && [ $(($(tput lines) * 2)) -gt "$(tput cols)" ]; then
SPLIT='h'
elif [ "$SPLIT" != 'h' ]; then
SPLIT='v'
fi
case "$TERMINAL" in
tmux) # tmux splits are inverted
if [ "$SPLIT" = "v" ]; then DSPLIT="h"; else DSPLIT="v"; fi
tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1" -e "PAGER=$PAGER" \
-e "USE_SCOPE=$USE_SCOPE" -e "SPLIT=$SPLIT" -e "USE_PISTOL=$USE_PISTOL" \
-e "BAT_STYLE=$BAT_STYLE" -e "PAGERPID=$PAGERPID" -e "FIFOPID=$FIFOPID" \
-e "IMGPID=$IMGPID" -e "CURSEL=$CURSEL" -e "TMPDIR=$TMPDIR" \
-e "ICONLOOKUP=$ICONLOOKUP" -e "NNN_PREVIEWDIR=$NNN_PREVIEWDIR" \
-e "NNN_PREVIEWWIDTH=$NNN_PREVIEWWIDTH" -e "NNN_PREVIEWHEIGHT=$NNN_PREVIEWHEIGHT" \
-e "FIFO_UEBERZUG=$FIFO_UEBERZUG" -e "QLPATH=$2" -d"$DSPLIT" "$0" "$1" ;;
kitty) # Setting the layout for the new window. It will be restored after the script ends.
kitty @ goto-layout splits
# Trying to use kitty's integrated window management as the split window. All
# environmental variables that will be used in the new window must be explicitly passed.
kitty @ launch --no-response --title "nnn preview" --keep-focus \
--cwd "$PWD" --env "PATH=$PATH" --env "NNN_FIFO=$NNN_FIFO" \
--env "PREVIEW_MODE=1" --env "PAGER=$PAGER" --env "TMPDIR=$TMPDIR" \
--env "USE_SCOPE=$USE_SCOPE" --env "SPLIT=$SPLIT" --env "TERMINAL=$TERMINAL"\
--env "USE_PISTOL=$USE_PISTOL" --env "BAT_STYLE=$BAT_STYLE" --env "FIFOPID=$FIFOPID" \
--env "PAGERPID=$PAGERPID" --env "IMGPID=$IMGPID" --env "FIFO_UEBERZUG=$FIFO_UEBERZUG" \
--env "ICONLOOKUP=$ICONLOOKUP" --env "NNN_PREVIEWDIR=$NNN_PREVIEWDIR" \
--env "NNN_PREVIEWWIDTH=$NNN_PREVIEWWIDTH" --env "NNN_PREVIEWHEIGHT=$NNN_PREVIEWHEIGHT" \
--env "CURSEL=$CURSEL" --location "${SPLIT}split" "$0" "$1" ;;
*) if [ -n "$2" ]; then
QUICKLOOK=1 QLPATH="$2" PREVIEW_MODE=1 "$0" "$1" &
else
PAGERPID="$PAGERPID" IMGPID="$IMGPID" CURSEL="$CURSEL" PREVIEW_MODE=1 \
FIFOPID="$FIFOPID" FIFO_UEBERZUG="$FIFO_UEBERZUG" $TERMINAL -e "$0" "$1" &
fi ;;
esac
} >/dev/null 2>&1
toggle_preview() {
if exists QuickLook.exe; then
QLPATH="QuickLook.exe"
elif exists Bridge.exe; then
QLPATH="Bridge.exe"
fi
if pkill -P "$(cat "$FIFOPID")"; then
if [ -n "$QLPATH" ] && stat "$1"; then
f="$(wslpath -w "$1")" && "$QLPATH" "$f" &
fi
else
start_preview "$1" "$QLPATH"
fi
} >/dev/null 2>&1
exists() {
type "$1" >/dev/null
}
fifo_pager() {
cmd="$1"
shift
# We use a FIFO to access $PAGER PID in jobs control
tmpfifopath="$TMPDIR/nnn-preview-tui-fifo.$$"
mkfifo "$tmpfifopath" || return
$PAGER < "$tmpfifopath" &
printf "%s" "$!" > "$PAGERPID"
(
exec > "$tmpfifopath"
if [ "$cmd" = "pager" ]; then
if exists bat; then
bat --terminal-width="$(tput cols)" --decorations=always --color=always \
--paging=never --style="$BAT_STYLE" "$@" &
else
$PAGER "$@" &
fi
else
"$cmd" "$@" &
fi
)
rm "$tmpfifopath"
} 2>/dev/null
# Binary file: show file info inside the pager
print_bin_info() {
printf -- "-------- \033[1;31mBinary file\033[0m --------\n"
if exists mediainfo; then
mediainfo "$1"
else
file -b "$1"
fi
} 2>/dev/null
handle_mime() {
case "$2" in
image/jpeg) image_preview "$cols" "$lines" "$1" ;;
image/gif) generate_preview "$cols" "$lines" "$1" "gif" ;;
image/*) generate_preview "$cols" "$lines" "$1" "image" ;;
video/*) generate_preview "$cols" "$lines" "$1" "video" ;;
audio/*) generate_preview "$cols" "$lines" "$1" "audio" ;;
application/font*|application/*opentype) generate_preview "$cols" "$lines" "$1" "font" ;;
*/*office*|*/*document*) generate_preview "$cols" "$lines" "$1" "office" ;;
application/zip) fifo_pager unzip -l "$1" ;;
text/troff)
if exists man; then
fifo_pager man -Pcat -l "$1"
else
fifo_pager pager "$1"
fi ;;
*) handle_ext "$1" "$3" "$4" ;;
esac
}
handle_ext() {
case "$2" in
epub) generate_preview "$cols" "$lines" "$1" "epub" ;;
pdf) generate_preview "$cols" "$lines" "$1" "pdf" ;;
gz|bz2) fifo_pager tar -tvf "$1" ;;
md) if exists glow; then
fifo_pager glow -s dark "$1"
elif exists lowdown; then
fifo_pager lowdown -Tterm "$1"
else
fifo_pager pager "$1"
fi ;;
htm|html|xhtml)
if exists w3m; then
fifo_pager w3m "$1"
elif exists lynx; then
fifo_pager lynx "$1"
elif exists elinks; then
fifo_pager elinks "$1"
else
fifo_pager pager "$1"
fi ;;
7z|a|ace|alz|arc|arj|bz|cab|cpio|deb|jar|lha|lz|lzh|lzma|lzo\
|rar|rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z)
if exists atool; then
fifo_pager atool -l "$1"
elif exists bsdtar; then
fifo_pager bsdtar -tvf "$1"
fi ;;
*) if [ "$3" = "bin" ]; then
fifo_pager print_bin_info "$1"
else
fifo_pager pager "$1"
fi ;;
esac
}
preview_file() {
clear
# Trying to use pistol if it's available.
if [ "$USE_PISTOL" -ne 0 ] && exists pistol; then
fifo_pager pistol "$1"
return
fi
# Trying to use scope.sh if it's available.
if [ "$USE_SCOPE" -ne 0 ] && exists scope.sh; then
fifo_pager scope.sh "$1" "$cols" "$lines" "$(mktemp -d)" "True"
return
fi
# Use QuickLook if it's available.
if [ -n "$QUICKLOOK" ]; then
stat "$1" && f="$(wslpath -w "$1")" && "$QLPATH" "$f" &
return
fi
# Detecting the exact type of the file: the encoding, mime type, and extension in lowercase.
encoding="$(file -bL --mime-encoding -- "$1")"
mimetype="$(file -bL --mime-type -- "$1")"
ext="${1##*.}"
[ -n "$ext" ] && ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
lines=$(tput lines)
cols=$(tput cols)
# Otherwise, falling back to the defaults.
if [ -d "$1" ]; then
cd "$1" || return
if [ "$ICONLOOKUP" -ne 0 ] && [ -f "$(dirname "$0")"/.iconlookup ]; then
[ "$SPLIT" = v ] && BSTR="\n"
# shellcheck disable=SC2012
ls -F --group-directories-first | head -n "$((lines - 3))" | "$(dirname "$0")"/.iconlookup -l "$cols" -B "$BSTR" -b " "
elif exists tree; then
fifo_pager tree --filelimit "$(find . -maxdepth 1 | wc -l)" -L 3 -C -F --dirsfirst --noreport
elif exists exa; then
exa -G --group-directories-first --colour=always
else
fifo_pager ls -F --group-directories-first --color=always
fi
elif [ "${encoding#*)}" = "binary" ]; then
handle_mime "$1" "$mimetype" "$ext" "bin"
else
handle_mime "$1" "$mimetype" "$ext"
fi
} 2>/dev/null
generate_preview() {
if [ -n "$QLPATH" ] && stat "$3"; then
f="$(wslpath -w "$3")" && "$QLPATH" "$f" &
elif [ ! -f "$NNN_PREVIEWDIR/$3.jpg" ] || [ -n "$(find -L "$3" -newer "$NNN_PREVIEWDIR/$3.jpg")" ]; then
mkdir -p "$NNN_PREVIEWDIR/${3%/*}"
case $4 in
audio) ffmpeg -i "$3" -filter_complex "scale=iw*min(1\,min($NNN_PREVIEWWIDTH/iw\,ih)):-1" "$NNN_PREVIEWDIR/$3.jpg" -y ;;
epub) gnome-epub-thumbnailer "$3" "$NNN_PREVIEWDIR/$3.jpg" ;;
font) fontpreview -i "$3" -o "$NNN_PREVIEWDIR/$3.jpg" ;;
gif) if [ -p "$FIFO_UEBERZUG" ] && exists convert; then
if [ ! -d "$NNN_PREVIEWDIR/$3" ]; then
mkdir -p "$NNN_PREVIEWDIR/$3"
convert -coalesce -resize "$NNN_PREVIEWWIDTH"x"$NNN_PREVIEWHEIGHT"\> "$3" "$NNN_PREVIEWDIR/$3/${3##*/}.jpg"
fi
while true; do
for frame in $(find "$NNN_PREVIEWDIR/$3"/*.jpg | sort -V); do
image_preview "$1" "$2" "$frame"
sleep 0.1
done
done &
printf "%s" "$!" > "$IMGPID"
return
else
image_preview "$1" "$2" "$3"
return
fi ;;
image) convert "$3" -flatten -resize "$NNN_PREVIEWWIDTH"x"$NNN_PREVIEWHEIGHT"\> "$NNN_PREVIEWDIR/$3.jpg" ;;
office) libreoffice --convert-to jpg "$3" --outdir "$NNN_PREVIEWDIR/${3%/*}"
filename="$(printf "%s" "${3##*/}" | cut -d. -f1)"
mv "$NNN_PREVIEWDIR/${3%/*}/$filename.jpg" "$NNN_PREVIEWDIR/$3.jpg" ;;
pdf) pdftoppm -jpeg -f 1 -singlefile "$3" "$NNN_PREVIEWDIR/$3" ;;
video) ffmpegthumbnailer -s0 -i "$3" -o "$NNN_PREVIEWDIR/$3.jpg" || rm "$NNN_PREVIEWDIR/$3.jpg" ;;
esac
fi >/dev/null
if [ -f "$NNN_PREVIEWDIR/$3.jpg" ]; then
image_preview "$1" "$2" "$NNN_PREVIEWDIR/$3.jpg"
else
fifo_pager print_bin_info "$3"
fi
} 2>/dev/null
image_preview() {
clear
if [ "$TERMINAL" = "kitty" ]; then
# Kitty terminal users can use the native image preview method
kitty +kitten icat --silent --place "$1"x"$2"@0x0 --transfer-mode=stream --stdin=no "$3" &
elif exists ueberzug; then
ueberzug_layer "$1" "$2" "$3" && return
elif exists catimg; then
catimg "$3" &
elif exists viu; then
viu -t "$3" &
else
fifo_pager print_bin_info "$3" && return
fi
printf "%s" "$!" > "$IMGPID"
} 2>/dev/null
ueberzug_layer() {
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"
}
ueberzug_remove() {
printf '{"action": "remove", "identifier": "nnn_ueberzug"}\n' > "$FIFO_UEBERZUG"
}
winch_handler() {
clear
kill "$(cat "$IMGPID")" "$(cat "$PAGERPID")"
if [ -p "$FIFO_UEBERZUG" ]; then
pkill -P "$$"
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
fi
cat "$CURSEL" > "$NNN_FIFO"
preview_file "$(cat "$CURSEL")"
preview_fifo &
wait "$!"
} 2>/dev/null
preview_fifo() {
printf "%s" "$$" > "$FIFOPID"
while read -r selection; do
kill "$(cat "$IMGPID")" "$(cat "$PAGERPID")"
[ -p "$FIFO_UEBERZUG" ] && ueberzug_remove
preview_file "$selection"
printf "%s" "$selection" > "$CURSEL"
done < "$NNN_FIFO"
} 2>/dev/null
if [ "$PREVIEW_MODE" ]; then
if [ "$TERMINAL" != "kitty" ] && exists ueberzug; then
mkfifo "$FIFO_UEBERZUG"
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
fi
printf "%s" "$PWD/$1" > "$CURSEL"
preview_file "$PWD/$1"
preview_fifo &
trap 'winch_handler' WINCH
trap 'rm "$PAGERPID" "$IMGPID" "$CURSEL" "$FIFO_UEBERZUG" "$FIFOPID" 2>/dev/null' INT HUP EXIT
wait "$!" 2>/dev/null
else
if [ ! -r "$NNN_FIFO" ]; then
clear
printf "No FIFO available! (\$NNN_FIFO='%s')\nPlease read Usage in preview-tui." "$NNN_FIFO"
cfg=$(stty -g); stty raw -echo; head -c 1; stty "$cfg"
elif [ "$KITTY_WINDOW_ID" ] && [ -z "$KITTY_LISTEN_ON" ]; then
clear
printf "\$KITTY_LISTEN_ON not set!\nPlease read Usage in preview-tui."
cfg=$(stty -g); stty raw -echo; head -c 1; stty "$cfg"
else
toggle_preview "$1" &
fi
fi
~/.config/nnn/plugins
❯
I tested this extensively when adding the resize handler. I normally don't use kitty
but I tested it again just now and can't reproduce it.
Thanks for the fast response, i cannot watch the video as the file's corrupted. let me check on my end if i could see anything wrong with my config and such.
Sorry, just noticed you were talking about opening tabs. I can reproduce the behavior there, never tested that before.
Not sure if anything can be done about it though, we don't know when kitty
is opening a new tab in preview-tui
.
Still don't see any issues when resizing though.
Also weird that the video is not showing, working fine here:
I think you would have to accept the fact that you will have to close/open preview-tui
after spawning a kitty
tab.
Please confirm that you don't see any issues when resizing only, no tabs.
The issues is still happen on resizing but not as fatal as i can just activate the plugin again, i can close the issues for now.
https://user-images.githubusercontent.com/9318372/120699325-d0d1de80-c4e2-11eb-9de6-42cbd04e3513.mp4
After further testing I was actually able to reproduce the broken preview after only resizing. It seems the resize handler is still not working correctly for kitty
, thanks for reporting. The previewer is broken after: resizing window-> changing entry. Will see if something can be done about it.
FYI this issue is not present on tmux
/ueberzug
.
The issues is still happen on resizing but not as fatal as i can just activate the plugin again, i can close the issues for now.
This behavior I can't reproduce...:thinking: The previewer only breaks after changing entry on my end...
But yeah in any case, resizing in preview-tui
is finicky although I never experienced any issues with tmux
. Your best bet is to close/open the preview after resizing the window. Will see if it can be improved, thanks again for reporting.
I see, thanks for the confirmation. I will switch to alacritty n ueberzug for the time's being. Thanks for taking time to help really appreciate it.
Yeah we have leftover preview_fifo()
processes for each resize for the non-ueberzug
case.
Not sure what the fix is, shell process management...
@KlzXS any thoughts on the leftover process?
I think I found something that covers all cases, please test @ahloiscreamo.
Resizing now also works properly for the non-ueberzug
case.
Hello, sorry for the late response. I just test the new build alongside the new plugin but unfortunately the issues still persist, just let me know anything that you need.
Out of curiosity, i installed the old version (nnn 4.0 sushi) just to confirm the issues, the old preview work just fine.
https://user-images.githubusercontent.com/9318372/120754035-aca3eb00-c53e-11eb-96b4-cf3a53ed0c0b.mp4
Thanks for testing. Are you sure though? I really don't see any issues any more on #1051. No matter what I do, preview-tui
keeps working.
It works fine on 4.0 because resizing is only handled for ueberzug
there, the kitty
preview would stay the same size.
I guess i should use ueberzug
to handle the preview, i have no idea why kitty icat is buggy on my system.
I'll be back after investigating.
Out of curiosity, i installed the old version (nnn 4.0 sushi) just to confirm the issues, the old preview work just fine.
https://user-images.githubusercontent.com/9318372/120754035-aca3eb00-c53e-11eb-96b4-cf3a53ed0c0b.mp4
Out of curiosity, will have no effect normally
If it isn't already, try setting TERM=linux
or TERM=xterm
, it sometimes help with some incompatibilities
I guess i should use
ueberzug
to handle the preview, i have no idea why kitty icat is buggy on my system. I'll be back after investigating.
Please check if you a have leftover a preview-tui
process for each resize on #1051 like I mentioned in https://github.com/jarun/nnn/issues/1050#issuecomment-854142824. This is what happened before #1051 and is what broke the previewer. I don't see that issue at all anymore no matter how much I resize.
P.S. ueberzug
is a lot faster than icat
so I would use that too.
Linux artix 5.12.8-artix1-1 #1 SMP PREEMPT Fri, 28 May 2021 22:51:03 +0000 x86_64 GNU/Linux
i3 version 4.19.1 (2021-02-01) © 2009 Michael Stapelberg and contributors
kitty 0.20.3 created by Kovid Goyal
fish, version 3.2.2
nnn
masterExact steps to reproduce the issue
Upgrade nnn to 4.1, open nnn inside kitty terminal, activate preview-tui plugin, the image preview will disappeared whenever i resize kitty window and when i launch kitty tab. video for references
https://user-images.githubusercontent.com/9318372/120694339-a8df7c80-c4dc-11eb-9f9a-703c09e782c5.mp4