Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.
94 changes: 94 additions & 0 deletions fzf--previewer.zsh
Original file line number Diff line number Diff line change
@@ -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
12 changes: 0 additions & 12 deletions opener.bash

This file was deleted.