Browse Source

preview-tui: async version using $PAGER to scroll preview (#597)

master
lvgx GitHub 5 years ago
parent
commit
bbcd89c8d6
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 14 deletions
  1. +28
    -14
      plugins/preview-tui

+ 28
- 14
plugins/preview-tui View File

@@ -4,7 +4,7 @@
# #
# Note: This plugin needs a "NNN_FIFO" to work. # 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: # 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 set a key for the plugin,
@@ -22,24 +22,38 @@
# Authors: Todd Yamakawa, Léo Villeveygoux # Authors: Todd Yamakawa, Léo Villeveygoux


TERMINAL="${TERMINAL:-xterm}" TERMINAL="${TERMINAL:-xterm}"
PAGER="${PAGER:-less}"


preview_file () { preview_file () {
kill %- %+ 2>/dev/null
clear clear
lines=$(($(tput lines)-1)) # prevent shell pipe reuse
cols=$(tput cols) tmpfifopath="${TMPDIR:-/tmp}/nnn-preview-tui-fifo.$$"
mkfifo "$tmpfifopath" || return

encoding="$(file -b --mime-encoding "$1")" encoding="$(file -b --mime-encoding "$1")"


if [ -d "$1" ]; then $PAGER < "$tmpfifopath" &
# Print directory tree (
cd "$1" && tree | head -n $lines | cut -c 1-"$cols" exec > "$tmpfifopath"
elif [ "$encoding" = "binary" ] ; then if [ -d "$1" ]; then
# Binary file: just print filetype info # Print directory tree
echo "-------- Binary file --------" cd "$1" && tree
file -b "$1" elif [ "$encoding" = "binary" ] ; then
else # Binary file: just print filetype info
# Text file: print file head echo "-------- Binary file --------"
head -n $lines "$1" | cut -c 1-"$cols" file -b "$1"
fi else
# Text file:
cat "$1"
fi &

)

rm "$tmpfifopath"
} }


if [ "$PREVIEW_MODE" ] ; then if [ "$PREVIEW_MODE" ] ; then


||||||
x
 
000:0
Loading…
Cancel
Save