mirror of
https://github.com/jarun/nnn.git
synced 2024-11-28 13:51:31 +00:00
preview-tui: simplify, add generic fifo_pager() (#625)
* preview-tui: simplify, add generic fifo_pager() I've commented new filetype checks for now, as we need to discuss which ones should be included by default, keeping in mind that this is supposed to be a minimal, adaptable plugin. * preview-tui: preview with man, tar, unzip by default
This commit is contained in:
parent
5dbb511ede
commit
01da467547
|
@ -4,19 +4,25 @@
|
||||||
#
|
#
|
||||||
# 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 $TERMINAL, less or $PAGER, file, tree
|
# Dependencies: tmux (>=3.0) or xterm or $TERMINAL, less or $PAGER,
|
||||||
|
# file, stat, tree, man, tar, unzip, ...
|
||||||
|
# ... add you own! (see examples in code)
|
||||||
#
|
#
|
||||||
# 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,
|
||||||
# then start `nnn`:
|
# then start `nnn`:
|
||||||
#
|
#
|
||||||
|
# $ nnn -a
|
||||||
|
#
|
||||||
|
# or
|
||||||
|
#
|
||||||
# $ NNN_FIFO=/tmp/nnn.fifo nnn
|
# $ NNN_FIFO=/tmp/nnn.fifo nnn
|
||||||
#
|
#
|
||||||
# Then in `nnn`, launch the `preview-tui` plugin.
|
# Then in `nnn`, launch the `preview-tui` plugin.
|
||||||
#
|
#
|
||||||
# If you provide the same NNN_FIFO to all nnn instances, there will be a
|
# If you provide the same NNN_FIFO to all nnn instances, there will be a
|
||||||
# single common preview window. I you provide different FIFO path, they
|
# single common preview window. I you provide different FIFO path (e.g.
|
||||||
# will be independent.
|
# with -a), they will be independent.
|
||||||
#
|
#
|
||||||
# Configure SPLIT to either "h" or "v" to set a 'h'orizontal split or a
|
# Configure SPLIT to either "h" or "v" to set a 'h'orizontal split or a
|
||||||
# 'v'ertical split
|
# 'v'ertical split
|
||||||
|
@ -25,37 +31,13 @@
|
||||||
# Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste
|
# Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste
|
||||||
|
|
||||||
TERMINAL="${TERMINAL:-xterm}"
|
TERMINAL="${TERMINAL:-xterm}"
|
||||||
PAGER="${PAGER:-less}"
|
PAGER="${PAGER:-less -R}"
|
||||||
SPLIT=
|
|
||||||
|
|
||||||
lines=$(($(tput lines)-1))
|
fifo_pager() {
|
||||||
cols=$(tput cols)
|
cmd="$1"
|
||||||
|
shift
|
||||||
|
|
||||||
beginswith() {
|
# We use a FIFO to access $PAGER PID in jobs control
|
||||||
case $1 in "$2"*) true;; *) false;; esac;
|
|
||||||
}
|
|
||||||
|
|
||||||
preview_file () {
|
|
||||||
kill "$(jobs -p)" 2>/dev/null
|
|
||||||
clear
|
|
||||||
|
|
||||||
encoding="$(file -b --mime-encoding "$1")"
|
|
||||||
|
|
||||||
# Detect mime type
|
|
||||||
mimetype="$(file --dereference --brief --mime-type -- "$1")"
|
|
||||||
|
|
||||||
# Detect file extention
|
|
||||||
ext="${1##*.}"
|
|
||||||
if ! [ -z "$ext" ]; then
|
|
||||||
ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -d "$1" ]; then
|
|
||||||
# Print directory tree
|
|
||||||
|
|
||||||
cd "$1" || return
|
|
||||||
|
|
||||||
# We use a FIFO to access less PID
|
|
||||||
tmpfifopath="${TMPDIR:-/tmp}/nnn-preview-tui-fifo.$$"
|
tmpfifopath="${TMPDIR:-/tmp}/nnn-preview-tui-fifo.$$"
|
||||||
mkfifo "$tmpfifopath" || return
|
mkfifo "$tmpfifopath" || return
|
||||||
|
|
||||||
|
@ -63,24 +45,44 @@ preview_file () {
|
||||||
|
|
||||||
(
|
(
|
||||||
exec > "$tmpfifopath"
|
exec > "$tmpfifopath"
|
||||||
tree&
|
"$cmd" "$@" &
|
||||||
)
|
)
|
||||||
|
|
||||||
rm "$tmpfifopath"
|
rm "$tmpfifopath"
|
||||||
#elif beginswith "$mimetype" "image/" ; then
|
}
|
||||||
# viu "$1" | head -n "$lines"
|
|
||||||
#elif beginswith "$mimetype" "text/troff" ; then
|
preview_file () {
|
||||||
# man -l "$1" &
|
kill %- %+ 2>/dev/null
|
||||||
elif beginswith "$mimetype" "application/zip" ; then
|
clear
|
||||||
#$PAGER "$(zip -sf "$1")" &
|
|
||||||
$PAGER "$(unzip -l "$1")" &
|
encoding="$(file -Lb --mime-encoding -- "$1")"
|
||||||
|
|
||||||
|
# Detect mime type
|
||||||
|
mimetype="$(file -Lb --mime-type -- "$1")"
|
||||||
|
|
||||||
|
# Detect file extention - use if you need
|
||||||
|
ext="${1##*.}"
|
||||||
|
if [ -n "$ext" ]; then
|
||||||
|
ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -d "$1" ]; then
|
||||||
|
# Print directory tree
|
||||||
|
cd "$1" || return
|
||||||
|
fifo_pager tree
|
||||||
|
#elif [ "${mimetype%%/*}" = "image" ] ; then
|
||||||
|
# catimg "$1"
|
||||||
|
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
|
elif [ "$ext" = "gz" ] || [ "$ext" = "bz2" ] ; then
|
||||||
$PAGER "$(tar -tvf "$1")" &
|
fifo_pager tar -tvf "$1"
|
||||||
elif [ "$encoding" = "binary" ] ; then
|
elif [ "$encoding" = "binary" ] ; then
|
||||||
# Binary file: just print filetype info
|
# Binary file: just print filetype info
|
||||||
echo "-------- binary file --------"
|
echo "-------- binary file --------"
|
||||||
file -b "$1"
|
file -b "$1"
|
||||||
echo ''
|
echo
|
||||||
stat "$1"
|
stat "$1"
|
||||||
else
|
else
|
||||||
# Text file:
|
# Text file:
|
||||||
|
@ -105,13 +107,9 @@ if [ "$PREVIEW_MODE" ] ; then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -e "${TMUX%%,*}" ] && [ "$(tmux -V | cut -c6)" -eq 3 ] ; then
|
if [ -e "${TMUX%%,*}" ] && [ "$(tmux -V | cut -c6)" -eq 3 ] ; then
|
||||||
if [ -z "$SPLIT" ]; then
|
if [ -z "$SPLIT" ] && [ $(($(tput lines) * 2)) -gt "$(tput cols)" ] ; then
|
||||||
if [ "$(( lines * 2 ))" -gt "$cols" ]; then
|
|
||||||
SPLIT='v'
|
SPLIT='v'
|
||||||
else
|
elif [ "$SPLIT" != 'v' ] ; then
|
||||||
SPLIT='h'
|
|
||||||
fi
|
|
||||||
elif [ "$SPLIT" != "h" ] && [ "$SPLIT" != "v" ] ; then
|
|
||||||
SPLIT='h'
|
SPLIT='h'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue