aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar lvgx <l@vgx.fr>2020-05-17 14:08:29 +0200
committerGravatar GitHub <noreply@github.com>2020-05-17 17:38:29 +0530
commita17ef6699da90cc03789f33bae9d02ae2e5aae5b (patch)
tree74d715ebfb74d39f9211e743247783990572a72a
parent5ea6bc338bdb1335a2ce87e35ffdfdac9ea0b8db (diff)
downloadnnn-a17ef6699da90cc03789f33bae9d02ae2e5aae5b.tar.gz
Add a preview-kitty plugin (#577)
-rw-r--r--plugins/README.md1
-rwxr-xr-xplugins/preview-kitty71
2 files changed, 72 insertions, 0 deletions
diff --git a/plugins/README.md b/plugins/README.md
index d6a3ddb..3d770ea 100644
--- a/plugins/README.md
+++ b/plugins/README.md
@@ -61,6 +61,7 @@ Plugins are installed to `${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins`.
| pdfread | Read a PDF or text file aloud | sh | pdftotext, mpv,<br>pico2wave |
| pdfview | View PDF file in `$PAGER` | sh | pdftotext/<br>mupdf-tools |
| picker | Pick files and list one per line (to pipe) | sh | nnn |
+| preview-kitty | File previewer in a kitty terminal pane | sh | [kitty](https://sw.kovidgoyal.net/kitty/), Optionally:<br>[exa](https://github.com/ogham/exa) [bat](https://github.com/sharkdp/bat) mediainfo |
| preview-tabbed | `tabbed`/xembed based file previewer | bash | _see in-file docs_ |
| preview-tui | Simple TUI file previewer (needs NNN_FIFO) | sh | tmux (>= 3.0)/xterm/<br>\$TERMINAL, file, tree |
| pskill | Fuzzy list by name and kill process or zombie | sh | fzf, ps, sudo/doas |
diff --git a/plugins/preview-kitty b/plugins/preview-kitty
new file mode 100755
index 0000000..4bff54d
--- /dev/null
+++ b/plugins/preview-kitty
@@ -0,0 +1,71 @@
+#!/usr/bin/env sh
+
+# Description: File previewer for kitty term using NNN_FIFO
+#
+# Dependencies:
+# - kitty (https://sw.kovidgoyal.net/kitty/) with allow_remote_control on
+# - file
+# - exa (https://github.com/ogham/exa) (fallback: ls)
+# - bat (https://github.com/sharkdp/bat) (fallback: cat)
+# - mediainfo (fallback: file)
+#
+# Usage:
+# This plugin only works in kitty (https://sw.kovidgoyal.net/kitty/),
+# and with kitty's 'allow_remote_control' option turned on.
+# You need to set a NNN_FIFO path and set a key for the plugin,
+# then start `nnn`:
+#
+# $ NNN_FIFO=/tmp/nnn.fifo nnn
+#
+# Then in `nnn`, launch the `preview-kitty` plugin.
+#
+# 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
+# will be independent.
+#
+# Shell: POSIX compliant
+# Authors: Léo Villeveygoux
+
+preview_file () {
+ clear
+ lines=$(($(tput lines)-1))
+ cols=$(tput cols)
+ mime="$(file -b --mime-type "$1")"
+
+ if [ -d "$1" ]; then
+ # Print directory tree
+ # shellcheck disable=SC2015
+ cd "$1" && \
+ COLUMNS=$cols exa -G --colour=always 2>/dev/null || ls --color=alway
+ elif [ "${mime%%/*}" = "text" ] ; then
+ # Print file head
+ bat --terminal-width="$cols" --paging=never --decorations=always \
+ --color=always "$1" 2>/dev/null || cat
+ elif [ "${mime%%/*}" = "image" ] ; then
+ kitty +kitten icat --silent --transfer-mode=stream --stdin=no "$1"
+ else
+ # Binary file
+ printf -- "-------- \033[1;31mBinary file\033[0m --------\n"
+ mediainfo "$1" 2>/dev/null || file -b "$1"
+ fi | head -n $lines
+}
+
+if [ "$PREVIEW_MODE" ] ; then
+ if [ ! -r "$NNN_FIFO" ] ; then
+ echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
+ read -r
+ exit 1
+ fi
+
+ preview_file "$1"
+
+ exec < "$NNN_FIFO"
+ while read -r selection ; do
+ preview_file "$selection"
+ done
+ exit 0
+fi
+
+kitty @ launch --no-response --title "nnn preview" --keep-focus \
+ --cwd="$PWD" --env "NNN_FIFO=$NNN_FIFO" --env "PREVIEW_MODE=1" \
+ "$0" "$1" > /dev/null