2023-04-19 01:20:41 +00:00
|
|
|
#!/usr/bin/env bash
|
2020-05-05 23:08:10 +00:00
|
|
|
|
2020-06-09 01:09:35 +00:00
|
|
|
# Description: Terminal based file previewer
|
2020-05-05 23:08:10 +00:00
|
|
|
#
|
2020-06-09 01:09:35 +00:00
|
|
|
# Note: This plugin needs a "NNN_FIFO" to work. See man.
|
2020-05-05 23:08:10 +00:00
|
|
|
#
|
2020-06-09 01:09:35 +00:00
|
|
|
# Dependencies:
|
2023-02-02 10:25:39 +00:00
|
|
|
# - Supports 6 independent methods to preview with:
|
2021-05-15 17:32:01 +00:00
|
|
|
# - tmux (>=3.0), or
|
|
|
|
# - kitty with allow_remote_control and listen_on set in kitty.conf, or
|
2023-02-02 10:25:39 +00:00
|
|
|
# - wezterm (https://wezfurlong.org/wezterm), or
|
2022-01-17 13:20:27 +00:00
|
|
|
# - QuickLook on WSL (https://github.com/QL-Win/QuickLook), or
|
|
|
|
# - Windows Terminal (https://github.com/Microsoft/Terminal | https://aka.ms/terminal) with WSL, or
|
2023-02-07 03:59:07 +00:00
|
|
|
# - $NNN_TERMINAL set to a terminal (it's xterm by default).
|
|
|
|
# - less or $NNN_PAGER
|
2023-04-19 23:06:43 +00:00
|
|
|
# - tree or exa or (GNU) ls
|
2021-05-15 17:32:01 +00:00
|
|
|
# - mediainfo or file
|
|
|
|
# - mktemp
|
|
|
|
# - unzip
|
|
|
|
# - tar
|
|
|
|
# - man
|
2021-05-30 21:48:10 +00:00
|
|
|
# - optional: bsdtar or atool for additional archive preview
|
2021-05-15 17:32:01 +00:00
|
|
|
# - optional: bat for code syntax highlighting
|
2023-02-02 10:25:39 +00:00
|
|
|
# - optional: ueberzug, kitty terminal, wezterm terminal, viu, catimg or chafa for images
|
2022-05-23 15:25:19 +00:00
|
|
|
# - optional: convert(ImageMagick) for playing gif preview (required for kitty image previews)
|
2021-05-30 21:48:10 +00:00
|
|
|
# - 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)
|
2022-03-25 16:21:11 +00:00
|
|
|
# - optional: djvulibre for djvu
|
2021-05-30 21:48:10 +00:00
|
|
|
# - optional: glow or lowdown for markdown
|
|
|
|
# - optional: w3m or lynx or elinks for html
|
2023-02-07 03:59:07 +00:00
|
|
|
# - optional: set/export NNN_ICONLOOKUP as 1 to enable file icons in front of directory previews with .iconlookup
|
2023-04-03 04:49:03 +00:00
|
|
|
# Icons and colors are configurable in .iconlookup
|
2021-05-15 17:32:01 +00:00
|
|
|
# - optional: scope.sh file viewer from ranger.
|
|
|
|
# 1. drop scope.sh executable in $PATH
|
2023-02-07 03:59:07 +00:00
|
|
|
# 2. set/export $NNN_SCOPE as 1
|
2021-05-15 17:32:01 +00:00
|
|
|
# - optional: pistol file viewer (https://github.com/doronbehar/pistol).
|
|
|
|
# 1. install pistol
|
2023-02-07 03:59:07 +00:00
|
|
|
# 2. set/export $NNN_PISTOL as 1
|
2020-05-05 23:08:10 +00:00
|
|
|
#
|
2020-05-06 05:12:29 +00:00
|
|
|
# Usage:
|
2020-06-09 01:09:35 +00:00
|
|
|
# You need to set a NNN_FIFO path and a key for the plugin with NNN_PLUG,
|
2020-05-05 23:08:10 +00:00
|
|
|
# then start `nnn`:
|
|
|
|
#
|
2020-06-02 08:56:50 +00:00
|
|
|
# $ nnn -a
|
|
|
|
#
|
|
|
|
# or
|
|
|
|
#
|
2020-05-05 23:08:10 +00:00
|
|
|
# $ NNN_FIFO=/tmp/nnn.fifo nnn
|
|
|
|
#
|
2021-05-15 17:32:01 +00:00
|
|
|
# Then launch the `preview-tui` plugin in `nnn`.
|
2020-05-05 23:08:10 +00:00
|
|
|
#
|
|
|
|
# If you provide the same NNN_FIFO to all nnn instances, there will be a
|
2020-07-14 23:19:13 +00:00
|
|
|
# single common preview window. If you provide different FIFO path (e.g.
|
2020-06-02 08:56:50 +00:00
|
|
|
# with -a), they will be independent.
|
2020-05-05 23:08:10 +00:00
|
|
|
#
|
2020-06-09 01:09:35 +00:00
|
|
|
# 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
|
2023-02-07 03:59:07 +00:00
|
|
|
# different terminal window will be used ($NNN_TERMINAL).
|
2020-06-09 01:09:35 +00:00
|
|
|
#
|
2022-05-23 15:25:19 +00:00
|
|
|
# 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.
|
2020-06-01 08:42:34 +00:00
|
|
|
#
|
2023-02-07 03:59:07 +00:00
|
|
|
# Wezterm should work out of the box. If `NNN_PREVIEWIMGPROG` is not specified it will use
|
2023-02-02 10:25:39 +00:00
|
|
|
# built in iTerm2 image protocol.
|
|
|
|
#
|
2023-04-19 23:06:43 +00:00
|
|
|
# Note that GNU ls is used for its `--group-directories-first` flag.
|
|
|
|
# On MacOS this may be installed with `brew install coreutils`, or the flag can be removed.
|
|
|
|
# iTerm2 users are recommended to use viu to view images without getting pixelated.
|
2021-10-14 20:07:11 +00:00
|
|
|
#
|
2022-01-17 13:20:27 +00:00
|
|
|
# Windows Terminal users can set "Profile termination behavior" under "Profile > Advanced" settings
|
2023-04-03 04:49:03 +00:00
|
|
|
# to automatically close pane on quit when exit code is 0.
|
2022-01-17 13:20:27 +00:00
|
|
|
#
|
2023-05-12 12:20:04 +00:00
|
|
|
# When specifying a different terminal, additional arguments are supported. In particular, you can
|
|
|
|
# append a specific title to the terminal and set it to "nofocus" in your WM config.
|
|
|
|
# E.g for alacritty and i3, you can set $NNN_TERMINAL to 'alacritty --title preview-tui' and add
|
|
|
|
# 'no_focus [title="preview-tui"]' to your i3 config file.
|
|
|
|
#
|
2023-04-19 23:06:43 +00:00
|
|
|
# Shell: Bash (for environment manipulation through arrays)
|
2022-01-17 13:20:27 +00:00
|
|
|
# Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste, Mario Ortiz Manero, Luuk van Baal, @WanderLanz
|
2020-05-05 23:08:10 +00:00
|
|
|
|
2023-02-07 03:59:07 +00:00
|
|
|
NNN_SPLIT=${NNN_SPLIT:-} # Set permanent split direction
|
|
|
|
NNN_TERMINAL=${NNN_TERMINAL:-} # Set external terminal to be used
|
|
|
|
NNN_SPLITSIZE=${NNN_SPLITSIZE:-50} # Set previewer split size percentage
|
|
|
|
TMPDIR=${TMPDIR:-/tmp}
|
|
|
|
NNN_PARENT=${NNN_FIFO#*.}
|
|
|
|
[ "$NNN_PARENT" -eq "$NNN_PARENT" ] 2>/dev/null || NNN_PARENT="" # Make empty if non-numeric
|
2023-04-19 01:20:41 +00:00
|
|
|
ENVVARS=(
|
|
|
|
"PWD=$PWD"
|
|
|
|
"PATH=$PATH"
|
|
|
|
"NNN_FIFO=$NNN_FIFO"
|
|
|
|
"NNN_SCOPE=${NNN_SCOPE:-0}"
|
|
|
|
"NNN_PISTOL=${NNN_PISTOL:-0}"
|
|
|
|
"NNN_ICONLOOKUP=${NNN_ICONLOOKUP:-0}"
|
|
|
|
"NNN_PAGER=${NNN_PAGER:-less -P?n -R}"
|
|
|
|
"NNN_BATTHEME=${NNN_BATTHEME:-ansi}"
|
|
|
|
"NNN_BATSTYLE=${NNN_BATSTYLE:-numbers}"
|
|
|
|
"NNN_PREVIEWWIDTH=${NNN_PREVIEWWIDTH:-1920}"
|
|
|
|
"NNN_PREVIEWHEIGHT=${NNN_PREVIEWHEIGHT:-1080}"
|
|
|
|
"NNN_PREVIEWDIR=${NNN_PREVIEWDIR:-$TMPDIR/nnn/previews}"
|
|
|
|
"NNN_PREVIEWIMGPROG=${NNN_PREVIEWIMGPROG:-}"
|
|
|
|
"FIFOPID=$TMPDIR/nnn-preview-tui-fifopid.$NNN_PARENT"
|
|
|
|
"FIFOPATH=$TMPDIR/nnn-preview-tui-fifo.$NNN_PARENT"
|
|
|
|
"PREVIEWPID=$TMPDIR/nnn-preview-tui-previewpid.$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"
|
|
|
|
)
|
2023-02-07 03:59:07 +00:00
|
|
|
|
2023-02-19 20:52:55 +00:00
|
|
|
trap '' PIPE
|
2022-07-22 08:49:13 +00:00
|
|
|
exists() { type "$1" >/dev/null 2>&1 ;}
|
|
|
|
pkill() { command pkill "$@" >/dev/null 2>&1 ;}
|
2023-04-19 23:06:43 +00:00
|
|
|
prompt() { clear; printf "%b" "$@"; cfg=$(stty -g); stty raw -echo; head -c 1; stty "$cfg" ;}
|
2023-02-19 20:52:55 +00:00
|
|
|
pidkill() {
|
2023-04-19 23:06:43 +00:00
|
|
|
if [ -f "$1" ]; then
|
|
|
|
PID="$(cat "$1" 2>/dev/null)" || return 1
|
|
|
|
kill "$PID" >/dev/null 2>&1
|
|
|
|
RET=$?
|
|
|
|
wait "$PID" 2>/dev/null
|
|
|
|
return $RET
|
|
|
|
fi
|
|
|
|
return 1
|
2023-02-19 20:52:55 +00:00
|
|
|
}
|
2021-12-24 00:39:21 +00:00
|
|
|
|
2021-05-16 00:11:05 +00:00
|
|
|
start_preview() {
|
2023-04-19 23:06:43 +00:00
|
|
|
if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.'; then
|
|
|
|
NNN_TERMINAL=tmux
|
|
|
|
elif [ -n "$KITTY_LISTEN_ON" ]; then
|
|
|
|
NNN_TERMINAL=kitty
|
|
|
|
elif [ -n "$WEZTERM_PANE" ]; then
|
|
|
|
NNN_TERMINAL=wezterm
|
|
|
|
elif [ -z "$NNN_TERMINAL" ] && [ "$TERM_PROGRAM" = "iTerm.app" ]; then
|
|
|
|
NNN_TERMINAL=iterm
|
|
|
|
elif [ -n "$WT_SESSION" ]; then
|
|
|
|
NNN_TERMINAL=winterm
|
|
|
|
else
|
|
|
|
NNN_TERMINAL="${NNN_TERMINAL:-xterm}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$NNN_SPLIT" ] && [ $(($(tput lines) * 2)) -gt "$(tput cols)" ]; then
|
|
|
|
NNN_SPLIT='h'
|
|
|
|
elif [ "$NNN_SPLIT" != 'h' ]; then
|
|
|
|
NNN_SPLIT='v'
|
|
|
|
fi
|
|
|
|
|
|
|
|
ENVVARS+=("NNN_TERMINAL=$NNN_TERMINAL" "NNN_SPLIT=$NNN_SPLIT" "QLPATH=$2" "PREVIEW_MODE=1")
|
|
|
|
case "$NNN_TERMINAL" in
|
|
|
|
iterm|winterm) # need run in separate shell command: escape
|
|
|
|
ENVVARS=("${ENVVARS[@]/#/\\\"}")
|
|
|
|
ENVVARS=("${ENVVARS[@]/%/\\\"}")
|
|
|
|
command="$SHELL -c 'env ${ENVVARS[*]} \\\"$0\\\" \\\"$1\\\"'" ;;
|
|
|
|
esac
|
|
|
|
|
2023-02-07 03:59:07 +00:00
|
|
|
case "$NNN_TERMINAL" in
|
2021-05-16 00:11:05 +00:00
|
|
|
tmux) # tmux splits are inverted
|
2023-04-19 23:06:43 +00:00
|
|
|
ENVVARS=("${ENVVARS[@]/#/-e}")
|
2023-02-07 03:59:07 +00:00
|
|
|
if [ "$NNN_SPLIT" = "v" ]; then split="h"; else split="v"; fi
|
2023-04-19 23:06:43 +00:00
|
|
|
tmux split-window "${ENVVARS[@]}" -d"$split" -p"$NNN_SPLITSIZE" "$0" "$1" ;;
|
2021-05-16 00:11:05 +00:00
|
|
|
kitty) # Setting the layout for the new window. It will be restored after the script ends.
|
2023-04-19 23:06:43 +00:00
|
|
|
ENVVARS=("${ENVVARS[@]/#/--env=}")
|
2021-05-16 00:11:05 +00:00
|
|
|
kitty @ goto-layout splits
|
2023-02-07 03:59:07 +00:00
|
|
|
# Trying to use kitty's integrated window management as the split window.
|
2023-04-19 01:20:41 +00:00
|
|
|
kitty @ launch --no-response --title "preview-tui" --keep-focus \
|
2023-04-19 23:06:43 +00:00
|
|
|
--cwd "$PWD" "${ENVVARS[@]}" --location "${NNN_SPLIT}split" "$0" "$1" ;;
|
2023-02-02 08:14:56 +00:00
|
|
|
wezterm)
|
2023-04-19 23:06:43 +00:00
|
|
|
export "${ENVVARS[@]}"
|
2023-02-07 03:59:07 +00:00
|
|
|
if [ "$NNN_SPLIT" = "v" ]; then split="--horizontal"; else split="--bottom"; fi
|
2023-04-19 23:06:43 +00:00
|
|
|
wezterm cli split-pane --cwd "$PWD" $split --percent "$NNN_SPLITSIZE" "$0" "$1" >/dev/null
|
2023-02-07 03:59:07 +00:00
|
|
|
wezterm cli activate-pane-direction Prev ;;
|
2021-10-14 20:07:11 +00:00
|
|
|
iterm)
|
2023-02-07 03:59:07 +00:00
|
|
|
if [ "$NNN_SPLIT" = "h" ]; then split="horizontally"; else split="vertically"; fi
|
2021-10-14 20:07:11 +00:00
|
|
|
osascript <<-EOF
|
|
|
|
tell application "iTerm"
|
|
|
|
tell current session of current window
|
2023-04-19 23:06:43 +00:00
|
|
|
split $split with default profile command "$command"
|
2021-10-14 20:07:11 +00:00
|
|
|
end tell
|
|
|
|
end tell
|
|
|
|
EOF
|
|
|
|
;;
|
2022-01-17 13:20:27 +00:00
|
|
|
winterm)
|
2023-02-07 03:59:07 +00:00
|
|
|
if [ "$NNN_SPLIT" = "h" ]; then split="H"; else split="V"; fi
|
2023-04-19 23:06:43 +00:00
|
|
|
wt -w 0 sp -$split -s"0.$NNN_SPLITSIZE" "$command" \; -w 0 mf previous 2>/dev/null ;;
|
2021-05-16 00:11:05 +00:00
|
|
|
*) if [ -n "$2" ]; then
|
2023-04-19 23:06:43 +00:00
|
|
|
env "${ENVVARS[@]}" QUICKLOOK=1 "$0" "$1" &
|
2021-05-16 00:11:05 +00:00
|
|
|
else
|
2023-05-12 12:20:04 +00:00
|
|
|
# shellcheck disable=SC2086 # (allow arguments)
|
|
|
|
env "${ENVVARS[@]}" $NNN_TERMINAL -e "$0" "$1" &
|
2021-05-16 00:11:05 +00:00
|
|
|
fi ;;
|
|
|
|
esac
|
2022-07-22 08:49:13 +00:00
|
|
|
}
|
2021-04-26 15:21:52 +00:00
|
|
|
|
2021-05-16 00:11:05 +00:00
|
|
|
toggle_preview() {
|
2023-04-19 23:06:43 +00:00
|
|
|
export "${ENVVARS[@]}"
|
2021-04-26 15:21:52 +00:00
|
|
|
if exists QuickLook.exe; then
|
|
|
|
QLPATH="QuickLook.exe"
|
|
|
|
elif exists Bridge.exe; then
|
|
|
|
QLPATH="Bridge.exe"
|
|
|
|
fi
|
2022-07-22 08:49:13 +00:00
|
|
|
if pidkill "$FIFOPID"; then
|
2021-09-24 10:12:13 +00:00
|
|
|
[ -p "$NNN_PPIPE" ] && printf "0" > "$NNN_PPIPE"
|
2022-07-22 08:49:13 +00:00
|
|
|
pidkill "$PREVIEWPID"
|
2021-06-04 02:10:35 +00:00
|
|
|
pkill -f "tail --follow $FIFO_UEBERZUG"
|
2021-05-16 00:11:05 +00:00
|
|
|
if [ -n "$QLPATH" ] && stat "$1"; then
|
|
|
|
f="$(wslpath -w "$1")" && "$QLPATH" "$f" &
|
2021-04-26 15:21:52 +00:00
|
|
|
fi
|
|
|
|
else
|
2021-09-24 10:12:13 +00:00
|
|
|
[ -p "$NNN_PPIPE" ] && printf "1" > "$NNN_PPIPE"
|
2021-09-22 15:15:35 +00:00
|
|
|
start_preview "$1" "$QLPATH"
|
2021-04-26 15:21:52 +00:00
|
|
|
fi
|
2020-06-09 01:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 08:56:50 +00:00
|
|
|
fifo_pager() {
|
|
|
|
cmd="$1"
|
|
|
|
shift
|
|
|
|
|
2023-02-07 03:59:07 +00:00
|
|
|
# We use a FIFO to access $NNN_PAGER PID in jobs control
|
|
|
|
mkfifo "$FIFOPATH" || return
|
2020-06-01 08:42:34 +00:00
|
|
|
|
2023-02-07 03:59:07 +00:00
|
|
|
$NNN_PAGER < "$FIFOPATH" &
|
2021-06-04 22:35:54 +00:00
|
|
|
printf "%s" "$!" > "$PREVIEWPID"
|
2020-05-05 23:08:10 +00:00
|
|
|
|
2020-06-02 08:56:50 +00:00
|
|
|
(
|
2023-02-07 03:59:07 +00:00
|
|
|
exec > "$FIFOPATH"
|
2021-05-01 11:44:19 +00:00
|
|
|
if [ "$cmd" = "pager" ]; then
|
|
|
|
if exists bat; then
|
2022-07-22 08:49:13 +00:00
|
|
|
bat --terminal-width="$cols" --decorations=always --color=always \
|
2023-02-07 03:59:07 +00:00
|
|
|
--paging=never --style="$NNN_BATSTYLE" --theme="$NNN_BATTHEME" "$@" &
|
2021-05-01 11:44:19 +00:00
|
|
|
else
|
2023-02-07 03:59:07 +00:00
|
|
|
$NNN_PAGER "$@" &
|
2021-05-01 11:44:19 +00:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
"$cmd" "$@" &
|
|
|
|
fi
|
2020-06-02 08:56:50 +00:00
|
|
|
)
|
|
|
|
|
2023-02-07 03:59:07 +00:00
|
|
|
rm "$FIFOPATH"
|
2022-07-22 08:49:13 +00:00
|
|
|
}
|
2020-06-01 13:52:16 +00:00
|
|
|
|
2020-06-09 01:09:35 +00:00
|
|
|
# Binary file: show file info inside the pager
|
|
|
|
print_bin_info() {
|
|
|
|
printf -- "-------- \033[1;31mBinary file\033[0m --------\n"
|
|
|
|
if exists mediainfo; then
|
2021-05-16 00:11:05 +00:00
|
|
|
mediainfo "$1"
|
2020-06-09 01:09:35 +00:00
|
|
|
else
|
|
|
|
file -b "$1"
|
|
|
|
fi
|
2022-07-22 08:49:13 +00:00
|
|
|
}
|
2020-06-09 01:09:35 +00:00
|
|
|
|
2021-05-14 17:16:30 +00:00
|
|
|
handle_mime() {
|
|
|
|
case "$2" in
|
2021-05-31 09:43:51 +00:00
|
|
|
image/jpeg) image_preview "$cols" "$lines" "$1" ;;
|
2021-05-30 21:48:10 +00:00
|
|
|
image/gif) generate_preview "$cols" "$lines" "$1" "gif" ;;
|
2022-03-25 16:21:11 +00:00
|
|
|
image/vnd.djvu) generate_preview "$cols" "$lines" "$1" "djvu" ;;
|
2021-05-30 21:48:10 +00:00
|
|
|
image/*) generate_preview "$cols" "$lines" "$1" "image" ;;
|
|
|
|
video/*) generate_preview "$cols" "$lines" "$1" "video" ;;
|
|
|
|
audio/*) generate_preview "$cols" "$lines" "$1" "audio" ;;
|
2021-10-17 05:34:09 +00:00
|
|
|
application/font*|application/*opentype|font/*) generate_preview "$cols" "$lines" "$1" "font" ;;
|
2023-02-25 09:52:33 +00:00
|
|
|
*/*office*|*/*document*|*/*msword|*/*ms-excel) generate_preview "$cols" "$lines" "$1" "office" ;;
|
2021-05-14 17:16:30 +00:00
|
|
|
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
|
2021-05-30 21:48:10 +00:00
|
|
|
epub) generate_preview "$cols" "$lines" "$1" "epub" ;;
|
|
|
|
pdf) generate_preview "$cols" "$lines" "$1" "pdf" ;;
|
2021-05-14 17:16:30 +00:00
|
|
|
gz|bz2) fifo_pager tar -tvf "$1" ;;
|
2021-05-30 21:48:10 +00:00
|
|
|
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 ;;
|
2021-05-14 17:16:30 +00:00
|
|
|
*) if [ "$3" = "bin" ]; then
|
|
|
|
fifo_pager print_bin_info "$1"
|
|
|
|
else
|
|
|
|
fifo_pager pager "$1"
|
|
|
|
fi ;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2021-05-30 21:48:10 +00:00
|
|
|
preview_file() {
|
2020-05-06 17:25:14 +00:00
|
|
|
clear
|
2020-06-10 00:41:57 +00:00
|
|
|
# Trying to use pistol if it's available.
|
2023-02-07 03:59:07 +00:00
|
|
|
if [ "$NNN_PISTOL" -ne 0 ] && exists pistol; then
|
2020-06-10 00:41:57 +00:00
|
|
|
fifo_pager pistol "$1"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2020-06-09 01:09:35 +00:00
|
|
|
# Trying to use scope.sh if it's available.
|
2023-02-07 03:59:07 +00:00
|
|
|
if [ "$NNN_SCOPE" -ne 0 ] && exists scope.sh; then
|
2021-05-16 00:11:05 +00:00
|
|
|
fifo_pager scope.sh "$1" "$cols" "$lines" "$(mktemp -d)" "True"
|
2020-06-09 01:09:35 +00:00
|
|
|
return
|
|
|
|
fi
|
2020-05-06 17:25:14 +00:00
|
|
|
|
2021-05-01 11:44:19 +00:00
|
|
|
# Use QuickLook if it's available.
|
|
|
|
if [ -n "$QUICKLOOK" ]; then
|
2021-05-16 00:11:05 +00:00
|
|
|
stat "$1" && f="$(wslpath -w "$1")" && "$QLPATH" "$f" &
|
2021-05-01 11:44:19 +00:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2021-05-16 00:11:05 +00:00
|
|
|
# Detecting the exact type of the file: the encoding, mime type, and extension in lowercase.
|
2021-03-21 14:03:43 +00:00
|
|
|
encoding="$(file -bL --mime-encoding -- "$1")"
|
|
|
|
mimetype="$(file -bL --mime-type -- "$1")"
|
2020-06-10 07:50:38 +00:00
|
|
|
ext="${1##*.}"
|
2021-05-16 00:11:05 +00:00
|
|
|
[ -n "$ext" ] && ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
|
2022-07-22 08:49:13 +00:00
|
|
|
lines=$(tput lines)
|
|
|
|
cols=$(tput cols)
|
2020-06-10 07:50:38 +00:00
|
|
|
|
2020-06-09 01:09:35 +00:00
|
|
|
# Otherwise, falling back to the defaults.
|
2021-05-01 11:44:19 +00:00
|
|
|
if [ -d "$1" ]; then
|
2020-05-27 23:34:53 +00:00
|
|
|
cd "$1" || return
|
2023-02-07 03:59:07 +00:00
|
|
|
if [ "$NNN_ICONLOOKUP" -ne 0 ] && [ -f "$(dirname "$0")"/.iconlookup ]; then
|
|
|
|
[ "$NNN_SPLIT" = v ] && BSTR="\n"
|
2021-05-30 21:48:10 +00:00
|
|
|
# shellcheck disable=SC2012
|
|
|
|
ls -F --group-directories-first | head -n "$((lines - 3))" | "$(dirname "$0")"/.iconlookup -l "$cols" -B "$BSTR" -b " "
|
|
|
|
elif exists tree; then
|
2021-04-27 19:28:38 +00:00
|
|
|
fifo_pager tree --filelimit "$(find . -maxdepth 1 | wc -l)" -L 3 -C -F --dirsfirst --noreport
|
2020-06-09 01:09:35 +00:00
|
|
|
elif exists exa; then
|
2021-05-16 00:11:05 +00:00
|
|
|
exa -G --group-directories-first --colour=always
|
2020-06-09 01:09:35 +00:00
|
|
|
else
|
2021-05-16 00:11:05 +00:00
|
|
|
fifo_pager ls -F --group-directories-first --color=always
|
2020-06-09 01:09:35 +00:00
|
|
|
fi
|
2023-02-17 00:45:17 +00:00
|
|
|
cd ..
|
2021-04-26 15:21:52 +00:00
|
|
|
elif [ "${encoding#*)}" = "binary" ]; then
|
2021-05-14 17:16:30 +00:00
|
|
|
handle_mime "$1" "$mimetype" "$ext" "bin"
|
2021-05-01 11:44:19 +00:00
|
|
|
else
|
2021-05-14 17:16:30 +00:00
|
|
|
handle_mime "$1" "$mimetype" "$ext"
|
2020-05-27 23:34:53 +00:00
|
|
|
fi
|
2022-07-22 08:49:13 +00:00
|
|
|
}
|
2020-05-06 17:25:14 +00:00
|
|
|
|
2021-05-30 21:48:10 +00:00
|
|
|
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
|
2021-12-20 01:39:10 +00:00
|
|
|
frameprefix="$NNN_PREVIEWDIR/$3/${3##*/}"
|
2021-05-30 21:48:10 +00:00
|
|
|
if [ ! -d "$NNN_PREVIEWDIR/$3" ]; then
|
|
|
|
mkdir -p "$NNN_PREVIEWDIR/$3"
|
2021-12-20 01:39:10 +00:00
|
|
|
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"
|
2021-05-30 21:48:10 +00:00
|
|
|
fi
|
2021-12-20 01:39:10 +00:00
|
|
|
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
|
|
|
|
done &
|
|
|
|
printf "%s" "$!" > "$PREVIEWPID"
|
|
|
|
return
|
2021-05-30 21:48:10 +00:00
|
|
|
else
|
|
|
|
image_preview "$1" "$2" "$3"
|
|
|
|
return
|
|
|
|
fi ;;
|
2021-09-24 10:12:13 +00:00
|
|
|
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 ;;
|
2021-05-30 21:48:10 +00:00
|
|
|
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" ;;
|
2022-07-22 08:49:13 +00:00
|
|
|
djvu) ddjvu -format=ppm -page=1 "$3" "$NNN_PREVIEWDIR/$3.jpg" ;;
|
2022-07-30 16:14:00 +00:00
|
|
|
video) ffmpegthumbnailer -m -s0 -i "$3" -o "$NNN_PREVIEWDIR/$3.jpg" || rm "$NNN_PREVIEWDIR/$3.jpg" ;;
|
2021-05-30 21:48:10 +00:00
|
|
|
esac
|
2022-07-22 08:49:13 +00:00
|
|
|
fi
|
2021-05-30 21:48:10 +00:00
|
|
|
if [ -f "$NNN_PREVIEWDIR/$3.jpg" ]; then
|
|
|
|
image_preview "$1" "$2" "$NNN_PREVIEWDIR/$3.jpg"
|
|
|
|
else
|
|
|
|
fifo_pager print_bin_info "$3"
|
|
|
|
fi
|
2022-07-22 08:49:13 +00:00
|
|
|
} >/dev/null 2>&1
|
2021-05-30 21:48:10 +00:00
|
|
|
|
2021-03-14 06:53:03 +00:00
|
|
|
image_preview() {
|
2021-05-30 21:48:10 +00:00
|
|
|
clear
|
2022-08-07 14:40:12 +00:00
|
|
|
exec >/dev/tty
|
2023-04-19 23:06:43 +00:00
|
|
|
if [ "$NNN_TERMINAL" = "kitty" ] && [ -z "$NNN_PREVIEWIMGPROG" ]; then
|
2022-06-28 10:43:35 +00:00
|
|
|
kitty +kitten icat --silent --scale-up --place "$1"x"$2"@0x0 --transfer-mode=stream --stdin=no "$3" &
|
2023-02-07 03:59:07 +00:00
|
|
|
elif [ "$NNN_TERMINAL" = "wezterm" ] && [ -z "$NNN_PREVIEWIMGPROG" ]; then
|
2023-02-02 10:25:39 +00:00
|
|
|
wezterm imgcat "$3" &
|
2023-02-07 03:59:07 +00:00
|
|
|
elif exists ueberzug && { [ -z "$NNN_PREVIEWIMGPROG" ] || [ "$NNN_PREVIEWIMGPROG" = "ueberzug" ] ;}; then
|
2021-05-16 00:11:05 +00:00
|
|
|
ueberzug_layer "$1" "$2" "$3" && return
|
2023-02-07 03:59:07 +00:00
|
|
|
elif exists catimg && { [ -z "$NNN_PREVIEWIMGPROG" ] || [ "$NNN_PREVIEWIMGPROG" = "catimg" ] ;}; then
|
2021-03-14 06:53:03 +00:00
|
|
|
catimg "$3" &
|
2023-02-07 03:59:07 +00:00
|
|
|
elif exists viu && { [ -z "$NNN_PREVIEWIMGPROG" ] || [ "$NNN_PREVIEWIMGPROG" = "viu" ] ;}; then
|
2021-03-14 06:53:03 +00:00
|
|
|
viu -t "$3" &
|
2023-02-07 03:59:07 +00:00
|
|
|
elif exists chafa && { [ -z "$NNN_PREVIEWIMGPROG" ] || [ "$NNN_PREVIEWIMGPROG" = "chafa" ] ;}; then
|
2023-01-29 22:37:24 +00:00
|
|
|
chafa "$3" &
|
2021-03-14 06:53:03 +00:00
|
|
|
else
|
2021-05-16 00:11:05 +00:00
|
|
|
fifo_pager print_bin_info "$3" && return
|
2020-05-06 04:24:31 +00:00
|
|
|
fi
|
2021-06-04 22:35:54 +00:00
|
|
|
printf "%s" "$!" > "$PREVIEWPID"
|
2022-07-22 08:49:13 +00:00
|
|
|
}
|
2020-05-06 04:24:31 +00:00
|
|
|
|
2021-03-14 06:53:03 +00:00
|
|
|
ueberzug_layer() {
|
2022-07-22 08:49:13 +00:00
|
|
|
[ -f "$POSOFFSET" ] && read -r x y < "$POSOFFSET"
|
2022-06-29 10:33:11 +00:00
|
|
|
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"
|
2021-03-14 06:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ueberzug_remove() {
|
|
|
|
printf '{"action": "remove", "identifier": "nnn_ueberzug"}\n' > "$FIFO_UEBERZUG"
|
|
|
|
}
|
2020-05-06 17:25:14 +00:00
|
|
|
|
2021-05-17 22:19:41 +00:00
|
|
|
winch_handler() {
|
2021-03-14 06:53:03 +00:00
|
|
|
clear
|
2022-07-22 08:49:13 +00:00
|
|
|
pidkill "$PREVIEWPID"
|
2021-05-18 09:20:56 +00:00
|
|
|
if [ -p "$FIFO_UEBERZUG" ]; then
|
2021-06-04 02:10:35 +00:00
|
|
|
pkill -f "tail --follow $FIFO_UEBERZUG"
|
2021-05-18 09:20:56 +00:00
|
|
|
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
|
|
|
|
fi
|
2021-05-01 16:12:13 +00:00
|
|
|
preview_file "$(cat "$CURSEL")"
|
2022-07-22 08:49:13 +00:00
|
|
|
}
|
2021-03-14 06:53:03 +00:00
|
|
|
|
|
|
|
preview_fifo() {
|
|
|
|
while read -r selection; do
|
2021-06-04 02:10:35 +00:00
|
|
|
if [ -n "$selection" ]; then
|
2022-07-22 08:49:13 +00:00
|
|
|
pidkill "$PREVIEWPID"
|
2021-06-04 02:10:35 +00:00
|
|
|
[ -p "$FIFO_UEBERZUG" ] && ueberzug_remove
|
2021-12-14 16:16:56 +00:00
|
|
|
[ "$selection" = "close" ] && break
|
2021-06-04 02:10:35 +00:00
|
|
|
preview_file "$selection"
|
|
|
|
printf "%s" "$selection" > "$CURSEL"
|
|
|
|
fi
|
2021-05-16 00:11:05 +00:00
|
|
|
done < "$NNN_FIFO"
|
2021-11-09 13:33:52 +00:00
|
|
|
sleep 0.1 # make sure potential preview by winch_handler is killed
|
2021-06-04 02:10:35 +00:00
|
|
|
pkill -P "$$"
|
2022-07-22 08:49:13 +00:00
|
|
|
}
|
2021-03-14 06:53:03 +00:00
|
|
|
|
2023-02-07 03:59:07 +00:00
|
|
|
if [ "$PREVIEW_MODE" -eq 1 ] 2>/dev/null; then
|
|
|
|
if [ "$NNN_TERMINAL" != "kitty" ] && exists ueberzug; then
|
2021-03-14 06:53:03 +00:00
|
|
|
mkfifo "$FIFO_UEBERZUG"
|
|
|
|
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
|
|
|
|
fi
|
|
|
|
|
2021-05-01 16:12:13 +00:00
|
|
|
preview_file "$PWD/$1"
|
2021-05-16 00:11:05 +00:00
|
|
|
preview_fifo &
|
2021-06-04 02:10:35 +00:00
|
|
|
printf "%s" "$!" > "$FIFOPID"
|
|
|
|
printf "%s" "$PWD/$1" > "$CURSEL"
|
|
|
|
trap 'winch_handler; wait' WINCH
|
2022-06-29 10:33:11 +00:00
|
|
|
trap 'rm "$PREVIEWPID" "$CURSEL" "$FIFO_UEBERZUG" "$FIFOPID" "$POSOFFSET" 2>/dev/null' INT HUP EXIT
|
2021-05-16 20:56:47 +00:00
|
|
|
wait "$!" 2>/dev/null
|
2022-01-17 13:20:27 +00:00
|
|
|
exit 0
|
2021-04-27 06:26:52 +00:00
|
|
|
else
|
|
|
|
if [ ! -r "$NNN_FIFO" ]; then
|
2023-02-07 03:59:07 +00:00
|
|
|
prompt "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')\nPlease read Usage in '$0'."
|
2021-07-05 13:40:18 +00:00
|
|
|
elif [ "$KITTY_WINDOW_ID" ] && [ -z "$TMUX" ] && [ -z "$KITTY_LISTEN_ON" ]; then
|
2023-02-07 03:59:07 +00:00
|
|
|
prompt "\$KITTY_LISTEN_ON not set!\nPlease read Usage in '$0'."
|
2021-04-27 06:26:52 +00:00
|
|
|
else
|
2022-07-22 08:49:13 +00:00
|
|
|
toggle_preview "$1" &
|
2021-04-27 06:26:52 +00:00
|
|
|
fi
|
2020-05-05 23:08:10 +00:00
|
|
|
fi
|