From 958ae3f264189b940ea6204762faf66ef97f99a5 Mon Sep 17 00:00:00 2001 From: fbt Date: Tue, 13 Jan 2015 23:40:37 +0300 Subject: [PATCH 1/2] associative arrays mess up sorting --- sx-open | 19 ++++++++++--------- sx-open.cfg | 8 ++++---- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/sx-open b/sx-open index 994b675..e7c7c96 100755 --- a/sx-open +++ b/sx-open @@ -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##*/} "; } 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 diff --git a/sx-open.cfg b/sx-open.cfg index fe2c2d8..5601418 100644 --- a/sx-open.cfg +++ b/sx-open.cfg @@ -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://.+' - ["javaws"]='.+kvm.+?\.cgi$' - ["browser"]='^http(s)://.+' + 'steam=^steam://.+' + 'javaws=.+kvm.+?\.cgi$' + 'browser=^http(s)://.+' ) mime_handlers=( - ["sxiv"]='image/.+' + 'sxiv=image/.+' ) # vim: syntax=sh From 6cb89f0cd680751599f1ca3038b3ab41467e3485 Mon Sep 17 00:00:00 2001 From: fbt Date: Sat, 17 Jan 2015 16:38:52 +0300 Subject: [PATCH 2/2] Code cleanup, thx to http://www.shellcheck.net --- sx-open | 1 - 1 file changed, 1 deletion(-) diff --git a/sx-open b/sx-open index e7c7c96..5f08eaa 100755 --- a/sx-open +++ b/sx-open @@ -3,7 +3,6 @@ # 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##*/} "; }