nix/bin/mailcap-open

65 lines
1.5 KiB
Bash
Executable file

#!/bin/bash
# Open file with mailcap viewer selected via fzf
export PATH="/etc/profiles/per-user/$USER/bin:/run/current-system/sw/bin:$PATH"
# Handle piped input or file argument
if [[ -n "$1" ]]; then
file="$1"
else
# Read from stdin to temp file
tmpfile=$(mktemp)
cat > "$tmpfile"
mime=$(file --mime-type -b "$tmpfile")
# Add extension based on mime type
case "$mime" in
application/pdf) ext=".pdf" ;;
image/png) ext=".png" ;;
image/jpeg) ext=".jpg" ;;
image/gif) ext=".gif" ;;
text/html) ext=".html" ;;
text/plain) ext=".txt" ;;
*) ext="" ;;
esac
if [[ -n "$ext" ]]; then
mv "$tmpfile" "${tmpfile}${ext}"
file="${tmpfile}${ext}"
else
file="$tmpfile"
fi
fi
mime=$(file --mime-type -b "$file")
# Get matching viewers based on mime type
viewers=()
viewers+=("open (default app)")
case "$mime" in
application/pdf)
viewers+=("zathura")
;;
image/*)
viewers+=("chafa (terminal)")
;;
text/html)
viewers+=("w3m (browser)")
viewers+=("less (text)")
;;
text/*)
viewers+=("less")
;;
esac
# Select with fzf
selected=$(printf '%s\n' "${viewers[@]}" | fzf --prompt="Open with: " --height=10)
case "$selected" in
"open (default app)") open "$file" ;;
"chafa (terminal)") chafa "$file"; read -n 1 -s -r -p "Press any key..." ;;
"zathura") zathura "$file" ;;
"w3m (browser)") w3m -T text/html "$file" ;;
"less"*) less "$file" ;;
esac