diff --git a/README.md b/README.md index 5486d2b..f0e0e5b 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,29 @@ -# opener +# fzf--previewer + A simple bash script designed for use with `fzf` preview but maybe useful in other ways? +`fzf --preview fzf--previewer` + Instead of showing the usual directory error or binary file warning with the fancy `bat` preview in `fzf` this script conditionally checks the selected file and previews a tree, a highlighted text, or a hex viewer. ## Prerequisites -This uses the programs: `bat`, for text file viewing; `tree`, for graphical dir listings; `hexyl`, for binary file viewing. +This uses the programs: + * `bat`, for text file viewing + * `tree`, for graphical dir listings + * `hexyl`, for binary file viewing + * `atool`, for view archive file content + * `pdftotext` (poppler-utils), for PDF preview + * `odt2txt`, simple converter from OpenDocument Text + * `catdoc` and `docx2txt`, for M\$-Office preview + * [kitty](https://sw.kovidgoyal.net/kitty/) `icat` + or [`ascii-image-converter`](https://github.com/TheZoraiz/ascii-image-converter) + for images -On Ubuntu based systems you can install them like so: +On Debian/Devuan based systems you can install them like so: ```bash -sudo apt update; sudo apt install -y bat tree hexyl +sudo apt update; sudo apt install -y bat tree hexyl atool poppler-utils odt2txt catdoc kitty ``` Similarly, I use this with `fzf` (fuzzy finder), which you will need to follow the usage example. @@ -52,6 +65,11 @@ alias fz="find . -maxdepth 1 | sed 's/^\.\///g' | fzf --preview 'opener {}'" # Usage -With the alias, running `fz` will give a searchable directory listing with a preview for every filetype. You should be able to use the up and down arrow keys to navigate or just start typing and the fuzzy search will narrow down what you want to find. The preview is scrollable on some systems. Hitting `[ENTER]` will output the filename of the current selection. Hitting `[ESCAPE]` will exit the `curses` session. +With the alias, running `fz` will give a searchable directory listing with a +preview for every filetype. You should be able to use the up and down arrow +keys to navigate or just start typing and the fuzzy search will narrow down +what you want to find. The preview is scrollable on some systems. Hitting +`[ENTER]` will output the filename of the current selection. Hitting +`[ESCAPE]` will exit the `curses` session. NOTE: I've only tested this on Linux, but it might work elsewhere. diff --git a/fzf--previewer.zsh b/fzf--previewer.zsh new file mode 100755 index 0000000..85554f3 --- /dev/null +++ b/fzf--previewer.zsh @@ -0,0 +1,94 @@ +#!/usr/bin/env zsh + +FILE_PATH=$1 + +bat=`/usr/bin/which bat || echo batcat` + +kitty +kitten icat --clear --z-index=-1 --transfer-mode=stream 2>/dev/null +if [ -d "$1" ]; then + tree --du -C -L 2 "$FILE_PATH" + +elif which $1 &>/dev/null; then + type -a $1 + if man -w $1 &>/dev/null; then + print + man $1 | col -bx | head -n 30 + fi +elif ! [ -f "$1" ]; then + echo $1 | read -A tokens + for token in $tokens; do + if [ -f $token ]; then + $bat --style=numbers --color=always --line-range :222 $token + fi + done +else + FILE_MIME=$(file --mime "$FILE_PATH") + filename=$(basename -- "$FILE_PATH") + FILE_EXT="${filename##*.}" + + case $FILE_EXT in + # This is copyed from my ~/.config/ranger/scope.sh + # Archive + a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\ + rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip|rar) + atool --list -- "${FILE_PATH}" && exit 0 + exit 1;; + 7z) + # Avoid password prompt by providing empty password + 7z l -p -- "${FILE_PATH}" && exit 0 + exit 1;; + + # PDF + pdf) + # Preview as text conversion + pdftotext -l 10 -nopgbrk -q -- "${FILE_PATH}" - | fmt -w ${PV_WIDTH} && exit 0 + mutool draw -F txt -i -- "${FILE_PATH}" 1-10 | fmt -w ${PV_WIDTH} && exit 0 + exiftool "${FILE_PATH}" && exit 0 + exit 1;; + + + # OpenDocument + odt|ods|odp|sxw) + # Preview as text conversion + odt2txt "${FILE_PATH}" && exit 0 + exit 1;; + + doc) + # Preview as text conversion + catdoc "${FILE_PATH}" && exit 0 + exit 1;; + docx) + # Preview as text conversion + docx2txt "${FILE_PATH}" - && exit 0 + exit 1;; + + # HTML + htm|html|xhtml) + # Preview as text conversion + w3m -dump "${FILE_PATH}" && exit 0 + lynx -dump -- "${FILE_PATH}" && exit 0 + elinks -dump "${FILE_PATH}" && exit 0 + ;; # Continue with next handler on failure + + [jJ][pP][gG]|[jJ][pP][eE][gG]|[gG][iI][fF]|[bB][mM][pP]|webp|[pP][nN][gG]|\ + [tT][iI][fF]|[tT][iI][fF][fF]) + zmodload zsh/mathfunc + width=$(( $COLUMNS - 10 )) + height=$(( int($LINES * 0.77) - 5 )) + identify ${FILE_PATH} + kitty +kitten icat --z-index=-1 --engine=builtin --clear --align=center --place=${width}x${height}@2x2 --transfer-mode=stream <"${FILE_PATH}" && exit 0 + ascii-image-converter --dimensions ${with},${height} --braille --color "${FILE_PATH}" && exit 0 + # fim sdl=600:600 ${FILE_PATH} + exit 1;; + + *) + if [[ $FILE_MIME == *binary ]] ; then + echo "$FILE_MIME" + hexyl -n 4kB "$FILE_PATH" && exit 0 + else + $bat --style=numbers --color=always --line-range :222 $1 && exit 0 + fi + exit 1;; + esac + +fi diff --git a/opener.bash b/opener.bash deleted file mode 100755 index 8cd4054..0000000 --- a/opener.bash +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env bash - -if [ -d "$1" ]; then - tree -a -C -L 3 "$1" -else - BAT_TEST=$(file --mime "$1") - if [[ $BAT_TEST == *binary ]] ; then - hexyl -n 4kB "$1" - else - bat --color=always --style=numbers --line-range=:500 "$1" - fi -fi