From b2eb1eabf09649ab4897a0b06c394ee6a4ff31fd Mon Sep 17 00:00:00 2001 From: fbt Date: Thu, 25 Sep 2014 19:05:17 +0400 Subject: [PATCH] made the code a bit cleaner --- sx-open | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/sx-open b/sx-open index 997e5e2..ba12725 100755 --- a/sx-open +++ b/sx-open @@ -8,33 +8,39 @@ cfg_file="$HOME/.config/sx-open.cfg" usage() { echo "usage function not implemented yet."; } handle_uri() { + local target="$1" + for h in "${!uri_handlers[@]}"; do grep -oE "${uri_handlers[${h}]}" <<< "$target" && { - "$h" "$target" - break + ${h} "$target" & + return 0 } done + + return 1 } handle_file() { + local target="$1" + + [[ -f "$target" ]] || return 1 + target_mimetype=$(file -ib "$target") for m in "${!mime_handlers[@]}"; do grep -oE "${mime_handlers[${m}]}" <<< "$target_mimetype" && { - "$m" "$target" - break + ${m} "$target" & + return 0 } done + + return 1 } main() { - target="$1"; [[ "$target" ]] || { usage; exit; } + [[ "$1" ]] || { usage; exit; } - if [[ -f "$target" ]]; then - handle_file - else - handle_uri - fi + handle_file "$1" || handle_uri "$1" } main "$@"