aboutsummaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorGravatar lvgx <l@vgx.fr>2020-05-27 20:02:22 +0200
committerGravatar GitHub <noreply@github.com>2020-05-27 23:32:22 +0530
commitbbcd89c8d6e1d81ea2e128468e98b3c42dca7d3d (patch)
tree4fa5d42612f0874f0ff70edd6a3b5a5857062d6f /plugins
parentb6762e292ca4f7ddf73f5752b571d3e0f04adbbb (diff)
downloadnnn-bbcd89c8d6e1d81ea2e128468e98b3c42dca7d3d.tar.gz
preview-tui: async version using $PAGER to scroll preview (#597)
Diffstat (limited to 'plugins')
-rwxr-xr-xplugins/preview-tui42
1 files changed, 28 insertions, 14 deletions
diff --git a/plugins/preview-tui b/plugins/preview-tui
index 5e1fd67..b156cac 100755
--- a/plugins/preview-tui
+++ b/plugins/preview-tui
@@ -4,7 +4,7 @@
#
# Note: This plugin needs a "NNN_FIFO" to work.
#
-# Dependencies: tmux (>=3.0) or xterm (or set $TERMINAL), file, tree
+# Dependencies: tmux (>=3.0) or xterm or $TERMINAL, less or $PAGER, file, tree
#
# Usage:
# You need to set a NNN_FIFO path and set a key for the plugin,
@@ -22,24 +22,38 @@
# Authors: Todd Yamakawa, Léo Villeveygoux
TERMINAL="${TERMINAL:-xterm}"
+PAGER="${PAGER:-less}"
preview_file () {
+ kill %- %+ 2>/dev/null
clear
- lines=$(($(tput lines)-1))
- cols=$(tput cols)
+
+ # prevent shell pipe reuse
+ tmpfifopath="${TMPDIR:-/tmp}/nnn-preview-tui-fifo.$$"
+ mkfifo "$tmpfifopath" || return
+
encoding="$(file -b --mime-encoding "$1")"
- if [ -d "$1" ]; then
- # Print directory tree
- cd "$1" && tree | head -n $lines | cut -c 1-"$cols"
- elif [ "$encoding" = "binary" ] ; then
- # Binary file: just print filetype info
- echo "-------- Binary file --------"
- file -b "$1"
- else
- # Text file: print file head
- head -n $lines "$1" | cut -c 1-"$cols"
- fi
+ $PAGER < "$tmpfifopath" &
+
+ (
+ exec > "$tmpfifopath"
+
+ if [ -d "$1" ]; then
+ # Print directory tree
+ cd "$1" && tree
+ elif [ "$encoding" = "binary" ] ; then
+ # Binary file: just print filetype info
+ echo "-------- Binary file --------"
+ file -b "$1"
+ else
+ # Text file:
+ cat "$1"
+ fi &
+
+ )
+
+ rm "$tmpfifopath"
}
if [ "$PREVIEW_MODE" ] ; then