dotfiles/bin/executable_xdg-open

67 lines
1.4 KiB
Bash

#!/usr/bin/env bash
set -eo pipefail
export SWAYSOCK=/run/user/1000/sway-ipc.sock
# Strip file:// protocol as file -b will handle this
argument=$(echo $1 | sed "s|file://||")
url_regex='^(https?|alfaview|mailto|ftp):/?/?.*'
if [[ $argument =~ $url_regex ]]; then
match=$argument
else
match=$(file -b --mime-type "${argument}")
fi;
echo "Searching for ${match} in handlers"
case "${match}" in
*.slack.*)
exec qutebrowser-comms "$1"
;;
https://staging-app*)
exec chromium "$1"
;;
inode/directory)
exec browse-folder "${argument}"
;;
alfaview:*)
exec alfaview "${argument}"
;;
ssh:*)
host=$(echo $1 | cut -d "/" -f 3)
exec foot ssh "$host"
;;
text/html|http://*|https://*)
exec browser "$1"
;;
application/x-postscript|image/x-eps|application/pdf)
exec zathura "$1"
;;
text/calendar)
echo $1 | wl-copy --trim-newline
exec notify-send -i x-office-calendar "Calendar Event" "Copied path ${@} to clipboard" >&2
;;
image/gif|video/*)
exec mpv --loop=inf "$1"
;;
image/*)
exec imv "$1"
;;
atom://*)
exec nix-shell -p atom --command "$1"
;;
application/vnd.openxmlformats-officedocument*|application/vnd.oasis.opendocument*)
exec libreoffice "$1"
;;
text/x-tex|application/json|text/plain|mailto://*)
exec em "$argument"
;;
*)
notify-send "${0}" "Don't know how to open ${@} of type ${match}" -t 2500 >&2
exec foot --app-id 'files' ranger $@
;;
esac