Home

Ueberzug Image Previews with FZF

Screenshot of the dmenu bookmarks script in action

Shell script wrapper for fzf that shows image previews for various filetypes, such as images, PDFs and videos.

Based on https://github.com/thimc/lfimg

Requisites

  • fzf
  • ueberzug (note: this is available in the apt repositories, but not on Github)
  • Various individual programs, such as ffmpegthumbnailer, pdftoppm, et cetera. See preview below for the full list.

Instructions

  1. Download fzfimg, cleaner and preview into one directory.
  2. Run chmod +x fzfimg cleaner preview to make them executable.
  3. Run ./fzfimg
  4. (Optional) Create an alias by adding alias fzfimg=~/Downloads/fzfimg/fzfimg to ~/.bashrc (which the approriate path, of course).

An Extra Suggestion

This script works well with another script for creating blurred backgrounds (see instructions here).

Scripts

fzfimg

#!/bin/sh
set -e

DIR=$(dirname "$0")
echo "$DIR"

cleanup() {
    exec 3>&-
	rm "$FIFO_UEBERZUG"
}

if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
	fzf "$@"
else
	[ ! -d "$HOME/.cache/fzf" ] && mkdir --parents "$HOME/.cache/fzf"
	export FIFO_UEBERZUG="$HOME/.cache/fzf/ueberzug-$$"
	mkfifo "$FIFO_UEBERZUG"
	ueberzug layer -s <"$FIFO_UEBERZUG" -p json &
	exec 3>"$FIFO_UEBERZUG"
	trap cleanup EXIT
    fzf --layout=reverse --preview ""$DIR"/cleaner && "$DIR"/preview {}"
    ~/Documents/Programs/Scripts/fzfimg/cleaner
fi

cleaner

#!/bin/sh
if [ -n "$FIFO_UEBERZUG" ]; then
	printf '{"action": "remove", "identifier": "PREVIEW"}\n' > "$FIFO_UEBERZUG"
fi

preview

#!/bin/sh
CACHE="$HOME/.cache/lf/thumbnail.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' \
	-- "$(readlink -f "$1")" | sha256sum | awk '{print $1}'))"

image() {
	if [ -n "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ]; then
		printf '{"action": "add", "identifier": "PREVIEW", "x": "%s", "y":"%s", "width": "%s", "height": "%s", "scaler": "contain", "path": "%s"}\n' \
            $(($(tput cols) + 7)) 1 \
            "${FZF_PREVIEW_COLUMNS}" "${FZF_PREVIEW_LINES}" "$1"\
            > "$FIFO_UEBERZUG"
		exit 1
	else
		chafa "$1" -s "$4x"
	fi
}

batorcat() {
	file="$1"
	shift
	if command -v bat > /dev/null 2>&1; then
		batcat --color=always --style=plain --pager=never "$file" "$@"
	else
		cat "$file"
	fi
}

case "$(printf "%s\n" "$(readlink -f "$1")" | awk '{print tolower($0)}')" in
	*.tgz|*.tar.gz) tar tzf "$1" ;;
	*.tar.bz2|*.tbz2) tar tjf "$1" ;;
	*.tar.txz|*.txz) xz --list "$1" ;;
	*.tar) tar tf "$1" ;;
	*.zip|*.jar|*.war|*.ear|*.oxt) unzip -l "$1" ;;
	*.rar) unrar l "$1" ;;
	*.7z) 7z l "$1" ;;
	*.[1-8]) man "$1" | col -b ;;
	*.o) nm "$1";;
	*.torrent) transmission-show "$1" ;;
	*.iso) iso-info --no-header -l "$1" ;;
	*.odt|*.ods|*.odp|*.sxw) odt2txt "$1" ;;
	*.doc) catdoc "$1" ;;
	*.docx) docx2txt "$1" ;;
	*.xml|*.html) w3m -dump "$1";;
	*.xls|*.xlsx)
		ssconvert --export-type=Gnumeric_stf:stf_csv "$1" "fd://1" | batorcat --language=csv
		;;
	*.wav|*.mp3|*.flac|*.m4a|*.wma|*.ape|*.ac3|*.og[agx]|*.spx|*.opus|*.as[fx]|*.mka)
		exiftool "$1"
		;;
	*.pdf)
		[ ! -f "${CACHE}.jpg" ] && pdftoppm -jpeg -f 1 -singlefile "$1" "$CACHE"
		image "${CACHE}.jpg" 
		;;
	*.epub)
		[ ! -f "$CACHE" ] && epub-thumbnailer "$1" "$CACHE" 1024
		image "$CACHE" 
		;;
	*.cbz|*.cbr|*.cbt)
		[ ! -f "$CACHE" ] && comicthumb "$1" "$CACHE" 1024
		image "$CACHE" 
		;;
	*.avi|*.mp4|*.wmv|*.dat|*.3gp|*.ogv|*.mkv|*.mpg|*.mpeg|*.vob|*.fl[icv]|*.m2v|*.mov|*.webm|*.ts|*.mts|*.m4v|*.r[am]|*.qt|*.divx)
		[ ! -f "${CACHE}.jpg" ] && \
			ffmpegthumbnailer -i "$1" -o "${CACHE}.jpg" -s 0 -q 5
		image "${CACHE}.jpg" 
		;;
	*.bmp|*.jpg|*.jpeg|*.png|*.xpm|*.webp|*.tiff|*.gif|*.jfif|*.ico)
		image "$1" 
		;;
	*.svg)
		[ ! -f "${CACHE}.jpg" ] && convert "$1" "${CACHE}.jpg"
		image "${CACHE}.jpg" 
		;;
	*.ino) batorcat --language=cpp "$1" ;;
	*) batorcat "$1" ;;
esac
exit 0

Tags

Categories

If you have questions, you can email me at jeremy.w.cains at gmail.com. View page source here.