From 57ed17ec04f6421d0a341d00b2688afb9bf6a001 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marrek=20No=C5=BEka?= Date: Tue, 22 Feb 2022 14:50:22 +0100 Subject: [PATCH 1/4] upgrade for specific file extentions --- README.md | 27 ++++++++++++++---- fzf--previewer.zsh | 71 ++++++++++++++++++++++++++++++++++++++++++++++ opener.bash | 12 -------- 3 files changed, 93 insertions(+), 17 deletions(-) create mode 100755 fzf--previewer.zsh delete mode 100755 opener.bash diff --git a/README.md b/README.md index 5486d2b..b9b3338 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,28 @@ -# 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 + * [`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 ``` Similarly, I use this with `fzf` (fuzzy finder), which you will need to follow the usage example. @@ -52,6 +64,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..dcd9743 --- /dev/null +++ b/fzf--previewer.zsh @@ -0,0 +1,71 @@ +#!/usr/bin/env bash + +FILE_PATH=$1 + +if [ -d "$1" ]; then + tree --du -C -L 2 "$FILE_PATH" +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]) + ascii-image-converter --width $[$COLUMNS - 20] --braille --color "${FILE_PATH}" && exit 0 + exit 1;; + + *) + if [[ $FILE_MIME == *binary ]] ; then + echo "$FILE_MIME" + hexyl -n 4kB "$FILE_PATH" && exit 0 + else + batcat --style=numbers --color=always --line-range :222 $1 + 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 From 475e6d04fd5da62f061bba05f6a8cab3bd765dce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marrek=20No=C5=BEka?= Date: Tue, 22 Feb 2022 15:22:16 +0100 Subject: [PATCH 2/4] alias batcat, bat --- fzf--previewer.zsh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fzf--previewer.zsh b/fzf--previewer.zsh index dcd9743..58de835 100755 --- a/fzf--previewer.zsh +++ b/fzf--previewer.zsh @@ -63,7 +63,8 @@ else echo "$FILE_MIME" hexyl -n 4kB "$FILE_PATH" && exit 0 else - batcat --style=numbers --color=always --line-range :222 $1 + cat --style=numbers --color=always --line-range :222 $1 && exit 0 + batcat --style=numbers --color=always --line-range :222 $1 && exit 0 fi exit 1;; esac From b27823ee231d46ef298d65b22d21862ca8dac724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marrek=20No=C5=BEka?= Date: Wed, 23 Feb 2022 12:33:33 +0100 Subject: [PATCH 3/4] preview files in history (^R) --- fzf--previewer.zsh | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/fzf--previewer.zsh b/fzf--previewer.zsh index 58de835..cefe8f8 100755 --- a/fzf--previewer.zsh +++ b/fzf--previewer.zsh @@ -1,9 +1,18 @@ -#!/usr/bin/env bash +#!/usr/bin/env zsh FILE_PATH=$1 +bat=`/usr/bin/which bat || echo batcat` + if [ -d "$1" ]; then tree --du -C -L 2 "$FILE_PATH" +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") @@ -63,8 +72,7 @@ else echo "$FILE_MIME" hexyl -n 4kB "$FILE_PATH" && exit 0 else - cat --style=numbers --color=always --line-range :222 $1 && exit 0 - batcat --style=numbers --color=always --line-range :222 $1 && exit 0 + $bat --style=numbers --color=always --line-range :222 $1 && exit 0 fi exit 1;; esac From 91424fbf348f700592e89066ef34a7cab81816d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marrek=20No=C5=BEka?= Date: Thu, 25 Apr 2024 16:19:26 +0200 Subject: [PATCH 4/4] add manpages and kitty images previews --- README.md | 5 +++-- fzf--previewer.zsh | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b9b3338..f0e0e5b 100644 --- a/README.md +++ b/README.md @@ -16,13 +16,14 @@ This uses the programs: * `pdftotext` (poppler-utils), for PDF preview * `odt2txt`, simple converter from OpenDocument Text * `catdoc` and `docx2txt`, for M\$-Office preview - * [`ascii-image-converter`](https://github.com/TheZoraiz/ascii-image-converter) + * [kitty](https://sw.kovidgoyal.net/kitty/) `icat` + or [`ascii-image-converter`](https://github.com/TheZoraiz/ascii-image-converter) for images On Debian/Devuan based systems you can install them like so: ```bash -sudo apt update; sudo apt install -y bat tree hexyl atool poppler-utils odt2txt catdoc +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. diff --git a/fzf--previewer.zsh b/fzf--previewer.zsh index cefe8f8..85554f3 100755 --- a/fzf--previewer.zsh +++ b/fzf--previewer.zsh @@ -4,8 +4,16 @@ 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 @@ -64,7 +72,13 @@ else [jJ][pP][gG]|[jJ][pP][eE][gG]|[gG][iI][fF]|[bB][mM][pP]|webp|[pP][nN][gG]|\ [tT][iI][fF]|[tT][iI][fF][fF]) - ascii-image-converter --width $[$COLUMNS - 20] --braille --color "${FILE_PATH}" && exit 0 + 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;; *)