2024-05-30 09:32:00 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# Dependencies:
|
|
|
|
# - https://github.com/sharkdp/bat
|
|
|
|
# - https://github.com/hpjansson/chafa
|
|
|
|
# - https://iterm2.com/utilities/imgcat
|
|
|
|
# - pdftoppm
|
|
|
|
# - exiftool
|
|
|
|
# - ffmpegthumbnailer
|
2024-06-13 12:04:48 +02:00
|
|
|
# - ImageMagick (magick)
|
2024-05-30 09:32:00 +02:00
|
|
|
|
|
|
|
if [[ $# -ne 1 ]]; then
|
|
|
|
>&2 echo "usage: $0 FILENAME"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
file=${1/#\~\//$HOME/}
|
|
|
|
type=$(file --dereference --brief --mime-type -- "$file")
|
|
|
|
|
|
|
|
# Array to hold temp files for cleanup
|
|
|
|
temp_files=()
|
|
|
|
|
|
|
|
# Cleanup function to remove temp files
|
|
|
|
cleanup() {
|
|
|
|
for temp_file in "${temp_files[@]}"; do
|
|
|
|
rm -f "$temp_file"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# Register the cleanup function to run on script exit
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
|
|
|
# Function to preview text files
|
|
|
|
preview_text() {
|
2024-06-08 06:14:08 +02:00
|
|
|
bat --style="${BAT_STYLE:-numbers,header}" --color=always --pager=never --line-range :19 -- "$file"
|
|
|
|
}
|
|
|
|
|
|
|
|
preview_json() {
|
|
|
|
jq -C . "$file"
|
|
|
|
}
|
|
|
|
|
|
|
|
preview_diff() {
|
|
|
|
delta < "$file"
|
|
|
|
}
|
|
|
|
|
|
|
|
preview_markdown() {
|
|
|
|
glow -s auto -w "$FZF_PREVIEW_COLUMNS" "$file"
|
2024-05-30 09:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
preview_image() {
|
|
|
|
local image_file="$1"
|
|
|
|
dim=${FZF_PREVIEW_COLUMNS}x${FZF_PREVIEW_LINES}
|
|
|
|
if [[ $dim = x ]]; then
|
|
|
|
dim=$(stty size </dev/tty | awk '{print $2 "x" $1}')
|
|
|
|
elif ! [[ $KITTY_WINDOW_ID ]] && ((FZF_PREVIEW_TOP + FZF_PREVIEW_LINES == $(stty size </dev/tty | awk '{print $1}'))); then
|
|
|
|
dim=${FZF_PREVIEW_COLUMNS}x$((FZF_PREVIEW_LINES - 1))
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $KITTY_WINDOW_ID ]]; then
|
|
|
|
kitty icat --clear --transfer-mode=memory --unicode-placeholder --stdin=no --place="$dim@0x0" "$image_file" | sed '$d' | sed $'$s/$/\e[m/'
|
|
|
|
elif command -v chafa >/dev/null; then
|
|
|
|
chafa -f sixel -O 9 -w 1 -s "$dim" "$image_file"
|
|
|
|
echo
|
|
|
|
elif command -v imgcat >/dev/null; then
|
|
|
|
imgcat -W "${dim%%x*}" -H "${dim##*x}" "$image_file"
|
|
|
|
else
|
|
|
|
file "$image_file"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
preview_pdf() {
|
|
|
|
temp_image=$(mktemp /tmp/preview.XXXXXX)
|
|
|
|
temp_files+=("$temp_image" "${temp_image}.jpg")
|
|
|
|
if pdftoppm -jpeg -f 1 -singlefile "$file" "$temp_image" >/dev/null 2>&1; then
|
|
|
|
temp_image="${temp_image}.jpg"
|
|
|
|
preview_image "$temp_image"
|
|
|
|
else
|
|
|
|
file "$file"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
preview_audio() {
|
|
|
|
if command -v exiftool >/dev/null; then
|
|
|
|
exiftool -FileName -Title -Artist -Album -Genre -ChannelMode -AudioBitrate -FileSize -MIMEType -SampleRate -Duration "$file"
|
|
|
|
else
|
|
|
|
echo "exiftool not found."
|
|
|
|
file "$file"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
preview_video() {
|
|
|
|
temp_image=$(mktemp /tmp/preview.XXXXXX.jpg)
|
|
|
|
temp_files+=("$temp_image")
|
|
|
|
if ffmpegthumbnailer -i "$file" -o "$temp_image" -s 0 >/dev/null 2>&1; then
|
2024-06-13 12:04:48 +02:00
|
|
|
magick "$temp_image" -resize 25% "$temp_image"
|
2024-05-30 09:32:00 +02:00
|
|
|
preview_image "$temp_image"
|
|
|
|
else
|
|
|
|
file "$file"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-06-08 06:14:08 +02:00
|
|
|
preview_archive() {
|
2024-05-30 09:32:00 +02:00
|
|
|
atool --list -- "$file"
|
|
|
|
}
|
|
|
|
|
|
|
|
preview_dir() {
|
|
|
|
echo
|
|
|
|
eza -Ga --group-directories-first --icons=always --color=always "$file"
|
|
|
|
}
|
|
|
|
|
|
|
|
case "$type" in
|
|
|
|
inode/directory)
|
|
|
|
preview_dir "$file"
|
|
|
|
;;
|
2024-06-08 06:14:08 +02:00
|
|
|
text/x-patch)
|
|
|
|
preview_diff "$file"
|
|
|
|
;;
|
|
|
|
text/x-diff)
|
|
|
|
preview_diff "$file"
|
2024-06-03 07:28:14 +02:00
|
|
|
;;
|
2024-05-30 09:32:00 +02:00
|
|
|
text/*)
|
2024-06-08 06:14:08 +02:00
|
|
|
case "$file" in
|
|
|
|
*.md|*.markdown)
|
|
|
|
preview_markdown "$file"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
preview_text "$file"
|
|
|
|
;;
|
|
|
|
esac
|
2024-05-30 09:32:00 +02:00
|
|
|
;;
|
|
|
|
image/*)
|
|
|
|
preview_image "$file"
|
|
|
|
;;
|
2024-06-08 06:14:08 +02:00
|
|
|
application/json)
|
|
|
|
preview_json "$file"
|
|
|
|
;;
|
2024-05-30 09:32:00 +02:00
|
|
|
application/pdf)
|
|
|
|
preview_pdf "$file"
|
|
|
|
;;
|
|
|
|
audio/*)
|
|
|
|
preview_audio "$file"
|
|
|
|
;;
|
|
|
|
video/*)
|
|
|
|
preview_video "$file"
|
|
|
|
;;
|
2024-06-08 06:14:08 +02:00
|
|
|
application/zip|application/x-tar|application/gzip|application/x-bzip2|application/x-xz|application/x-rar|application/x-7z-compressed)
|
|
|
|
preview_archive "$file"
|
2024-05-30 09:32:00 +02:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
exiftool "$file"
|
|
|
|
;;
|
|
|
|
esac
|