No no no, that was ugly as hell. Files need to be handled as uris if we want to do so by name

This commit is contained in:
Jack L. Frost 2014-11-21 14:14:01 +03:00
parent 97b1be6732
commit 214ab03647
2 changed files with 16 additions and 23 deletions

34
sx-open
View File

@ -1,8 +1,12 @@
#!/usr/bin/env bash
# This is an attempt to replace xdg-open with something sane.
declare -A uri_handlers
declare -A mime_handlers
# Source the config file.
cfg_file="$HOME/.config/sx-open.cfg"
cfg_uri_regex='^[A-Za-z]([A-Za-z0-9+.-]+)?://.+'
[[ -f "$cfg_file" ]] && { source "$cfg_file"; }
usage() { echo "${0##*/} <uri/file>"; }
@ -20,18 +24,7 @@ handle_uri() {
return 1
}
handle_fs_target() {
local target="${1##*file://}"
target_filename="${target##*/}"
for n in "${!filename_handlers[@]}"; do
[[ "$target_filename" =~ ${filename_handlers[${n}]} ]] && {
${n} "$target" &
return 0
}
done
handle_mime() {
target_mimetype=$(file -ib "$target")
for m in "${!mime_handlers[@]}"; do
@ -45,17 +38,20 @@ handle_fs_target() {
}
main() {
declare -A uri_handlers
declare -A mime_handlers
declare -A filename_handlers
target="$1"
[[ "$target" ]] || { usage; exit; }
if [[ -e "$target" || "$target" == 'file://'* ]]; then
handle_fs_target "$target"
else
if [[ "$target" =~ ${cfg_uri_regex} ]]; then
handle_uri "$target"
elif [[ -e "$target" ]]; then
[[ "$target" =~ ^/.* ]] || { target="${PWD}/${target}"; }
handle_uri "file://${target}" || {
handle_mime "$target"
}
else
echo "$target is not a uri nor is it an existing file. Bailing."
return 1
fi
[[ "$?" -gt 0 ]] && { echo "No handlers found for $target"; }

View File

@ -4,10 +4,7 @@
uri_handlers=(
["steam"]='^steam://.+'
["browser"]='.+'
)
filename_handlers=(
["javaws"]='^kvm.+?\.cgi$'
["javaws"]='^file:///.+?kvm.+?\.cgi$'
)
mime_handlers=(