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

View File

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