Fix nnn
This commit is contained in:
parent
ca15677f7c
commit
bd8edd113a
5 changed files with 503 additions and 147 deletions
|
@ -41,6 +41,10 @@ if test -f /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
|
|||
set --prepend --export --global fish_user_paths $HOME/.nix-profile/bin /nix/var/nix/profiles/default/bin
|
||||
end
|
||||
|
||||
if has_command nnn
|
||||
source $DOTFILES_PATH/common/nnn/config.fish
|
||||
end
|
||||
|
||||
test $PWD = $HOME && begin
|
||||
cd $NICE_HOME || cd
|
||||
end
|
||||
|
|
|
@ -55,18 +55,6 @@ alias ....... "d ../../../../../.."
|
|||
alias ........ "d ../../../../../../.."
|
||||
alias ......... "d ../../../../../../../.."
|
||||
|
||||
if has_command nnn
|
||||
function r --wraps nnn --description 'Run nnn with support for jump-to-directory-on-exit via ^G'
|
||||
# TODO: this would break with multiple nnn instances, right?
|
||||
# probably need to mktemp instead
|
||||
set -u NNN_TMPFILE (mktemp)
|
||||
export NNN_TMPFILE
|
||||
nnn -P p $argv
|
||||
test -e $NNN_TMPFILE && source $NNN_TMPFILE && cp $NNN_TMPFILE ~/.nnn-last-tmpfile && rm $NNN_TMPFILE
|
||||
end
|
||||
alias l r
|
||||
end
|
||||
|
||||
has_command tmux && alias t "tmux"
|
||||
has_command rsync && alias rcp 'rsync -r -ah --progress'
|
||||
has_command bat && alias cat bat
|
||||
|
|
|
@ -11,6 +11,7 @@ font_size 12.5
|
|||
# font_features Iosevka-Heavy-Italic +calt +dlig
|
||||
|
||||
allow_remote_control yes
|
||||
listen_on unix:@kitty
|
||||
repaint_delay 5
|
||||
input_delay 5
|
||||
sync_to_monitor yes
|
||||
|
|
|
@ -1,4 +1,46 @@
|
|||
# global
|
||||
set -Ux NNN_OPTS EHacdx
|
||||
set -Ux NNN_COLORS 1234
|
||||
set -Ux NNN_PLUG p:preview
|
||||
set -Ux NNN_FCOLORS 030304020005050805030501
|
||||
|
||||
# per-shell
|
||||
set --export NNN_TMPDIR (mktemp -d)
|
||||
set --export NNN_TMPFILE $NNN_TMPDIR/tmp
|
||||
mkfifo $NNN_TMPDIR/fifo
|
||||
set --export NNN_FIFO $NNN_TMPDIR/fifo
|
||||
|
||||
function n --wraps nnn --description 'support nnn quit and change directory'
|
||||
# Block nesting of nnn in subshells
|
||||
if test -n "$NNNLVL" -a "$NNNLVL" -ge 1
|
||||
echo "nnn is already running"
|
||||
return
|
||||
end
|
||||
|
||||
# The behaviour is set to cd on quit (nnn checks if NNN_TMPFILE is set)
|
||||
# If NNN_TMPFILE is set to a custom path, it must be exported for nnn to
|
||||
# see. To cd on quit only on ^G, remove the "-x" from both lines below,
|
||||
# without changing the paths.
|
||||
if test -n "$XDG_CONFIG_HOME"
|
||||
set -x NNN_TMPFILE "$XDG_CONFIG_HOME/nnn/.lastd"
|
||||
else
|
||||
set -x NNN_TMPFILE "$HOME/.config/nnn/.lastd"
|
||||
end
|
||||
|
||||
# Unmask ^Q (, ^V etc.) (if required, see `stty -a`) to Quit nnn
|
||||
# stty start undef
|
||||
# stty stop undef
|
||||
# stty lwrap undef
|
||||
# stty lnext undef
|
||||
|
||||
# The command function allows one to alias this function to `nnn` without
|
||||
# making an infinitely recursive alias
|
||||
command nnn -P p $argv
|
||||
|
||||
if test -e $NNN_TMPFILE
|
||||
source $NNN_TMPFILE
|
||||
rm $NNN_TMPFILE
|
||||
end
|
||||
end
|
||||
|
||||
alias l 'n'
|
||||
|
|
|
@ -1,161 +1,482 @@
|
|||
#!/usr/bin/env sh
|
||||
|
||||
SPLIT="$SPLIT"
|
||||
TERMINAL="$TERMINAL"
|
||||
# Description: Terminal based file previewer
|
||||
#
|
||||
# Note: This plugin needs a "NNN_FIFO" to work. See man.
|
||||
#
|
||||
# Dependencies:
|
||||
# - Supports 5 independent methods to preview with:
|
||||
# - tmux (>=3.0), or
|
||||
# - kitty with allow_remote_control and listen_on set in kitty.conf, or
|
||||
# - QuickLook on WSL (https://github.com/QL-Win/QuickLook), or
|
||||
# - Windows Terminal (https://github.com/Microsoft/Terminal | https://aka.ms/terminal) with WSL, or
|
||||
# - $TERMINAL set to a terminal (it's xterm by default).
|
||||
# - less or $PAGER
|
||||
# - tree or exa or ls
|
||||
# - mediainfo or file
|
||||
# - mktemp
|
||||
# - unzip
|
||||
# - tar
|
||||
# - man
|
||||
# - optional: bsdtar or atool for additional archive preview
|
||||
# - optional: bat for code syntax highlighting
|
||||
# - optional: ueberzug, kitty terminal, viu or catimg for images
|
||||
# - optional: convert(ImageMagick) for playing gif preview (required for kitty image previews)
|
||||
# - optional: ffmpegthumbnailer for video thumbnails (https://github.com/dirkvdb/ffmpegthumbnailer)
|
||||
# - optional: ffmpeg for audio thumbnails
|
||||
# - optional: libreoffce for opendocument/officedocument preview
|
||||
# - optional: pdftoppm(poppler) for pdf thumbnails
|
||||
# - optional: gnome-epub-thumbnailer for epub thumbnails (https://gitlab.gnome.org/GNOME/gnome-epub-thumbnailer)
|
||||
# - optional: fontpreview for font preview (https://github.com/sdushantha/fontpreview)
|
||||
# - optional: djvulibre for djvu
|
||||
# - optional: glow or lowdown for markdown
|
||||
# - optional: w3m or lynx or elinks for html
|
||||
# - optional: set/export ICONLOOKUP as 1 to enable file icons in front of directory previews with .iconlookup
|
||||
# Icons and colors are configureable in .iconlookup
|
||||
# - optional: scope.sh file viewer from ranger.
|
||||
# 1. drop scope.sh executable in $PATH
|
||||
# 2. set/export $USE_SCOPE as 1
|
||||
# - optional: pistol file viewer (https://github.com/doronbehar/pistol).
|
||||
# 1. install pistol
|
||||
# 2. set/export $USE_PISTOL as 1
|
||||
#
|
||||
# Usage:
|
||||
# You need to set a NNN_FIFO path and a key for the plugin with NNN_PLUG,
|
||||
# then start `nnn`:
|
||||
#
|
||||
# $ nnn -a
|
||||
#
|
||||
# or
|
||||
#
|
||||
# $ NNN_FIFO=/tmp/nnn.fifo nnn
|
||||
#
|
||||
# Then launch the `preview-tui` plugin in `nnn`.
|
||||
#
|
||||
# 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 (e.g.
|
||||
# with -a), they will be independent.
|
||||
#
|
||||
# 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 (as in, the line that splits the
|
||||
# windows will be horizontal or vertical).
|
||||
#
|
||||
# Kitty users need something similar to the following in their kitty.conf:
|
||||
# - `allow_remote_control yes`
|
||||
# - `listen_on unix:$TMPDIR/kitty`
|
||||
# - `enabled_layouts splits` (optional)
|
||||
# With ImageMagick installed, this terminal can use the icat kitten to display images.
|
||||
# Refer to kitty documentation for further details.
|
||||
#
|
||||
# Iterm2 users are recommended to use viu to view images without getting pixelated.
|
||||
#
|
||||
# Windows Terminal users can set "Profile termination behavior" under "Profile > Advanced" settings
|
||||
# to automaticaly close pane on quit when exit code is 0.
|
||||
#
|
||||
# Shell: POSIX compliant
|
||||
# Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste, Mario Ortiz Manero, Luuk van Baal, @WanderLanz
|
||||
|
||||
#SPLIT="$SPLIT" # you can set a permanent split here
|
||||
#TERMINAL="$TERMINAL" # same goes for the terminal
|
||||
SPLIT_SIZE="${SPLIT_SIZE:-50}" # split size in percentage for supported previewers
|
||||
USE_SCOPE="${USE_SCOPE:-0}"
|
||||
IN_KITTY=0
|
||||
[ -z "$SSH_CONNECTION" ] && [ -n "$KITTY_WINDOW_ID" ] && kitty @ ls --no-response >/dev/null 2>&1 && IN_KITTY=1
|
||||
USE_PISTOL="${USE_PISTOL:-0}"
|
||||
ICONLOOKUP="${ICONLOOKUP:-0}"
|
||||
PAGER="${PAGER:-less -P?n -R}"
|
||||
TMPDIR="${TMPDIR:-/tmp}"
|
||||
BAT_STYLE="${BAT_STYLE:-numbers}"
|
||||
BAT_THEME="${BAT_THEME:-ansi}"
|
||||
# Consider setting NNN_PREVIEWDIR to $XDG_CACHE_HOME/nnn/previews if you want to keep previews on disk between reboots
|
||||
NNN_PREVIEWDIR="${NNN_PREVIEWDIR:-$TMPDIR/nnn/previews}"
|
||||
NNN_PREVIEWWIDTH="${NNN_PREVIEWWIDTH:-1920}"
|
||||
NNN_PREVIEWHEIGHT="${NNN_PREVIEWHEIGHT:-1080}"
|
||||
NNN_PARENT="${NNN_FIFO#*.}"
|
||||
[ "$NNN_PARENT" -eq "$NNN_PARENT" ] 2>/dev/null || NNN_PARENT=""
|
||||
FIFOPID="$TMPDIR/nnn-preview-tui-fifopid.$NNN_PARENT"
|
||||
PREVIEWPID="$TMPDIR/nnn-preview-tui-pagerpid.$NNN_PARENT"
|
||||
CURSEL="$TMPDIR/nnn-preview-tui-selection.$NNN_PARENT"
|
||||
FIFO_UEBERZUG="$TMPDIR/nnn-preview-tui-ueberzug-fifo.$NNN_PARENT"
|
||||
POSOFFSET="$TMPDIR/nnn-preview-tui-posoffset"
|
||||
|
||||
exists() { type "$1" >/dev/null 2>&1 ;}
|
||||
pkill() { command pkill "$@" >/dev/null 2>&1 ;}
|
||||
pidkill() { [ -f "$1" ] && kill "$(cat "$1")" >/dev/null 2>&1 ;}
|
||||
prompt() { printf "%b" "$@"; cfg=$(stty -g); stty raw -echo; head -c 1; stty "$cfg" ;}
|
||||
|
||||
start_preview() {
|
||||
[ "$PAGER" = "most" ] && PAGER="less -R"
|
||||
|
||||
if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.'; then
|
||||
TERMINAL=tmux
|
||||
elif [ "$IN_KITTY" == 1 ]; then
|
||||
elif [ -n "$KITTY_LISTEN_ON" ]; then
|
||||
TERMINAL=kitty
|
||||
elif [ -z "$TERMINAL" ] && [ "$TERM_PROGRAM" = "iTerm.app" ]; then
|
||||
TERMINAL=iterm
|
||||
elif [ -n "$WT_SESSION" ]; then
|
||||
TERMINAL=winterm
|
||||
else
|
||||
TERMINAL="${TERMINAL:-xterm}"
|
||||
fi
|
||||
|
||||
# we can't actually work if we're about to try the kitty method over SSH, so just exit
|
||||
[ -n "$SSH_CONNECTION" ] && [ "$TERMINAL" = "kitty" ] && exit 0
|
||||
|
||||
if [ -z "$SPLIT" ] && [ $(($(tput lines) * 2)) -gt "$(tput cols)" ]; then
|
||||
SPLIT='h'
|
||||
elif [ "$SPLIT" != 'h' ]; then
|
||||
SPLIT='v'
|
||||
fi
|
||||
|
||||
exists() {
|
||||
which "$1" >/dev/null 2>&1
|
||||
case "$TERMINAL" in
|
||||
tmux) # tmux splits are inverted
|
||||
if [ "$SPLIT" = "v" ]; then DSPLIT="h"; else DSPLIT="v"; fi
|
||||
DSPLIT="$SPLIT"
|
||||
tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1" -e "CURSEL=$CURSEL" \
|
||||
-e "TMPDIR=$TMPDIR" -e "FIFOPID=$FIFOPID" -e "POSOFFSET=$POSOFFSET" \
|
||||
-e "BAT_STYLE=$BAT_STYLE" -e "BAT_THEME=$BAT_THEME" -e "PREVIEWPID=$PREVIEWPID" \
|
||||
-e "PAGER=$PAGER" -e "ICONLOOKUP=$ICONLOOKUP" -e "NNN_PREVIEWWIDTH=$NNN_PREVIEWWIDTH" \
|
||||
-e "USE_SCOPE=$USE_SCOPE" -e "SPLIT=$SPLIT" -e "USE_PISTOL=$USE_PISTOL" \
|
||||
-e "NNN_PREVIEWDIR=$NNN_PREVIEWDIR" -e "NNN_PREVIEWHEIGHT=$NNN_PREVIEWHEIGHT" \
|
||||
-e "FIFO_UEBERZUG=$FIFO_UEBERZUG" -e "QLPATH=$2" -d"$DSPLIT" -p"$SPLIT_SIZE" "$0" "$1" ;;
|
||||
kitty) # Setting the layout for the new window. It will be restored after the script ends.
|
||||
kitty @ goto-layout splits
|
||||
# Trying to use kitty's integrated window management as the split window. All
|
||||
# environmental variables that will be used in the new window must be explicitly passed.
|
||||
kitty @ launch --no-response --title "nnn preview" --keep-focus \
|
||||
--cwd "$PWD" --env "PATH=$PATH" --env "NNN_FIFO=$NNN_FIFO" \
|
||||
--env "PREVIEW_MODE=1" --env "PAGER=$PAGER" --env "TMPDIR=$TMPDIR" \
|
||||
--env "USE_SCOPE=$USE_SCOPE" --env "SPLIT=$SPLIT" --env "TERMINAL=$TERMINAL"\
|
||||
--env "PREVIEWPID=$PREVIEWPID" --env "FIFO_UEBERZUG=$FIFO_UEBERZUG" \
|
||||
--env "ICONLOOKUP=$ICONLOOKUP" --env "NNN_PREVIEWHEIGHT=$NNN_PREVIEWHEIGHT" \
|
||||
--env "NNN_PREVIEWWIDTH=$NNN_PREVIEWWIDTH" --env "NNN_PREVIEWDIR=$NNN_PREVIEWDIR" \
|
||||
--env "USE_PISTOL=$USE_PISTOL" --env "BAT_STYLE=$BAT_STYLE" \
|
||||
--env "BAT_THEME=$BAT_THEME" --env "FIFOPID=$FIFOPID" \
|
||||
--env "CURSEL=$CURSEL" --location "${SPLIT}split" "$0" "$1" ;;
|
||||
iterm)
|
||||
command="$SHELL -c 'cd $PWD; \
|
||||
PATH=\\\"$PATH\\\" NNN_FIFO=\\\"$NNN_FIFO\\\" PREVIEW_MODE=1 PAGER=\\\"$PAGER\\\" \
|
||||
USE_SCOPE=\\\"$USE_SCOPE\\\" SPLIT=\\\"$SPLIT\\\" TERMINAL=\\\"$TERMINAL\\\" \
|
||||
PREVIEWPID=\\\"$PREVIEWPID\\\" CURSEL=\\\"$CURSEL\\\" TMPDIR=\\\"$TMPDIR\\\" \
|
||||
ICONLOOKUP=\\\"$ICONLOOKUP\\\" NNN_PREVIEWHEIGHT=\\\"$NNN_PREVIEWHEIGHT\\\" \
|
||||
NNN_PREVIEWWIDTH=\\\"$NNN_PREVIEWWIDTH\\\" NNN_PREVIEWDIR=\\\"$NNN_PREVIEWDIR\\\" \
|
||||
USE_PISTOL=\\\"$USE_PISTOL\\\" BAT_STYLE=\\\"$BAT_STYLE\\\" \
|
||||
BAT_THEME=\\\"$BAT_THEME\\\" FIFOPID=\\\"$FIFOPID\\\" \\\"$0\\\" \\\"$1\\\"'"
|
||||
if [ "$SPLIT" = "h" ]; then split="horizontally"; else split="vertically"; fi
|
||||
osascript <<-EOF
|
||||
tell application "iTerm"
|
||||
tell current session of current window
|
||||
split $split with default profile command "$command"
|
||||
end tell
|
||||
end tell
|
||||
EOF
|
||||
;;
|
||||
winterm)
|
||||
if [ "$SPLIT" = "h" ]; then split="H"; else split="V"; fi
|
||||
cmd.exe /c wt -w 0 sp -$split -s$((SPLIT_SIZE / 100)) bash -c "cd $PWD \; \
|
||||
PATH='$PATH' NNN_FIFO=$NNN_FIFO PREVIEW_MODE=1 CURSEL=$CURSEL TMPDIR=$TMPDIR \
|
||||
FIFOPID=$FIFOPID BAT_STYLE=$BAT_STYLE BAT_THEME=$BAT_THEME PREVIEWPID=$PREVIEWPID \
|
||||
PAGER='$PAGER' ICONLOOKUP=$ICONLOOKUP NNN_PREVIEWWIDTH=$NNN_PREVIEWWIDTH \
|
||||
USE_SCOPE=$USE_SCOPE SPLIT=$SPLIT USE_PISTOL=$USE_PISTOL \
|
||||
NNN_PREVIEWDIR=$NNN_PREVIEWDIR NNN_PREVIEWHEIGHT=$NNN_PREVIEWHEIGHT \
|
||||
FIFO_UEBERZUG=$FIFO_UEBERZUG QLPATH=$2 $0 $1" \; -w 0 mf previous
|
||||
;;
|
||||
*) if [ -n "$2" ]; then
|
||||
QUICKLOOK=1 QLPATH="$2" PREVIEW_MODE=1 "$0" "$1" &
|
||||
else
|
||||
PREVIEWPID="$PREVIEWPID" CURSEL="$CURSEL" PREVIEW_MODE=1 \
|
||||
FIFOPID="$FIFOPID" FIFO_UEBERZUG="$FIFO_UEBERZUG" $TERMINAL -e "$0" "$1" &
|
||||
fi ;;
|
||||
esac
|
||||
}
|
||||
|
||||
toggle_preview() {
|
||||
if exists QuickLook.exe; then
|
||||
QLPATH="QuickLook.exe"
|
||||
elif exists Bridge.exe; then
|
||||
QLPATH="Bridge.exe"
|
||||
fi
|
||||
if pidkill "$FIFOPID"; then
|
||||
[ -p "$NNN_PPIPE" ] && printf "0" > "$NNN_PPIPE"
|
||||
pidkill "$PREVIEWPID"
|
||||
pkill -f "tail --follow $FIFO_UEBERZUG"
|
||||
if [ -n "$QLPATH" ] && stat "$1"; then
|
||||
f="$(wslpath -w "$1")" && "$QLPATH" "$f" &
|
||||
fi
|
||||
else
|
||||
[ -p "$NNN_PPIPE" ] && printf "1" > "$NNN_PPIPE"
|
||||
start_preview "$1" "$QLPATH"
|
||||
fi
|
||||
}
|
||||
|
||||
fifo_pager() {
|
||||
cmd="$1"; shift
|
||||
tmpfifopath="${TMPDIR:-/tmp}/nnn-preview-tui-fifo.$$"
|
||||
cmd="$1"
|
||||
shift
|
||||
|
||||
# We use a FIFO to access $PAGER PID in jobs control
|
||||
tmpfifopath="$TMPDIR/nnn-preview-tui-fifo.$$"
|
||||
mkfifo "$tmpfifopath" || return
|
||||
|
||||
$PAGER < "$tmpfifopath" &
|
||||
printf "%s" "$!" > "$PREVIEWPID"
|
||||
|
||||
(
|
||||
exec > "$tmpfifopath"
|
||||
if [ "$cmd" = "pager" ]; then
|
||||
if exists bat; then
|
||||
bat --terminal-width="$cols" --decorations=always --color=always \
|
||||
--paging=never --style="$BAT_STYLE" --theme="$BAT_THEME" "$@" &
|
||||
else
|
||||
$PAGER "$@" &
|
||||
fi
|
||||
else
|
||||
"$cmd" "$@" &
|
||||
fi
|
||||
)
|
||||
|
||||
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
|
||||
mediainfo "$1"
|
||||
else
|
||||
file -b "$1"
|
||||
# TODO: hexyl?
|
||||
fi
|
||||
}
|
||||
|
||||
handle_mime() {
|
||||
case "$2" in
|
||||
image/jpeg) image_preview "$cols" "$lines" "$1" ;;
|
||||
image/gif) generate_preview "$cols" "$lines" "$1" "gif" ;;
|
||||
image/vnd.djvu) generate_preview "$cols" "$lines" "$1" "djvu" ;;
|
||||
image/*) generate_preview "$cols" "$lines" "$1" "image" ;;
|
||||
video/*) generate_preview "$cols" "$lines" "$1" "video" ;;
|
||||
audio/*) generate_preview "$cols" "$lines" "$1" "audio" ;;
|
||||
application/font*|application/*opentype|font/*) generate_preview "$cols" "$lines" "$1" "font" ;;
|
||||
*/*office*|*/*document*) generate_preview "$cols" "$lines" "$1" "office" ;;
|
||||
application/zip) fifo_pager unzip -l "$1" ;;
|
||||
text/troff)
|
||||
if exists man; then
|
||||
fifo_pager man -Pcat -l "$1"
|
||||
else
|
||||
fifo_pager pager "$1"
|
||||
fi ;;
|
||||
*) handle_ext "$1" "$3" "$4" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
handle_ext() {
|
||||
case "$2" in
|
||||
epub) generate_preview "$cols" "$lines" "$1" "epub" ;;
|
||||
pdf) generate_preview "$cols" "$lines" "$1" "pdf" ;;
|
||||
gz|bz2) fifo_pager tar -tvf "$1" ;;
|
||||
md) if exists glow; then
|
||||
fifo_pager glow -s dark "$1"
|
||||
elif exists lowdown; then
|
||||
fifo_pager lowdown -Tterm "$1"
|
||||
else
|
||||
fifo_pager pager "$1"
|
||||
fi ;;
|
||||
htm|html|xhtml)
|
||||
if exists w3m; then
|
||||
fifo_pager w3m "$1"
|
||||
elif exists lynx; then
|
||||
fifo_pager lynx "$1"
|
||||
elif exists elinks; then
|
||||
fifo_pager elinks "$1"
|
||||
else
|
||||
fifo_pager pager "$1"
|
||||
fi ;;
|
||||
7z|a|ace|alz|arc|arj|bz|cab|cpio|deb|jar|lha|lz|lzh|lzma|lzo\
|
||||
|rar|rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z)
|
||||
if exists atool; then
|
||||
fifo_pager atool -l "$1"
|
||||
elif exists bsdtar; then
|
||||
fifo_pager bsdtar -tvf "$1"
|
||||
fi ;;
|
||||
*) if [ "$3" = "bin" ]; then
|
||||
fifo_pager print_bin_info "$1"
|
||||
else
|
||||
fifo_pager pager "$1"
|
||||
fi ;;
|
||||
esac
|
||||
}
|
||||
|
||||
preview_file() {
|
||||
kill %- %+ 2>/dev/null && wait %- %+ 2>/dev/null
|
||||
clear
|
||||
|
||||
# Trying to use pistol if it's available.
|
||||
# if [ "$USE_PISTOL" -ne 0 ] && exists pistol; then
|
||||
# fifo_pager pistol "$1"
|
||||
# return
|
||||
# fi
|
||||
|
||||
# Detecting the exact type of the file: the encoding, mime type, and
|
||||
# extension in lowercase.
|
||||
encoding="$(file -Lb --mime-encoding -- "$1")"
|
||||
mimetype="$(file -Lb --mime-type -- "$1")"
|
||||
ext="${1##*.}"
|
||||
if [ -n "$ext" ]; then
|
||||
ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
|
||||
if [ "$USE_PISTOL" -ne 0 ] && exists pistol; then
|
||||
fifo_pager pistol "$1"
|
||||
return
|
||||
fi
|
||||
lines=$(($(tput lines)-1))
|
||||
|
||||
# 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"
|
||||
return
|
||||
fi
|
||||
|
||||
# Use QuickLook if it's available.
|
||||
if [ -n "$QUICKLOOK" ]; then
|
||||
stat "$1" && f="$(wslpath -w "$1")" && "$QLPATH" "$f" &
|
||||
return
|
||||
fi
|
||||
|
||||
# Detecting the exact type of the file: the encoding, mime type, and extension in lowercase.
|
||||
encoding="$(file -bL --mime-encoding -- "$1")"
|
||||
mimetype="$(file -bL --mime-type -- "$1")"
|
||||
ext="${1##*.}"
|
||||
[ -n "$ext" ] && ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
|
||||
lines=$(tput lines)
|
||||
cols=$(tput cols)
|
||||
|
||||
# Otherwise, falling back to the defaults.
|
||||
if [ -d "$1" ]; then
|
||||
cd "$1" || return
|
||||
if exists exa; then
|
||||
fifo_pager exa --tree --level 3 --colour=always 2>/dev/null
|
||||
if [ "$ICONLOOKUP" -ne 0 ] && [ -f "$(dirname "$0")"/.iconlookup ]; then
|
||||
[ "$SPLIT" = v ] && BSTR="\n"
|
||||
# shellcheck disable=SC2012
|
||||
ls -F --group-directories-first | head -n "$((lines - 3))" | "$(dirname "$0")"/.iconlookup -l "$cols" -B "$BSTR" -b " "
|
||||
elif exists tree; then
|
||||
fifo_pager tree -L 3 -F
|
||||
fifo_pager tree --filelimit "$(find . -maxdepth 1 | wc -l)" -L 3 -C -F --dirsfirst --noreport
|
||||
elif exists exa; then
|
||||
exa -G --group-directories-first --colour=always
|
||||
else
|
||||
fifo_pager ls --color=always
|
||||
fifo_pager ls -F --group-directories-first --color=always
|
||||
fi
|
||||
elif [ "$encoding" = "binary" ]; then
|
||||
if [ "${mimetype%%/*}" = "image" ] ; then
|
||||
if [ "$IN_KITTY" = "1" ]; 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"
|
||||
elif exists viu; then
|
||||
viu -t "$1"
|
||||
elif [ "${encoding#*)}" = "binary" ]; then
|
||||
handle_mime "$1" "$mimetype" "$ext" "bin"
|
||||
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"
|
||||
else
|
||||
if exists bat; then
|
||||
fifo_pager bat --terminal-width="$cols" --paging=never --decorations=always --color=always \
|
||||
"$1" 2>/dev/null
|
||||
else
|
||||
$PAGER "$1" &
|
||||
fi
|
||||
handle_mime "$1" "$mimetype" "$ext"
|
||||
fi
|
||||
}
|
||||
|
||||
if [ "$PREVIEW_MODE" ] ; then
|
||||
if [ ! -r "$NNN_FIFO" ] ; then
|
||||
echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
|
||||
read -r
|
||||
exit 1
|
||||
generate_preview() {
|
||||
if [ -n "$QLPATH" ] && stat "$3"; then
|
||||
f="$(wslpath -w "$3")" && "$QLPATH" "$f" &
|
||||
elif [ ! -f "$NNN_PREVIEWDIR/$3.jpg" ] || [ -n "$(find -L "$3" -newer "$NNN_PREVIEWDIR/$3.jpg")" ]; then
|
||||
mkdir -p "$NNN_PREVIEWDIR/${3%/*}"
|
||||
case $4 in
|
||||
audio) ffmpeg -i "$3" -filter_complex "scale=iw*min(1\,min($NNN_PREVIEWWIDTH/iw\,ih)):-1" "$NNN_PREVIEWDIR/$3.jpg" -y ;;
|
||||
epub) gnome-epub-thumbnailer "$3" "$NNN_PREVIEWDIR/$3.jpg" ;;
|
||||
font) fontpreview -i "$3" -o "$NNN_PREVIEWDIR/$3.jpg" ;;
|
||||
gif) if [ -p "$FIFO_UEBERZUG" ] && exists convert; then
|
||||
frameprefix="$NNN_PREVIEWDIR/$3/${3##*/}"
|
||||
if [ ! -d "$NNN_PREVIEWDIR/$3" ]; then
|
||||
mkdir -p "$NNN_PREVIEWDIR/$3"
|
||||
convert -coalesce -resize "$NNN_PREVIEWWIDTH"x"$NNN_PREVIEWHEIGHT"\> "$3" "$frameprefix.jpg" ||
|
||||
MAGICK_TMPDIR="/tmp" convert -coalesce -resize "$NNN_PREVIEWWIDTH"x"$NNN_PREVIEWHEIGHT"\> "$3" "$frameprefix.jpg"
|
||||
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"
|
||||
frames=$(($(find "$NNN_PREVIEWDIR/$3" | wc -l) - 2))
|
||||
[ $frames -lt 0 ] && return
|
||||
while true; do
|
||||
for i in $(seq 0 $frames); do
|
||||
image_preview "$1" "$2" "$frameprefix-$i.jpg"
|
||||
sleep 0.1
|
||||
done
|
||||
|
||||
# TODO: detect if file is opened and close preview until back in nnn
|
||||
|
||||
# Restoring the previous layout for kitty users. This will only work for
|
||||
# kitty >= 0.18.0.
|
||||
if [ "$TERMINAL" = "kitty" ]; then
|
||||
kitty @ last-used-layout --no-response >/dev/null 2>&1
|
||||
fi
|
||||
|
||||
exit 0
|
||||
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. It will be restored after the
|
||||
# script ends.
|
||||
kitty @ goto-layout splits >/dev/null
|
||||
|
||||
# Trying to use kitty's integrated window management as the split window.
|
||||
# All environmental variables that will be used in the new window must
|
||||
# be explicitly passed.
|
||||
kitty @ launch --no-response --title "nnn preview" --keep-focus \
|
||||
--cwd "$PWD" --env "PATH=$PATH" --env "NNN_FIFO=$NNN_FIFO" \
|
||||
--env "PREVIEW_MODE=1" --env "PAGER=$PAGER" \
|
||||
--env "USE_SCOPE=$USE_SCOPE" --env "SPLIT=$SPLIT" \
|
||||
--env "USE_PISTOL=$USE_PISTOL" \
|
||||
--location "${SPLIT}split" "$0" "$1" >/dev/null
|
||||
done &
|
||||
printf "%s" "$!" > "$PREVIEWPID"
|
||||
return
|
||||
else
|
||||
PREVIEW_MODE=1 $TERMINAL -e "$0" "$1" &
|
||||
image_preview "$1" "$2" "$3"
|
||||
return
|
||||
fi ;;
|
||||
image) if exists convert; then
|
||||
convert "$3" -flatten -resize "$NNN_PREVIEWWIDTH"x"$NNN_PREVIEWHEIGHT"\> "$NNN_PREVIEWDIR/$3.jpg"
|
||||
else
|
||||
image_preview "$1" "$2" "$3" && return
|
||||
fi ;;
|
||||
office) libreoffice --convert-to jpg "$3" --outdir "$NNN_PREVIEWDIR/${3%/*}"
|
||||
filename="$(printf "%s" "${3##*/}" | cut -d. -f1)"
|
||||
mv "$NNN_PREVIEWDIR/${3%/*}/$filename.jpg" "$NNN_PREVIEWDIR/$3.jpg" ;;
|
||||
pdf) pdftoppm -jpeg -f 1 -singlefile "$3" "$NNN_PREVIEWDIR/$3" ;;
|
||||
djvu) ddjvu -format=ppm -page=1 "$3" "$NNN_PREVIEWDIR/$3.jpg" ;;
|
||||
video) ffmpegthumbnailer -m -s0 -i "$3" -o "$NNN_PREVIEWDIR/$3.jpg" || rm "$NNN_PREVIEWDIR/$3.jpg" ;;
|
||||
esac
|
||||
fi
|
||||
if [ -f "$NNN_PREVIEWDIR/$3.jpg" ]; then
|
||||
image_preview "$1" "$2" "$NNN_PREVIEWDIR/$3.jpg"
|
||||
else
|
||||
fifo_pager print_bin_info "$3"
|
||||
fi
|
||||
} >/dev/null 2>&1
|
||||
|
||||
image_preview() {
|
||||
clear
|
||||
exec >/dev/tty
|
||||
if [ "$TERMINAL" = "kitty" ]; then
|
||||
# Kitty terminal users can use the native image preview method
|
||||
kitty +kitten icat --silent --scale-up --place "$1"x"$2"@0x0 --transfer-mode=stream --stdin=no "$3" &
|
||||
elif exists ueberzug; then
|
||||
ueberzug_layer "$1" "$2" "$3" && return
|
||||
elif exists catimg; then
|
||||
catimg "$3" &
|
||||
elif exists viu; then
|
||||
viu -t "$3" &
|
||||
else
|
||||
fifo_pager print_bin_info "$3" && return
|
||||
fi
|
||||
printf "%s" "$!" > "$PREVIEWPID"
|
||||
}
|
||||
|
||||
ueberzug_layer() {
|
||||
[ -f "$POSOFFSET" ] && read -r x y < "$POSOFFSET"
|
||||
printf '{"action": "add", "identifier": "nnn_ueberzug", "x": %d, "y": %d, "width": "%d", "height": "%d", "scaler": "fit_contain", "path": "%s"}\n'\
|
||||
"${x:-0}" "${y:-0}" "$1" "$2" "$3" > "$FIFO_UEBERZUG"
|
||||
}
|
||||
|
||||
ueberzug_remove() {
|
||||
printf '{"action": "remove", "identifier": "nnn_ueberzug"}\n' > "$FIFO_UEBERZUG"
|
||||
}
|
||||
|
||||
winch_handler() {
|
||||
clear
|
||||
pidkill "$PREVIEWPID"
|
||||
if [ -p "$FIFO_UEBERZUG" ]; then
|
||||
pkill -f "tail --follow $FIFO_UEBERZUG"
|
||||
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
|
||||
fi
|
||||
preview_file "$(cat "$CURSEL")"
|
||||
}
|
||||
|
||||
preview_fifo() {
|
||||
while read -r selection; do
|
||||
if [ -n "$selection" ]; then
|
||||
pidkill "$PREVIEWPID"
|
||||
[ -p "$FIFO_UEBERZUG" ] && ueberzug_remove
|
||||
[ "$selection" = "close" ] && break
|
||||
preview_file "$selection"
|
||||
printf "%s" "$selection" > "$CURSEL"
|
||||
fi
|
||||
done < "$NNN_FIFO"
|
||||
sleep 0.1 # make sure potential preview by winch_handler is killed
|
||||
pkill -P "$$"
|
||||
}
|
||||
|
||||
if [ "$PREVIEW_MODE" ]; then
|
||||
if [ "$TERMINAL" != "kitty" ] && exists ueberzug; then
|
||||
mkfifo "$FIFO_UEBERZUG"
|
||||
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
|
||||
fi
|
||||
|
||||
preview_file "$PWD/$1"
|
||||
preview_fifo &
|
||||
printf "%s" "$!" > "$FIFOPID"
|
||||
printf "%s" "$PWD/$1" > "$CURSEL"
|
||||
trap 'winch_handler; wait' WINCH
|
||||
trap 'rm "$PREVIEWPID" "$CURSEL" "$FIFO_UEBERZUG" "$FIFOPID" "$POSOFFSET" 2>/dev/null' INT HUP EXIT
|
||||
wait "$!" 2>/dev/null
|
||||
exit 0
|
||||
else
|
||||
if [ ! -r "$NNN_FIFO" ]; then
|
||||
clear
|
||||
prompt "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')\nPlease read Usage in preview-tui."
|
||||
elif [ "$KITTY_WINDOW_ID" ] && [ -z "$TMUX" ] && [ -z "$KITTY_LISTEN_ON" ]; then
|
||||
clear
|
||||
prompt "\$KITTY_LISTEN_ON not set!\nPlease read Usage in preview-tui."
|
||||
else
|
||||
toggle_preview "$1" &
|
||||
fi
|
||||
fi
|
||||
|
|
Reference in a new issue