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 | | [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-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](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 | | [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/) | | [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 | | [ringtone](ringtone) | Create a variable bitrate mp3 ringtone from file | sh | date, ffmpeg |

View file

@ -3,7 +3,7 @@
# Description: Terminal based file previewer # Description: Terminal based file previewer
# #
# Note: This plugin needs a "NNN_FIFO" to work. See man. # 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: # Dependencies:
# - Supports 3 independent methods to preview with: # - Supports 3 independent methods to preview with:
@ -58,14 +58,19 @@
# without extra dependencies. # without extra dependencies.
# #
# Shell: POSIX compliant # 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 SPLIT="$SPLIT" # you can set a permanent split here
TERMINAL="$TERMINAL" # same goes for the terminal TERMINAL="$TERMINAL" # same goes for the terminal
USE_SCOPE="${USE_SCOPE:-0}" USE_SCOPE="${USE_SCOPE:-0}"
USE_PISTOL="${USE_PISTOL:-0}" USE_PISTOL="${USE_PISTOL:-0}"
PAGER="${PAGER:-less -R}" PAGER="${PAGER:-less -P?n -R}"
TMPDIR="${TMPDIR:-/tmp}" 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" [ "$PAGER" = "most" ] && PAGER="less -R"
if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.'; then if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.'; then
@ -115,9 +120,7 @@ print_bin_info() {
} }
preview_file () { preview_file () {
kill %- %+ 2>/dev/null && wait %- %+ 2>/dev/null
clear clear
# Trying to use pistol if it's available. # Trying to use pistol if it's available.
if [ "$USE_PISTOL" -ne 0 ] && exists pistol; then if [ "$USE_PISTOL" -ne 0 ] && exists pistol; then
fifo_pager pistol "$1" fifo_pager pistol "$1"
@ -146,7 +149,7 @@ preview_file () {
if [ -d "$1" ]; then if [ -d "$1" ]; then
cd "$1" || return cd "$1" || return
if exists tree; then if exists tree; then
fifo_pager tree -L 3 -F fifo_pager tree -L 1 --dirsfirst -F -C
elif exists exa; then elif exists exa; then
fifo_pager exa -G --colour=always 2>/dev/null fifo_pager exa -G --colour=always 2>/dev/null
else else
@ -183,10 +186,10 @@ image_preview() {
ueberzug_layer "$1" "$2" "$3" ueberzug_layer "$1" "$2" "$3"
elif exists catimg; then elif exists catimg; then
catimg "$3" & catimg "$3" &
gifpid="$!" echo "$!" > "$GIFPID"
elif exists viu; then elif exists viu; then
viu -t "$3" & viu -t "$3" &
gifpid="$!" echo "$!" > "$GIFPID"
else else
fifo_pager print_bin_info "$1" fifo_pager print_bin_info "$1"
fi fi
@ -202,8 +205,8 @@ ueberzug_remove() {
ueberzug_refresh() { ueberzug_refresh() {
clear clear
pkill -P "$$" pkill -P "$$" >/dev/null 2>&1
pkill -f -n preview-tui pkill -f -n preview-tui-ext >/dev/null 2>&1
echo > "$NNN_FIFO" echo > "$NNN_FIFO"
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json & tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
preview_fifo & preview_fifo &
@ -211,7 +214,7 @@ ueberzug_refresh() {
} }
if [ "$TERMINAL" != "kitty" ] && [ "$PREVIEW_MODE" ] && exists ueberzug; then if [ "$TERMINAL" != "kitty" ] && [ "$PREVIEW_MODE" ] && exists ueberzug; then
trap 'ueberzug_refresh' WINCH trap 'ueberzug_refresh' WINCH
trap 'rm "$FIFO_UEBERZUG"' INT HUP EXIT trap 'rm "$FIFO_UEBERZUG" "$PAGERPID" "$GIFPID"' INT HUP EXIT
fi fi
preview_fifo() { preview_fifo() {
@ -219,11 +222,13 @@ preview_fifo() {
# shellcheck disable=SC2002 # shellcheck disable=SC2002
cat "$NNN_FIFO" |\ cat "$NNN_FIFO" |\
while read -r selection; do 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 [ "$TERMINAL" != "kitty" ] && exists ueberzug && ueberzug_remove
preview_file "$selection" preview_file "$selection"
done done
[ "$TERMINAL" != "kitty" ] && exists ueberzug && rm "$FIFO_UEBERZUG" [ "$TERMINAL" != "kitty" ] && exists ueberzug && rm "$FIFO_UEBERZUG"
rm "$PAGERPID" "$GIFPID" >/dev/null 2>&1
} }
if [ "$PREVIEW_MODE" ]; then if [ "$PREVIEW_MODE" ]; then

View file

@ -65,7 +65,7 @@
# without extra dependencies. # without extra dependencies.
# #
# Shell: POSIX compliant # 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 SPLIT="$SPLIT" # you can set a permanent split here
TERMINAL="$TERMINAL" # same goes for the terminal TERMINAL="$TERMINAL" # same goes for the terminal
@ -74,6 +74,10 @@ USE_PISTOL="${USE_PISTOL:-0}"
PAGER="${PAGER:-less -P?n -R}" PAGER="${PAGER:-less -P?n -R}"
ARCHIVES="$(echo "$NNN_ARCHIVE" | sed 's/.*(\(.*\)).*/\1/;s/|/ /g')" ARCHIVES="$(echo "$NNN_ARCHIVE" | sed 's/.*(\(.*\)).*/\1/;s/|/ /g')"
TMPDIR="${TMPDIR:-/tmp}" 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" [ "$PAGER" = "most" ] && PAGER="less -R"
@ -104,7 +108,7 @@ fifo_pager() {
mkfifo "$tmpfifopath" || return mkfifo "$tmpfifopath" || return
$PAGER < "$tmpfifopath" & $PAGER < "$tmpfifopath" &
pagerpid="$!" echo "$!" > "$PAGERPID"
( (
exec > "$tmpfifopath" exec > "$tmpfifopath"
@ -125,9 +129,7 @@ print_bin_info() {
} }
preview_file () { preview_file () {
kill %- %+ 2>/dev/null && wait %- %+ 2>/dev/null
clear clear
# Trying to use pistol if it's available. # Trying to use pistol if it's available.
if [ "$USE_PISTOL" -ne 0 ] && exists pistol; then if [ "$USE_PISTOL" -ne 0 ] && exists pistol; then
fifo_pager pistol "$1" fifo_pager pistol "$1"
@ -202,7 +204,7 @@ preview_file () {
generate_preview() { generate_preview() {
if [ ! -f "$TMPDIR/$3.png" ]; then if [ ! -f "$TMPDIR/$3.png" ]; then
fifo_pager print_bin_info "$3" [ "$4" != gif ] && fifo_pager print_bin_info "$3"
mkdir -p "$TMPDIR/${3%/*}" mkdir -p "$TMPDIR/${3%/*}"
case $4 in case $4 in
audio) ffmpeg -i "$3" "$TMPDIR/$3.png" -y >/dev/null 2>&1 ;; audio) ffmpeg -i "$3" "$TMPDIR/$3.png" -y >/dev/null 2>&1 ;;
@ -220,7 +222,7 @@ generate_preview() {
done done
[ "$LOOP_GIFS" -eq 0 ] && return [ "$LOOP_GIFS" -eq 0 ] && return
done & done &
gifpid="$!" echo "$!" > "$GIFPID"
return return
else else
image_preview "$1" "$2" "$3" image_preview "$1" "$2" "$3"
@ -232,12 +234,12 @@ generate_preview() {
pdf) pdftoppm -png -f 1 -singlefile "$3" "$TMPDIR/$3" >/dev/null 2>&1 ;; 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 ;; video) ffmpegthumbnailer -i "$3" -o "$TMPDIR/$3.png" -s 0 -q 10 >/dev/null 2>&1 ;;
esac esac
kill "$pagerpid"
fi fi
image_preview "$1" "$2" "$TMPDIR/$3.png" image_preview "$1" "$2" "$TMPDIR/$3.png"
} }
image_preview() { image_preview() {
clear
if [ "$TERMINAL" = "kitty" ]; then if [ "$TERMINAL" = "kitty" ]; then
# Kitty terminal users can use the native image preview method. # Kitty terminal users can use the native image preview method.
kitty +kitten icat --silent --place "$1"x"$2"@0x0 --transfer-mode=stream --stdin=no \ 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" ueberzug_layer "$1" "$2" "$3"
elif exists catimg; then elif exists catimg; then
catimg "$3" & catimg "$3" &
gifpid="$!" echo "$!" > "$GIFPID"
elif exists viu; then elif exists viu; then
viu -t "$3" & viu -t "$3" &
gifpid="$!" echo "$!" > "$GIFPID"
else else
fifo_pager print_bin_info "$1" fifo_pager print_bin_info "$1"
fi fi
@ -265,8 +267,8 @@ ueberzug_remove() {
ueberzug_refresh() { ueberzug_refresh() {
clear clear
pkill -P "$$" pkill -P "$$" >/dev/null 2>&1
pkill -f -n preview-tui-ext pkill -f -n preview-tui-ext >/dev/null 2>&1
echo > "$NNN_FIFO" echo > "$NNN_FIFO"
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json & tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
preview_fifo & preview_fifo &
@ -274,7 +276,7 @@ ueberzug_refresh() {
} }
if [ "$TERMINAL" != "kitty" ] && [ "$PREVIEW_MODE" ] && exists ueberzug; then if [ "$TERMINAL" != "kitty" ] && [ "$PREVIEW_MODE" ] && exists ueberzug; then
trap 'ueberzug_refresh' WINCH trap 'ueberzug_refresh' WINCH
trap 'rm "$FIFO_UEBERZUG"' INT HUP EXIT trap 'rm "$FIFO_UEBERZUG" "$PAGERPID" "$GIFPID"' INT HUP EXIT
fi fi
preview_fifo() { preview_fifo() {
@ -282,11 +284,13 @@ preview_fifo() {
# shellcheck disable=SC2002 # shellcheck disable=SC2002
cat "$NNN_FIFO" |\ cat "$NNN_FIFO" |\
while read -r selection ; do 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 [ "$TERMINAL" != "kitty" ] && exists ueberzug && ueberzug_remove
preview_file "$selection" preview_file "$selection"
done done
[ "$TERMINAL" != "kitty" ] && exists ueberzug && rm "$FIFO_UEBERZUG" [ "$TERMINAL" != "kitty" ] && exists ueberzug && rm "$FIFO_UEBERZUG"
rm "$PAGERPID" "$GIFPID" >/dev/null 2>&1
} }
@ -298,7 +302,6 @@ if [ "$PREVIEW_MODE" ]; then
fi fi
if [ "$TERMINAL" != "kitty" ] && exists ueberzug; then if [ "$TERMINAL" != "kitty" ] && exists ueberzug; then
FIFO_UEBERZUG="$TMPDIR/nnn-ueberzug-fifo.$$"
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