preview-tui(-ext) subshell job control (#891)

* subshell job control

* remove unnecessary kill

* Update docs

* update PAGER and tree command

* restore clear
This commit is contained in:
luukvbaal 2021-03-20 01:58:46 +01:00 committed by GitHub
parent a88bbe1a9c
commit 2e4aca5e6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 26 deletions

View File

@ -55,6 +55,7 @@ Plugins extend the capabilities of `nnn`. They are _executable_ scripts (or bina
| [picker](picker) | Pick files and list one per line (to pipe) | sh | nnn |
| [preview-tabbed](preview-tabbed) | Tabbed/xembed based file previewer | bash | _see in-file docs_ |
| [preview-tui](preview-tui) | Tmux/kitty/xterm/`$TERMINAL` based file previewer | sh | _see in-file docs_ |
| [preview-tui-ext](preview-tui-ext) | Meant to be an exhaustive version of [preview-tui](preview-tui) | sh | _see in-file docs_ |
| [pskill](pskill) | Fuzzy list by name and kill process or zombie | sh | fzf, ps, sudo/doas |
| [renamer](renamer) | Batch rename selection or files in dir | sh | [qmv](https://www.nongnu.org/renameutils/)/[vidir](https://joeyh.name/code/moreutils/) |
| [ringtone](ringtone) | Create a variable bitrate mp3 ringtone from file | sh | date, ffmpeg |

View File

@ -3,7 +3,7 @@
# Description: Terminal based file previewer
#
# Note: This plugin needs a "NNN_FIFO" to work. See man.
# For a more extended version of this script, including ueberzug support, see preview-tui-ext.
# For a more extended version of this script with additional optional dependencies, see preview-tui-ext.
#
# Dependencies:
# - Supports 3 independent methods to preview with:
@ -58,14 +58,19 @@
# without extra dependencies.
#
# Shell: POSIX compliant
# Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste, Mario Ortiz Manero
# 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}"
PAGER="${PAGER:-less -R}"
PAGER="${PAGER:-less -P?n -R}"
TMPDIR="${TMPDIR:-/tmp}"
NUMPREVIEWTUI="$(($(find "$TMPDIR" -maxdepth 1 -name 'nnn-preview-tui-pagerpid*' 2>/dev/null | wc -l) + 1))"
PAGERPID="$TMPDIR/nnn-preview-tui-pagerpid.$NUMPREVIEWTUI"
GIFPID="$TMPDIR/nnn-preview-tui-gifpid.$NUMPREVIEWTUI"
FIFO_UEBERZUG="$TMPDIR/nnn-preview-tui-ueberzug-fifo.$NUMPREVIEWTUI"
[ "$PAGER" = "most" ] && PAGER="less -R"
if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.'; then
@ -115,9 +120,7 @@ print_bin_info() {
}
preview_file () {
kill %- %+ 2>/dev/null && wait %- %+ 2>/dev/null
clear
# Trying to use pistol if it's available.
if [ "$USE_PISTOL" -ne 0 ] && exists pistol; then
fifo_pager pistol "$1"
@ -146,7 +149,7 @@ preview_file () {
if [ -d "$1" ]; then
cd "$1" || return
if exists tree; then
fifo_pager tree -L 3 -F
fifo_pager tree -L 1 --dirsfirst -F -C
elif exists exa; then
fifo_pager exa -G --colour=always 2>/dev/null
else
@ -183,10 +186,10 @@ image_preview() {
ueberzug_layer "$1" "$2" "$3"
elif exists catimg; then
catimg "$3" &
gifpid="$!"
echo "$!" > "$GIFPID"
elif exists viu; then
viu -t "$3" &
gifpid="$!"
echo "$!" > "$GIFPID"
else
fifo_pager print_bin_info "$1"
fi
@ -202,8 +205,8 @@ ueberzug_remove() {
ueberzug_refresh() {
clear
pkill -P "$$"
pkill -f -n preview-tui
pkill -P "$$" >/dev/null 2>&1
pkill -f -n preview-tui-ext >/dev/null 2>&1
echo > "$NNN_FIFO"
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
preview_fifo &
@ -211,7 +214,7 @@ ueberzug_refresh() {
}
if [ "$TERMINAL" != "kitty" ] && [ "$PREVIEW_MODE" ] && exists ueberzug; then
trap 'ueberzug_refresh' WINCH
trap 'rm "$FIFO_UEBERZUG"' INT HUP EXIT
trap 'rm "$FIFO_UEBERZUG" "$PAGERPID" "$GIFPID"' INT HUP EXIT
fi
preview_fifo() {
@ -219,11 +222,13 @@ preview_fifo() {
# shellcheck disable=SC2002
cat "$NNN_FIFO" |\
while read -r selection; do
[ "$gifpid" -ne 0 ] && kill "$gifpid"
kill "$(cat "$GIFPID" 2>/dev/null)" >/dev/null 2>&1
kill "$(cat "$PAGERPID" 2>/dev/null)" >/dev/null 2>&1
[ "$TERMINAL" != "kitty" ] && exists ueberzug && ueberzug_remove
preview_file "$selection"
done
[ "$TERMINAL" != "kitty" ] && exists ueberzug && rm "$FIFO_UEBERZUG"
rm "$PAGERPID" "$GIFPID" >/dev/null 2>&1
}
if [ "$PREVIEW_MODE" ]; then

View File

@ -65,7 +65,7 @@
# without extra dependencies.
#
# Shell: POSIX compliant
# Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste, Mario Ortiz Manero
# 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
@ -74,6 +74,10 @@ USE_PISTOL="${USE_PISTOL:-0}"
PAGER="${PAGER:-less -P?n -R}"
ARCHIVES="$(echo "$NNN_ARCHIVE" | sed 's/.*(\(.*\)).*/\1/;s/|/ /g')"
TMPDIR="${TMPDIR:-/tmp}"
NUMPREVIEWTUI="$(($(find "$TMPDIR" -maxdepth 1 -name 'nnn-preview-tui-pagerpid*' 2>/dev/null | wc -l) + 1))"
PAGERPID="$TMPDIR/nnn-preview-tui-pagerpid.$NUMPREVIEWTUI"
GIFPID="$TMPDIR/nnn-preview-tui-gifpid.$NUMPREVIEWTUI"
FIFO_UEBERZUG="$TMPDIR/nnn-preview-tui-ueberzug-fifo.$NUMPREVIEWTUI"
[ "$PAGER" = "most" ] && PAGER="less -R"
@ -104,7 +108,7 @@ fifo_pager() {
mkfifo "$tmpfifopath" || return
$PAGER < "$tmpfifopath" &
pagerpid="$!"
echo "$!" > "$PAGERPID"
(
exec > "$tmpfifopath"
@ -125,9 +129,7 @@ print_bin_info() {
}
preview_file () {
kill %- %+ 2>/dev/null && wait %- %+ 2>/dev/null
clear
# Trying to use pistol if it's available.
if [ "$USE_PISTOL" -ne 0 ] && exists pistol; then
fifo_pager pistol "$1"
@ -202,7 +204,7 @@ preview_file () {
generate_preview() {
if [ ! -f "$TMPDIR/$3.png" ]; then
fifo_pager print_bin_info "$3"
[ "$4" != gif ] && fifo_pager print_bin_info "$3"
mkdir -p "$TMPDIR/${3%/*}"
case $4 in
audio) ffmpeg -i "$3" "$TMPDIR/$3.png" -y >/dev/null 2>&1 ;;
@ -220,7 +222,7 @@ generate_preview() {
done
[ "$LOOP_GIFS" -eq 0 ] && return
done &
gifpid="$!"
echo "$!" > "$GIFPID"
return
else
image_preview "$1" "$2" "$3"
@ -232,12 +234,12 @@ generate_preview() {
pdf) pdftoppm -png -f 1 -singlefile "$3" "$TMPDIR/$3" >/dev/null 2>&1 ;;
video) ffmpegthumbnailer -i "$3" -o "$TMPDIR/$3.png" -s 0 -q 10 >/dev/null 2>&1 ;;
esac
kill "$pagerpid"
fi
image_preview "$1" "$2" "$TMPDIR/$3.png"
}
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 \
@ -246,10 +248,10 @@ image_preview() {
ueberzug_layer "$1" "$2" "$3"
elif exists catimg; then
catimg "$3" &
gifpid="$!"
echo "$!" > "$GIFPID"
elif exists viu; then
viu -t "$3" &
gifpid="$!"
echo "$!" > "$GIFPID"
else
fifo_pager print_bin_info "$1"
fi
@ -265,8 +267,8 @@ ueberzug_remove() {
ueberzug_refresh() {
clear
pkill -P "$$"
pkill -f -n preview-tui-ext
pkill -P "$$" >/dev/null 2>&1
pkill -f -n preview-tui-ext >/dev/null 2>&1
echo > "$NNN_FIFO"
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
preview_fifo &
@ -274,7 +276,7 @@ ueberzug_refresh() {
}
if [ "$TERMINAL" != "kitty" ] && [ "$PREVIEW_MODE" ] && exists ueberzug; then
trap 'ueberzug_refresh' WINCH
trap 'rm "$FIFO_UEBERZUG"' INT HUP EXIT
trap 'rm "$FIFO_UEBERZUG" "$PAGERPID" "$GIFPID"' INT HUP EXIT
fi
preview_fifo() {
@ -282,11 +284,13 @@ preview_fifo() {
# shellcheck disable=SC2002
cat "$NNN_FIFO" |\
while read -r selection ; do
[ "$gifpid" -ne 0 ] && kill "$gifpid"
kill "$(cat "$GIFPID" 2>/dev/null)" >/dev/null 2>&1
kill "$(cat "$PAGERPID" 2>/dev/null)" >/dev/null 2>&1
[ "$TERMINAL" != "kitty" ] && exists ueberzug && ueberzug_remove
preview_file "$selection"
done
[ "$TERMINAL" != "kitty" ] && exists ueberzug && rm "$FIFO_UEBERZUG"
rm "$PAGERPID" "$GIFPID" >/dev/null 2>&1
}
@ -298,7 +302,6 @@ if [ "$PREVIEW_MODE" ]; then
fi
if [ "$TERMINAL" != "kitty" ] && exists ueberzug; then
FIFO_UEBERZUG="$TMPDIR/nnn-ueberzug-fifo.$$"
mkfifo "$FIFO_UEBERZUG"
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
fi