diff --git a/plugins/README.md b/plugins/README.md
index aa6749d2..e5c0667b 100644
--- a/plugins/README.md
+++ b/plugins/README.md
@@ -3,8 +3,8 @@
read ebooks with plugin gutenread (Android)
-
-Live Previews asciicast
+
+Live Previews
## Introduction
@@ -63,9 +63,8 @@ Plugins are installed to `${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins`.
| pdfread | Read a PDF or text file aloud | sh | pdftotext, mpv,
pico2wave |
| pdfview | View PDF file in `$PAGER` | sh | pdftotext/
mupdf-tools |
| picker | Pick files and list one per line (to pipe) | sh | nnn |
-| preview-kitty | File previewer in a kitty terminal pane | sh | [kitty](https://sw.kovidgoyal.net/kitty/), optional:
[exa](https://github.com/ogham/exa) [bat](https://github.com/sharkdp/bat) mediainfo |
| preview-tabbed | `tabbed`/xembed based file previewer | bash | _see in-file docs_ |
-| preview-tui | Simple TUI file previewer (needs NNN_FIFO) | sh | tmux (>= 3.0)/xterm/
\$TERMINAL, file, tree |
+| preview-tui | Tmux/kitty/`$TERMINAL` based file previewer | sh | _see in-file docs_ |
| pskill | Fuzzy list by name and kill process or zombie | sh | fzf, ps, sudo/doas |
| renamer | Batch rename selection or files in dir | sh | [qmv](https://www.nongnu.org/renameutils/)/[vidir](https://joeyh.name/code/moreutils/) |
| ringtone | Create a variable bitrate mp3 ringtone from file | sh | date, ffmpeg |
diff --git a/plugins/preview-kitty b/plugins/preview-kitty
deleted file mode 100755
index af51cee0..00000000
--- a/plugins/preview-kitty
+++ /dev/null
@@ -1,77 +0,0 @@
-#!/usr/bin/env sh
-
-# Description: File previewer for kitty term using NNN_FIFO
-#
-# Dependencies:
-# - kitty (https://sw.kovidgoyal.net/kitty/) with allow_remote_control on
-# - file
-# - exa (https://github.com/ogham/exa) (fallback: ls)
-# - bat (https://github.com/sharkdp/bat) (fallback: cat)
-# - mediainfo (fallback: file)
-#
-# Usage:
-# This plugin only works in kitty (https://sw.kovidgoyal.net/kitty/),
-# and with kitty's 'allow_remote_control' option turned on.
-# You need to set a NNN_FIFO path and set a key for the plugin,
-# then start `nnn`:
-#
-# $ NNN_FIFO=/tmp/nnn.fifo nnn
-#
-# Then in `nnn`, launch the `preview-kitty` plugin.
-#
-# 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, they
-# will be independent.
-#
-# Shell: POSIX compliant
-# Authors: Léo Villeveygoux
-
-preview_file () {
- kill %% 2>/dev/null
- clear
- lines=$(($(tput lines)-1))
- cols=$(tput cols)
- mime="$(file -b --mime-type "$1")"
- encoding="$(file -b --mime-encoding "$1")"
-
- if [ -d "$1" ]; then
- # Print directory tree
- # shellcheck disable=SC2015
- cd "$1" && \
- (COLUMNS=$cols exa -G --colour=always 2>/dev/null ||\
- ls --color=alway) | head -n $lines &
- # remove second clause to preview SVG files (but this is slow)
- elif [ "${mime%%/*}" = "image" ] && [ "$encoding" = "binary" ] ; then
- kitty +kitten icat --silent --transfer-mode=stream --stdin=no "$1" &
- elif [ "$encoding" = "binary" ] ; then
- # Binary file: show file info
- printf -- "-------- \033[1;31mBinary file\033[0m --------\n"
- (mediainfo "$1" 2>/dev/null || file -b "$1") | head -n $((lines - 1)) &
- else
- # Text file: print colored file content
- (bat --terminal-width="$cols" --paging=never --decorations=always \
- --color=always "$1" 2>/dev/null || cat) | head -n $lines &
- fi
-}
-
-if [ "$PREVIEW_MODE" ] ; then
- if [ ! -r "$NNN_FIFO" ] ; then
- echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
- read -r
- exit 1
- fi
-
- preview_file "$1"
-
- # use cat instead of 'exec <' to avoid issues with dash shell
- # shellcheck disable=SC2002
- cat "$NNN_FIFO" |\
- while read -r selection ; do
- preview_file "$selection"
- done
- exit 0
-fi
-
-kitty @ launch --no-response --title "nnn preview" --keep-focus \
- --cwd="$PWD" --env "NNN_FIFO=$NNN_FIFO" --env "PREVIEW_MODE=1" \
- "$0" "$1" > /dev/null
diff --git a/plugins/preview-tui b/plugins/preview-tui
index 62ba94c8..222f13fd 100755
--- a/plugins/preview-tui
+++ b/plugins/preview-tui
@@ -1,15 +1,29 @@
#!/usr/bin/env sh
-# Description: Text based file previewer
+# Description: Terminal based file previewer
#
-# Note: This plugin needs a "NNN_FIFO" to work.
+# Note: This plugin needs a "NNN_FIFO" to work. See man.
#
-# Dependencies: tmux (>=3.0) or xterm or $TERMINAL, less or $PAGER,
-# file, stat, tree, man, tar, unzip, ...
-# ... add you own! (see examples in code)
+# Dependencies:
+# - Supports 3 independent methods to preview with:
+# - tmux (>=3.0), or
+# - kitty with allow_remote_control on, or
+# - $TERMINAL set to a terminal (it's xterm by default).
+# - less or $PAGER
+# - tree or exa or ls
+# - mediainfo or file
+# - unzip
+# - tar
+# - man
+# - optional: bat for code syntax highlighting
+# - optional: kitty terminal or catimg for images
+# - optional: scope.sh file viewer from ranger.
+# To use:
+# 1. drop scope.sh executable in $PATH
+# 2. set/export $USE_SCOPE as 1
#
# Usage:
-# You need to set a NNN_FIFO path and set a key for the plugin,
+# You need to set a NNN_FIFO path and a key for the plugin with NNN_PLUG,
# then start `nnn`:
#
# $ nnn -a
@@ -24,15 +38,42 @@
# single common preview window. I you provide different FIFO path (e.g.
# with -a), they will be independent.
#
-# Configure SPLIT to either "h" or "v" to set a 'h'orizontal split or a
-# 'v'ertical split
+# 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.
+#
+# Kitty users need `allow_remote_control` set to `yes`. 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
+# Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste, Mario Ortiz Manero
-TERMINAL="${TERMINAL:-xterm}"
+USE_SCOPE="${USE_SCOPE:-0}"
PAGER="${PAGER:-less -R}"
+if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.'; then
+ TERMINAL=tmux
+elif [ -n "$KITTY_WINDOW_ID" ] && kitty @ ls >/dev/null 2>&1; then
+ TERMINAL=kitty
+else
+ TERMINAL="${TERMINAL:-xterm}"
+fi
+
+if [ -z "$SPLIT" ] && [ $(($(tput lines) * 2)) -gt "$(tput cols)" ] ; then
+ SPLIT='v'
+elif [ "$SPLIT" != 'v' ] ; then
+ SPLIT='h'
+fi
+
+exists() {
+ command -v "$1" >/dev/null 2>&1
+}
+
fifo_pager() {
cmd="$1"
shift
@@ -51,42 +92,75 @@ fifo_pager() {
rm "$tmpfifopath"
}
+# 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" 2>/dev/null
+ else
+ file -b "$1"
+ fi
+}
+
preview_file () {
kill %- %+ 2>/dev/null && wait %- %+ 2>/dev/null
clear
+ # Detecting the exact type of the file: the encoding, mime type, and
+ # extension in lowercase.
encoding="$(file -Lb --mime-encoding -- "$1")"
-
- # Detect mime type
mimetype="$(file -Lb --mime-type -- "$1")"
-
- # Detect file extention
ext="${1##*.}"
if [ -n "$ext" ]; then
ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
fi
+ lines=$(($(tput lines)-1))
+ cols=$(tput cols)
+ # 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" 2>/dev/null
+ return
+ fi
+
+ # Otherwise, falling back to the defaults.
if [ -d "$1" ]; then
- # Print directory tree
cd "$1" || return
- fifo_pager tree
- #elif [ "${mimetype%%/*}" = "image" ] ; then
- # catimg "$1"
+ if exists tree; then
+ fifo_pager tree
+ elif exists exa; then
+ fifo_pager exa -G --colour=always 2>/dev/null
+ else
+ fifo_pager ls --color=always
+ fi
+ elif [ "$encoding" = "binary" ]; then
+ if [ "${mimetype%%/*}" = "image" ] ; then
+ if [ "$TERMINAL" = "kitty" ]; then
+ # Kitty terminal users can use the native image preview method.
+ kitty +kitten icat --silent --transfer-mode=stream --stdin=no \
+ "$1" &
+ elif exists catimg; then
+ catimg "$1"
+ else
+ fifo_pager print_bin_info "$1"
+ fi
+ elif [ "$mimetype" = "application/zip" ] ; then
+ fifo_pager unzip -l "$1"
+ elif [ "$ext" = "gz" ] || [ "$ext" = "bz2" ] ; then
+ fifo_pager tar -tvf "$1"
+ else
+ fifo_pager print_bin_info "$1"
+ fi
elif [ "$mimetype" = "text/troff" ] ; then
fifo_pager man -Pcat -l "$1"
- elif [ "$mimetype" = "application/zip" ] ; then
- fifo_pager unzip -l "$1"
- elif [ "$ext" = "gz" ] || [ "$ext" = "bz2" ] ; then
- fifo_pager tar -tvf "$1"
- elif [ "$encoding" = "binary" ] ; then
- # Binary file: just print filetype info
- echo "-------- binary file --------"
- file -b "$1"
- echo
- stat "$1"
else
- # Text file:
- $PAGER "$1" &
+ if exists bat; then
+ fifo_pager bat --paging=never --decorations=always --color=always \
+ "$1" 2>/dev/null
+ else
+ $PAGER "$1" &
+ fi
fi
}
@@ -99,21 +173,31 @@ if [ "$PREVIEW_MODE" ] ; then
preview_file "$1"
- exec < "$NNN_FIFO"
+ # use cat instead of 'exec <' to avoid issues with dash shell
+ # shellcheck disable=SC2002
+ cat "$NNN_FIFO" |\
while read -r selection ; do
preview_file "$selection"
done
exit 0
fi
-if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.' ; then
- if [ -z "$SPLIT" ] && [ $(($(tput lines) * 2)) -gt "$(tput cols)" ] ; then
- SPLIT='v'
- elif [ "$SPLIT" != 'v' ] ; then
- SPLIT='h'
- fi
+if [ "$TERMINAL" = "tmux" ]; then
+ # tmux splits are inverted
+ if [ "$SPLIT" = "v" ]; then SPLIT="h"; else SPLIT="v"; fi
tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1" -d"$SPLIT" "$0" "$1"
+elif [ "$TERMINAL" = "kitty" ]; then
+ # Setting the layout for the new window.
+ kitty @ goto-layout splits >/dev/null
+
+ # Trying to use kitty's integrated window management as the split window.
+ kitty @ launch --no-response --title "nnn preview" --keep-focus \
+ --cwd "$PWD" --env "NNN_FIFO=$NNN_FIFO" --env "PREVIEW_MODE=1" \
+ --location "${SPLIT}split" "$0" "$1" >/dev/null
+
+ # Restoring the previous layout.
+ kitty @ last-used-layout --no-response >/dev/null 2>&1
else
PREVIEW_MODE=1 $TERMINAL -e "$0" "$1" &
fi