3 Commits
1.2 ... 1.4

Author SHA1 Message Date
fbt
958ae3f264 associative arrays mess up sorting 2015-01-13 23:40:37 +03:00
fbt
d8b9b5dc01 ^file:// is redundant 2014-11-26 15:59:17 +03:00
fbt
7696ea5ce8 ugh, making this work with mimes is annoying 2014-11-25 13:22:15 +03:00
2 changed files with 21 additions and 23 deletions

36
sx-open
View File

@@ -1,9 +1,6 @@
#!/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+.-]+)?://.+'
@@ -14,9 +11,11 @@ usage() { echo "${0##*/} <uri/file>"; }
handle_uri() {
local target="$1"
for h in "${!uri_handlers[@]}"; do
[[ "$target" =~ ${uri_handlers[${h}]} ]] && {
${h} "$target" &
for h in "${uri_handlers[@]}"; do
IFS='=' read cmd regex <<< "$h"
[[ "$target" =~ ${regex} ]] && {
${cmd} "$target" &
return 0
}
done
@@ -27,9 +26,11 @@ handle_uri() {
handle_mime() {
target_mimetype=$(file -ib "$target")
for m in "${!mime_handlers[@]}"; do
[[ "$target_mimetype" =~ ${mime_handlers[${m}]} ]] && {
${m} "$target" &
for m in "${mime_handlers[@]}"; do
IFS='=' read cmd regex <<< "$m"
[[ "$target_mimetype" =~ ${regex} ]] && {
${cmd} "$target" &
return 0
}
done
@@ -41,20 +42,17 @@ main() {
target="$1"
[[ "$target" ]] || { usage; exit; }
if [[ "$target" =~ ${cfg_uri_regex} ]]; then
handle_uri "$target"
elif [[ -e "$target" ]]; then
[[ "$target" =~ ^/.* ]] || { target="${PWD}/${target}"; }
handle_uri "$target" || {
[[ "$target" =~ file://.+ ]] && { target="${target##*file://}"; }
[[ -e "$target" ]] && {
[[ "$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"; }
[[ "$?" -gt 0 ]] && { echo "No handlers found for $target"; }
return 0
}

View File

@@ -2,13 +2,13 @@
# Note that as sx-open checks the regexes in order, they should be placed in order from specific to less so.
uri_handlers=(
["steam"]='^steam://.+'
["browser"]='.+'
["javaws"]='^file:///.+?kvm.+?\.cgi$'
'steam=^steam://.+'
'javaws=.+kvm.+?\.cgi$'
'browser=^http(s)://.+'
)
mime_handlers=(
["sxiv"]='image/.+'
'sxiv=image/.+'
)
# vim: syntax=sh