sx-open/sx-open

67 lines
1.2 KiB
Plaintext
Raw Normal View History

2014-09-25 13:26:15 +00:00
#!/usr/bin/env bash
# This is an attempt to replace xdg-open with something sane.
# Source the config file.
cfg_file="$HOME/.config/sx-open.cfg"
[[ -f "$cfg_file" ]] && { source "$cfg_file"; }
2014-11-08 05:03:59 +00:00
usage() { echo "${0##*/} <uri/file>"; }
2014-09-25 13:26:15 +00:00
handle_uri() {
2014-09-25 15:05:17 +00:00
local target="$1"
2014-09-25 13:26:15 +00:00
for h in "${!uri_handlers[@]}"; do
2014-10-16 09:46:57 +00:00
[[ "$target" =~ ${uri_handlers[${h}]} ]] && {
2014-09-25 15:05:17 +00:00
${h} "$target" &
return 0
2014-09-25 13:26:15 +00:00
}
done
2014-09-25 15:05:17 +00:00
return 1
2014-09-25 13:26:15 +00:00
}
handle_fs_target() {
2014-09-25 15:27:52 +00:00
local target="${1##*file://}"
2014-09-25 15:05:17 +00:00
target_filename="${target##*/}"
for n in "${!filename_handlers[@]}"; do
[[ "$target_filename" =~ ${filename_handlers[${n}]} ]] && {
${n} "$target" &
return 0
}
done
2014-09-25 13:26:15 +00:00
target_mimetype=$(file -ib "$target")
for m in "${!mime_handlers[@]}"; do
2014-10-16 09:46:57 +00:00
[[ "$target_mimetype" =~ ${mime_handlers[${m}]} ]] && {
2014-09-25 15:05:17 +00:00
${m} "$target" &
return 0
2014-09-25 13:26:15 +00:00
}
done
2014-09-25 15:05:17 +00:00
return 1
2014-09-25 13:26:15 +00:00
}
main() {
declare -A uri_handlers
declare -A mime_handlers
declare -A filename_handlers
target="$1"
[[ "$target" ]] || { usage; exit; }
2014-09-25 15:27:52 +00:00
if [[ -e "$target" || "$target" == 'file://'* ]]; then
handle_fs_target "$target"
else
handle_uri "$target"
fi
2014-09-25 18:16:11 +00:00
[[ "$?" -gt 0 ]] && { echo "No handlers found for $target"; }
return 0
2014-09-25 13:26:15 +00:00
}
main "$@"