diff options
author | lvgx <l@vgx.fr> | 2020-05-06 01:08:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-06 04:38:10 +0530 |
commit | ba51b369313dd3fc5686676c24e3172aa8f6c270 (patch) | |
tree | adabfa7e26aa2aabe3a3fdd65258de3aabfd0378 | |
parent | c360f5c90894c6d3c4065d678d52c08445466016 (diff) | |
download | nnn-ba51b369313dd3fc5686676c24e3172aa8f6c270.tar.gz |
Add a tmux/xterm based text previewer plugin (#557)
Uses `NNN_FIFO`, minimal dependencies.
Co-authored-by: Todd Yamakawa <todd.yamakawa@arm.com>
Co-authored-by: Todd Yamakawa <todd.yamakawa@arm.com>
-rw-r--r-- | plugins/README.md | 1 | ||||
-rwxr-xr-x | plugins/preview-tui | 68 |
2 files changed, 69 insertions, 0 deletions
diff --git a/plugins/README.md b/plugins/README.md index 4ae5bd2..c8a0590 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -61,6 +61,7 @@ Plugins are installed to `${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins`. | pdfview | View PDF file in `$PAGER` | sh | pdftotext/<br>mupdf-tools | | picker | Pick files and list one per line (to pipe) | sh | nnn | | preview-tabbed | `tabbed`/xembed based file previewer | bash | _see in-file docs_ | +| preview-tui | Simple TUI file previewer (needs NNN_FIFO) | sh | tmux/xterm/\$TERMINAL <br> file, tree | | pskill | Fuzzy list by name and kill process or zombie | sh | fzf/fzy, ps,<br>sudo/doas | | renamer | Batch rename selection or files in dir | sh | [qmv](https://www.nongnu.org/renameutils/)/[vidir](https://joeyh.name/code/moreutils/) | | ringtone | Create a variable bitrate mp3 ringtone from file | sh | date, ffmpeg | diff --git a/plugins/preview-tui b/plugins/preview-tui new file mode 100755 index 0000000..3efa4ac --- /dev/null +++ b/plugins/preview-tui @@ -0,0 +1,68 @@ +#!/usr/bin/env sh + +# Description: Text based file previewer +# +# Note: This plugin needs a "NNN_FIFO" to work. +# +# Dependencies: tmux or xterm (or set $TERMINAL), file, tree +# +# How to use: +# 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-tui` 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. +# +# Authors: Todd Yamakawa and Léo Villeveygoux + +TERMINAL="${TERMINAL:-xterm}" +NUKE="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins/nuke" + +if [ ! -r "$NNN_FIFO" ] ; then + echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2 + exit 1 +fi +if [ ! -x "$NUKE" ] ; then + echo "Nuke not found!" >&2 + exit 1 +fi + +if [ "$PREVIEW_MODE" ] ; then + exec < "$NNN_FIFO" + while read -r selection ; do + clear + lines=$(($(tput lines)-1)) + cols=$(tput cols) + mime="$(file -b --mime-type "$selection")" + + if [ "$mime" = "inode/directory" ] ; then + # Print directory tree + cd "$selection" && tree | head -n $lines | cut -c 1-"$cols" + elif [ "${mime%%/*}" = "text" ] ; then + # Print file head + head -n $lines "$selection" | cut -c 1-"$cols" + else + # Binary file + echo "-------- Binary file --------" + file -b "$selection" + fi + done + + if [ -e "${TMUX%%,*}" ] ; then + tmux kill-pane + fi + + exit 0 +fi + +if [ -e "${TMUX%%,*}" ] ; then + tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1" -dh "$0" +else + PREVIEW_MODE=1 GUI=0 $TERMINAL -e "$0" & +fi + |