#!/usr/bin/env bash # fzf_preview_mine file="$1" center() { local text="$1" local width="$FZF_PREVIEW_COLUMNS" local len=${#text} local pad=$(( (width - len) / 2 )) [[ $pad -lt 0 ]] && pad=0 printf "%*s%s\n" "$pad" "" "$text" } generic() { fold -s -w "$FZF_PREVIEW_COLUMNS" "$1" | head -n 2500 } # echo centered extension or filename (if there is no extension) base="${file##*/}" if [[ "$base" == *.* && "$base" != .* ]]; then ext="${base##*.}" else ext="$base" fi center "$ext" mime=$(file --mime-type -b "$file" 2>/dev/null) encoding=$(file --mime-encoding -b "$file" 2>/dev/null) debug="0" tmp=$(mktemp -d) cleanup() { pkill -P $$ sleep 0.1 rm -rf "$tmp" } trap cleanup EXIT chafaView () { clear local file width_px height_px width_px="$((FZF_PREVIEW_COLUMNS))" height_px="$((FZF_PREVIEW_LINES))" file="$1" chafa --dither=diffusion -f sixels -s "${width_px}x${height_px}" "$file" 2>/dev/null echo } if [[ $mime == image/* ]]; then (( debug )) && echo "image" if [[ "$file" == *.psd ]] || [[ "$file" = *.avif ]]; then convert "$file" -flatten "$tmp/tmp.png" file="$tmp/tmp.png" fi if [[ "$file" == *.svg ]]; then inkscape "$file" --actions="export-filename:$tmp/tmp.png; export-do" 2> /dev/null file="$tmp/tmp.png" fi chafaView "$file" || generic "$file" elif [[ $mime == audio/* ]] || [[ "${file,,}" == *.m4a ]] || [[ "${file,,}" == *.mp3 ]]; then mediainfo "$file" 2> /dev/null mpv --no-video --quiet "$file" 2> /dev/null elif [[ $mime == video/* ]] || [[ "${file,,}" == *.mxf ]]; then (( debug )) && echo "video" dur=$(ffprobe -v panic -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file") [[ -z "$dur" || "$dur" == "N/A" ]] && dur=50 halfdur=$(bc -l <<< "$dur / 2.00") ffmpeg -hide_banner -loglevel panic -ss "$halfdur" -i "$file" -frames:v 1 "$tmp/middle.png" -y chafaView "$tmp/middle.png" elif [[ "$file" == *.epub ]]; then { epub2txt -a -n "$file" 2>/dev/null || pandoc "$file" -t plain 2>/dev/null } | fold -s -w "$FZF_PREVIEW_COLUMNS" elif [[ "$file" == *.mobi ]]; then ebook-convert "$file" "$tmp/tmp.txt" 2>/dev/null clear fold -s -w "$FZF_PREVIEW_COLUMNS" "$tmp/tmp.txt" elif [[ "$file" == *.html ]] || [[ "$file" == *.htm ]]; then w3m -o display_link_number=1 -dump -cols "$FZF_PREVIEW_COLUMNS" "$file" || generic "$file" elif [[ "$mime" == application/pdf ]]; then # graphical, show 1st page only pdftocairo -png -scale-to 1200 -singlefile "$file" "$tmp/out" chafaView "$tmp/out.png" # textual, extract all text pdftotext -layout "$file" - 2>/dev/null \ | fold -s -w "$FZF_PREVIEW_COLUMNS" # use shift + up/down to switch between two previews # atool for compressed stuff elif [[ "$file" == *.zip ]] || [[ "$file" == *.tar* ]] || [[ "$file" == *.7z ]] || [[ "$file" == *.rar ]]; then als -l "$file" 2>/dev/null | head -n 500 elif [[ "$file" == *.deb ]]; then dpkg-deb --contents "$file" # fonts elif [[ "$file" == *.otf ]] || [[ "$file" == *.ttf ]] || [[ "$file" == *.woff ]]; then name="${file##*/}" # fira.ttf name="${name%.*}" # fira name="${name^^}" # FIRA convert -size 1600x800 xc:black \ -font "$file" \ -fill white \ -gravity northwest \ \ -pointsize 120 \ -annotate +40+80 "${name}" \ \ -pointsize 60 \ -annotate +40+320 "The quick brown čšž ČŠŽ\njumps over the lazy dog\n\nIN RHONCUS NULLA VEL ANTE,\nQUIS BLANDIT TURPIS SAGITTIS.\nSED SODALES, JUSTO AC PLACERAT." \ \ "$tmp/font.png" #open "$tmp/font.png" chafaView "$tmp/font.png" elif [[ $encoding == binary ]]; then echo "[BINARY: $(basename "$file")]" xxd "$file" | head -100 || strings "$file" | head -300 else generic "$file" fi