"Fossies" - the Fresh Open Source Software Archive

Member "nnn-4.2/plugins/preview-tui" (21 Jul 2021, 16446 Bytes) of package /linux/misc/nnn-v4.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "preview-tui": v4.1.1_vs_v4.2.

    1 #!/usr/bin/env sh
    2 
    3 # Description: Terminal based file previewer
    4 #
    5 # Note: This plugin needs a "NNN_FIFO" to work. See man.
    6 #
    7 # Dependencies:
    8 #   - Supports 4 independent methods to preview with:
    9 #       - tmux (>=3.0), or
   10 #       - kitty with allow_remote_control and listen_on set in kitty.conf, or
   11 #       - QuickLook on WSL (https://github.com/QL-Win/QuickLook)
   12 #       - $TERMINAL set to a terminal (it's xterm by default).
   13 #   - less or $PAGER
   14 #   - tree or exa or ls
   15 #   - mediainfo or file
   16 #   - mktemp
   17 #   - unzip
   18 #   - tar
   19 #   - man
   20 #   - optional: bsdtar or atool for additional archive preview
   21 #   - optional: bat for code syntax highlighting
   22 #   - optional: ueberzug, kitty terminal, viu or catimg for images
   23 #   - optional: convert(ImageMagick) for playing gif preview
   24 #   - optional: ffmpegthumbnailer for video thumbnails (https://github.com/dirkvdb/ffmpegthumbnailer)
   25 #   - optional: ffmpeg for audio thumbnails
   26 #   - optional: libreoffce for opendocument/officedocument preview
   27 #   - optional: pdftoppm(poppler) for pdf thumbnails
   28 #   - optional: gnome-epub-thumbnailer for epub thumbnails (https://gitlab.gnome.org/GNOME/gnome-epub-thumbnailer)
   29 #   - optional: fontpreview for font preview (https://github.com/sdushantha/fontpreview)
   30 #   - optional: glow or lowdown for markdown
   31 #   - optional: w3m or lynx or elinks for html
   32 #   - optional: set/export ICONLOOKUP as 1 to enable file icons in front of directory previews with .iconlookup
   33 #       Icons and colors are configureable in .iconlookup
   34 #   - optional: scope.sh file viewer from ranger.
   35 #       1. drop scope.sh executable in $PATH
   36 #       2. set/export $USE_SCOPE as 1
   37 #   - optional: pistol file viewer (https://github.com/doronbehar/pistol).
   38 #       1. install pistol
   39 #       2. set/export $USE_PISTOL as 1
   40 #
   41 # Usage:
   42 #   You need to set a NNN_FIFO path and a key for the plugin with NNN_PLUG,
   43 #   then start `nnn`:
   44 #
   45 #     $ nnn -a
   46 #
   47 #   or
   48 #
   49 #     $ NNN_FIFO=/tmp/nnn.fifo nnn
   50 #
   51 #   Then launch the `preview-tui` plugin in `nnn`.
   52 #
   53 #   If you provide the same NNN_FIFO to all nnn instances, there will be a
   54 #   single common preview window. If you provide different FIFO path (e.g.
   55 #   with -a), they will be independent.
   56 #
   57 #   The previews will be shown in a tmux split. If that isn't possible, it
   58 #   will try to use a kitty terminal split. And as a final fallback, a
   59 #   different terminal window will be used ($TERMINAL).
   60 #
   61 #   Tmux and kitty users can configure $SPLIT to either "h" or "v" to set a
   62 #   'h'orizontal split or a 'v'ertical split (as in, the line that splits the
   63 #   windows will be horizontal or vertical).
   64 #
   65 #   Kitty users need `allow_remote_control` set to `yes`, and `listen_on` set
   66 #   to e.g. "$TMPDIR/kitty". To customize the window split, `enabled_layouts`
   67 #   has to be set to `all` or `splits` (the former is the default value).
   68 #   This terminal is also able to show images without extra dependencies.
   69 #
   70 # Shell: POSIX compliant
   71 # Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste, Mario Ortiz Manero, Luuk van Baal
   72 
   73 #SPLIT="$SPLIT"  # you can set a permanent split here
   74 #TERMINAL="$TERMINAL"  # same goes for the terminal
   75 USE_SCOPE="${USE_SCOPE:-0}"
   76 USE_PISTOL="${USE_PISTOL:-0}"
   77 ICONLOOKUP="${ICONLOOKUP:-0}"
   78 PAGER="${PAGER:-less -P?n -R}"
   79 TMPDIR="${TMPDIR:-/tmp}"
   80 BAT_STYLE="${BAT_STYLE:-numbers}"
   81 # Consider setting NNN_PREVIEWDIR to $XDG_CACHE_HOME/nnn/previews if you want to keep previews on disk between reboots
   82 NNN_PREVIEWDIR="${NNN_PREVIEWDIR:-$TMPDIR/nnn/previews}"
   83 NNN_PREVIEWWIDTH="${NNN_PREVIEWWIDTH:-1920}"
   84 NNN_PREVIEWHEIGHT="${NNN_PREVIEWHEIGHT:-1080}"
   85 NNN_PARENT="${NNN_FIFO#*.}"
   86 FIFOPID="$TMPDIR/nnn-preview-tui-fifopid.$NNN_PARENT"
   87 PREVIEWPID="$TMPDIR/nnn-preview-tui-pagerpid.$NNN_PARENT"
   88 CURSEL="$TMPDIR/nnn-preview-tui-selection.$NNN_PARENT"
   89 FIFO_UEBERZUG="$TMPDIR/nnn-preview-tui-ueberzug-fifo.$NNN_PARENT"
   90 
   91 start_preview() {
   92     [ "$PAGER" = "most" ] && PAGER="less -R"
   93 
   94     if [ -e "${TMUX%%,*}" ] && tmux -V | grep -q '[ -][3456789]\.'; then
   95         TERMINAL=tmux
   96     elif [ -n "$KITTY_LISTEN_ON" ]; then
   97         TERMINAL=kitty
   98     else
   99         TERMINAL="${TERMINAL:-xterm}"
  100     fi
  101 
  102     if [ -z "$SPLIT" ] && [ $(($(tput lines) * 2)) -gt "$(tput cols)" ]; then
  103         SPLIT='h'
  104     elif [ "$SPLIT" != 'h' ]; then
  105         SPLIT='v'
  106     fi
  107 
  108     case "$TERMINAL" in
  109         tmux) # tmux splits are inverted
  110             if [ "$SPLIT" = "v" ]; then DSPLIT="h"; else DSPLIT="v"; fi
  111             tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1"  \
  112                 -e "CURSEL=$CURSEL" -e "TMPDIR=$TMPDIR" -e "FIFOPID=$FIFOPID" \
  113                 -e "BAT_STYLE=$BAT_STYLE" -e "PREVIEWPID=$PREVIEWPID" -e "PAGER=$PAGER" \
  114                 -e "ICONLOOKUP=$ICONLOOKUP" -e "NNN_PREVIEWWIDTH=$NNN_PREVIEWWIDTH" \
  115                 -e "USE_SCOPE=$USE_SCOPE" -e "SPLIT=$SPLIT" -e "USE_PISTOL=$USE_PISTOL" \
  116                 -e "NNN_PREVIEWDIR=$NNN_PREVIEWDIR" -e "NNN_PREVIEWHEIGHT=$NNN_PREVIEWHEIGHT" \
  117                 -e "FIFO_UEBERZUG=$FIFO_UEBERZUG" -e "QLPATH=$2" -d"$DSPLIT" "$0" "$1" ;;
  118         kitty) # Setting the layout for the new window. It will be restored after the script ends.
  119             kitty @ goto-layout splits
  120             # Trying to use kitty's integrated window management as the split window. All
  121             # environmental variables that will be used in the new window must be explicitly passed.
  122             kitty @ launch --no-response --title "nnn preview" --keep-focus \
  123                 --cwd "$PWD" --env "PATH=$PATH" --env "NNN_FIFO=$NNN_FIFO" \
  124                 --env "PREVIEW_MODE=1" --env "PAGER=$PAGER" --env "TMPDIR=$TMPDIR" \
  125                 --env "USE_SCOPE=$USE_SCOPE" --env "SPLIT=$SPLIT" --env "TERMINAL=$TERMINAL"\
  126                 --env "PREVIEWPID=$PREVIEWPID" --env "FIFO_UEBERZUG=$FIFO_UEBERZUG" \
  127                 --env "ICONLOOKUP=$ICONLOOKUP" --env "NNN_PREVIEWHEIGHT=$NNN_PREVIEWHEIGHT" \
  128                 --env "NNN_PREVIEWWIDTH=$NNN_PREVIEWWIDTH" --env "NNN_PREVIEWDIR=$NNN_PREVIEWDIR" \
  129                 --env "USE_PISTOL=$USE_PISTOL" --env "BAT_STYLE=$BAT_STYLE" --env "FIFOPID=$FIFOPID" \
  130                 --env "CURSEL=$CURSEL" --location "${SPLIT}split" "$0" "$1" ;;
  131         *)  if [ -n "$2" ]; then
  132                 QUICKLOOK=1 QLPATH="$2" PREVIEW_MODE=1 "$0" "$1" &
  133             else
  134                 PREVIEWPID="$PREVIEWPID" CURSEL="$CURSEL" PREVIEW_MODE=1 \
  135                      FIFOPID="$FIFOPID" FIFO_UEBERZUG="$FIFO_UEBERZUG" $TERMINAL -e "$0" "$1" &
  136             fi ;;
  137     esac
  138 } >/dev/null 2>&1
  139 
  140 toggle_preview() {
  141     if exists QuickLook.exe; then
  142         QLPATH="QuickLook.exe"
  143     elif exists Bridge.exe; then
  144         QLPATH="Bridge.exe"
  145     fi
  146     if kill "$(cat "$FIFOPID")"; then
  147         kill "$(cat "$PREVIEWPID")"
  148         pkill -f "tail --follow $FIFO_UEBERZUG"
  149         if [ -n "$QLPATH" ] && stat "$1"; then
  150             f="$(wslpath -w "$1")" && "$QLPATH" "$f" &
  151         fi
  152     else
  153         start_preview "$1" "$QLPATH"
  154     fi
  155 } >/dev/null 2>&1
  156 
  157 exists() {
  158     type "$1" >/dev/null
  159 }
  160 
  161 fifo_pager() {
  162     cmd="$1"
  163     shift
  164 
  165     # We use a FIFO to access $PAGER PID in jobs control
  166     tmpfifopath="$TMPDIR/nnn-preview-tui-fifo.$$"
  167     mkfifo "$tmpfifopath" || return
  168 
  169     $PAGER < "$tmpfifopath" &
  170     printf "%s" "$!" > "$PREVIEWPID"
  171 
  172     (
  173         exec > "$tmpfifopath"
  174         if [ "$cmd" = "pager" ]; then
  175             if exists bat; then
  176                 bat --terminal-width="$(tput cols)" --decorations=always --color=always \
  177                     --paging=never --style="$BAT_STYLE" "$@" &
  178             else
  179                 $PAGER "$@" &
  180             fi
  181         else
  182             "$cmd" "$@" &
  183         fi
  184     )
  185 
  186     rm "$tmpfifopath"
  187 } 2>/dev/null
  188 
  189 # Binary file: show file info inside the pager
  190 print_bin_info() {
  191     printf -- "-------- \033[1;31mBinary file\033[0m --------\n"
  192     if exists mediainfo; then
  193         mediainfo "$1"
  194     else
  195         file -b "$1"
  196     fi
  197 } 2>/dev/null
  198 
  199 handle_mime() {
  200     case "$2" in
  201         image/jpeg) image_preview "$cols" "$lines" "$1" ;;
  202         image/gif) generate_preview "$cols" "$lines" "$1" "gif" ;;
  203         image/*) generate_preview "$cols" "$lines" "$1" "image" ;;
  204         video/*) generate_preview "$cols" "$lines" "$1" "video" ;;
  205         audio/*) generate_preview "$cols" "$lines" "$1" "audio" ;;
  206         application/font*|application/*opentype) generate_preview "$cols" "$lines" "$1" "font" ;;
  207         */*office*|*/*document*) generate_preview "$cols" "$lines" "$1" "office" ;;
  208         application/zip) fifo_pager unzip -l "$1" ;;
  209         text/troff)
  210             if exists man; then
  211                 fifo_pager man -Pcat -l "$1"
  212             else
  213                 fifo_pager pager "$1"
  214             fi ;;
  215         *) handle_ext "$1" "$3" "$4" ;;
  216     esac
  217 }
  218 
  219 handle_ext() {
  220     case "$2" in
  221         epub) generate_preview "$cols" "$lines" "$1" "epub" ;;
  222         pdf) generate_preview "$cols" "$lines" "$1" "pdf" ;;
  223         gz|bz2) fifo_pager tar -tvf "$1" ;;
  224         md) if exists glow; then
  225                 fifo_pager glow -s dark "$1"
  226             elif exists lowdown; then
  227                 fifo_pager lowdown -Tterm "$1"
  228             else
  229                 fifo_pager pager "$1"
  230             fi ;;
  231         htm|html|xhtml)
  232             if exists w3m; then
  233                 fifo_pager w3m "$1"
  234             elif exists lynx; then
  235                 fifo_pager lynx "$1"
  236             elif exists elinks; then
  237                 fifo_pager elinks "$1"
  238             else
  239                 fifo_pager pager "$1"
  240             fi ;;
  241         7z|a|ace|alz|arc|arj|bz|cab|cpio|deb|jar|lha|lz|lzh|lzma|lzo\
  242         |rar|rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z)
  243             if exists atool; then
  244                 fifo_pager atool -l "$1"
  245             elif exists bsdtar; then
  246                 fifo_pager bsdtar -tvf "$1"
  247             fi ;;
  248         *) if [ "$3" = "bin" ]; then
  249                fifo_pager print_bin_info "$1"
  250            else
  251                fifo_pager pager "$1"
  252            fi ;;
  253     esac
  254 }
  255 
  256 preview_file() {
  257     clear
  258     # Trying to use pistol if it's available.
  259     if [ "$USE_PISTOL" -ne 0 ] && exists pistol; then
  260         fifo_pager pistol "$1"
  261         return
  262     fi
  263 
  264     # Trying to use scope.sh if it's available.
  265     if [ "$USE_SCOPE" -ne 0 ] && exists scope.sh; then
  266         fifo_pager scope.sh "$1" "$cols" "$lines" "$(mktemp -d)" "True"
  267         return
  268     fi
  269 
  270     # Use QuickLook if it's available.
  271     if [ -n "$QUICKLOOK" ]; then
  272         stat "$1" && f="$(wslpath -w "$1")" && "$QLPATH" "$f" &
  273         return
  274     fi
  275 
  276     # Detecting the exact type of the file: the encoding, mime type, and extension in lowercase.
  277     encoding="$(file -bL --mime-encoding -- "$1")"
  278     mimetype="$(file -bL --mime-type -- "$1")"
  279     ext="${1##*.}"
  280     [ -n "$ext" ] && ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
  281     lines=$(tput lines)
  282     cols=$(tput cols)
  283 
  284     # Otherwise, falling back to the defaults.
  285     if [ -d "$1" ]; then
  286         cd "$1" || return
  287         if [ "$ICONLOOKUP" -ne 0 ] && [ -f "$(dirname "$0")"/.iconlookup ]; then
  288             [ "$SPLIT" = v ] && BSTR="\n"
  289             # shellcheck disable=SC2012
  290             ls -F --group-directories-first | head -n "$((lines - 3))" | "$(dirname "$0")"/.iconlookup -l "$cols" -B "$BSTR" -b " "
  291         elif exists tree; then
  292             fifo_pager tree --filelimit "$(find . -maxdepth 1 | wc -l)" -L 3 -C -F --dirsfirst --noreport
  293         elif exists exa; then
  294             exa -G --group-directories-first --colour=always
  295         else
  296             fifo_pager ls -F --group-directories-first --color=always
  297         fi
  298     elif [ "${encoding#*)}" = "binary" ]; then
  299         handle_mime "$1" "$mimetype" "$ext" "bin"
  300     else
  301         handle_mime "$1" "$mimetype" "$ext"
  302     fi
  303 } 2>/dev/null
  304 
  305 generate_preview() {
  306   if [ -n "$QLPATH" ] && stat "$3"; then
  307         f="$(wslpath -w "$3")" && "$QLPATH" "$f" &
  308   elif [ ! -f "$NNN_PREVIEWDIR/$3.jpg" ] || [ -n "$(find -L "$3" -newer "$NNN_PREVIEWDIR/$3.jpg")" ]; then
  309         mkdir -p "$NNN_PREVIEWDIR/${3%/*}"
  310         case $4 in
  311             audio) ffmpeg -i "$3" -filter_complex "scale=iw*min(1\,min($NNN_PREVIEWWIDTH/iw\,ih)):-1" "$NNN_PREVIEWDIR/$3.jpg" -y ;;
  312             epub) gnome-epub-thumbnailer "$3" "$NNN_PREVIEWDIR/$3.jpg" ;;
  313             font) fontpreview -i "$3" -o "$NNN_PREVIEWDIR/$3.jpg" ;;
  314             gif) if [ -p "$FIFO_UEBERZUG" ] && exists convert; then
  315                     if [ ! -d "$NNN_PREVIEWDIR/$3" ]; then
  316                         mkdir -p "$NNN_PREVIEWDIR/$3"
  317                         convert -coalesce -resize "$NNN_PREVIEWWIDTH"x"$NNN_PREVIEWHEIGHT"\> "$3" "$NNN_PREVIEWDIR/$3/${3##*/}.jpg"
  318                     fi
  319                         while true; do
  320                             for frame in $(find "$NNN_PREVIEWDIR/$3"/*.jpg | sort -V); do
  321                                 image_preview "$1" "$2" "$frame"
  322                                 sleep 0.1
  323                             done
  324                         done &
  325                         printf "%s" "$!" > "$PREVIEWPID"
  326                         return
  327                  else
  328                     exec >/dev/tty
  329                     image_preview "$1" "$2" "$3"
  330                     return
  331                  fi ;;
  332             image) convert "$3" -flatten -resize "$NNN_PREVIEWWIDTH"x"$NNN_PREVIEWHEIGHT"\> "$NNN_PREVIEWDIR/$3.jpg" ;;
  333             office) libreoffice --convert-to jpg "$3" --outdir "$NNN_PREVIEWDIR/${3%/*}"
  334                     filename="$(printf "%s" "${3##*/}" | cut -d. -f1)"
  335                     mv "$NNN_PREVIEWDIR/${3%/*}/$filename.jpg" "$NNN_PREVIEWDIR/$3.jpg" ;;
  336             pdf) pdftoppm -jpeg -f 1 -singlefile "$3" "$NNN_PREVIEWDIR/$3" ;;
  337             video) ffmpegthumbnailer -s0 -i "$3" -o "$NNN_PREVIEWDIR/$3.jpg" || rm "$NNN_PREVIEWDIR/$3.jpg" ;;
  338         esac
  339     fi >/dev/null
  340     if [ -f "$NNN_PREVIEWDIR/$3.jpg" ]; then
  341         image_preview "$1" "$2" "$NNN_PREVIEWDIR/$3.jpg"
  342     else
  343         fifo_pager print_bin_info "$3"
  344     fi
  345 } 2>/dev/null
  346 
  347 image_preview() {
  348     clear
  349     if [ "$TERMINAL" = "kitty" ]; then
  350         # Kitty terminal users can use the native image preview method
  351         kitty +kitten icat --silent --place "$1"x"$2"@0x0 --transfer-mode=stream --stdin=no "$3" &
  352     elif exists ueberzug; then
  353         ueberzug_layer "$1" "$2" "$3" && return
  354     elif exists catimg; then
  355         catimg "$3" &
  356     elif exists viu; then
  357         viu -t "$3" &
  358     else
  359         fifo_pager print_bin_info "$3" && return
  360     fi
  361     printf "%s" "$!" > "$PREVIEWPID"
  362 } 2>/dev/null
  363 
  364 ueberzug_layer() {
  365     printf '{"action": "add", "identifier": "nnn_ueberzug", "x": 0, "y": 0, "width": "%d", "height": "%d", "scaler": "fit_contain", "path": "%s"}\n' "$1" "$2" "$3" > "$FIFO_UEBERZUG"
  366 }
  367 
  368 ueberzug_remove() {
  369     printf '{"action": "remove", "identifier": "nnn_ueberzug"}\n' > "$FIFO_UEBERZUG"
  370 }
  371 
  372 winch_handler() {
  373     clear
  374     kill "$(cat "$PREVIEWPID")"
  375     if [ -p "$FIFO_UEBERZUG" ]; then
  376         pkill -f "tail --follow $FIFO_UEBERZUG"
  377         tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
  378     fi
  379     preview_file "$(cat "$CURSEL")"
  380     if ! kill -0 "$NNN_PARENT" || ! pgrep nnn >/dev/null; then
  381         pkill -P "$$"
  382     fi
  383 } 2>/dev/null
  384 
  385 preview_fifo() {
  386     while read -r selection; do
  387         if [ -n "$selection" ]; then
  388             kill "$(cat "$PREVIEWPID")"
  389             [ -p "$FIFO_UEBERZUG" ] && ueberzug_remove
  390             preview_file "$selection"
  391             printf "%s" "$selection" > "$CURSEL"
  392         fi
  393     done < "$NNN_FIFO"
  394     pkill -P "$$"
  395 } 2>/dev/null
  396 
  397 if [ "$PREVIEW_MODE" ]; then
  398     if [ "$TERMINAL" != "kitty" ] && exists ueberzug; then
  399         mkfifo "$FIFO_UEBERZUG"
  400         tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser json &
  401     fi
  402 
  403     preview_file "$PWD/$1"
  404     preview_fifo &
  405     printf "%s" "$!" > "$FIFOPID"
  406     printf "%s" "$PWD/$1" > "$CURSEL"
  407     trap 'winch_handler; wait' WINCH
  408     trap 'rm "$PREVIEWPID" "$CURSEL" "$FIFO_UEBERZUG" "$FIFOPID" 2>/dev/null' INT HUP EXIT
  409     wait "$!" 2>/dev/null
  410 else
  411     if [ ! -r "$NNN_FIFO" ]; then
  412         clear
  413         printf "No FIFO available! (\$NNN_FIFO='%s')\nPlease read Usage in preview-tui." "$NNN_FIFO"
  414         cfg=$(stty -g); stty raw -echo; head -c 1; stty "$cfg"
  415     elif [ "$KITTY_WINDOW_ID" ] && [ -z "$TMUX" ] && [ -z "$KITTY_LISTEN_ON" ]; then
  416         clear
  417         printf "\$KITTY_LISTEN_ON not set!\nPlease read Usage in preview-tui."
  418         cfg=$(stty -g); stty raw -echo; head -c 1; stty "$cfg"
  419     else
  420         toggle_preview "$1" &
  421     fi
  422 fi