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:
parent
97b1be6732
commit
214ab03647
34
sx-open
34
sx-open
|
@ -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"; }
|
||||
|
|
|
@ -4,10 +4,7 @@
|
|||
uri_handlers=(
|
||||
["steam"]='^steam://.+'
|
||||
["browser"]='.+'
|
||||
)
|
||||
|
||||
filename_handlers=(
|
||||
["javaws"]='^kvm.+?\.cgi$'
|
||||
["javaws"]='^file:///.+?kvm.+?\.cgi$'
|
||||
)
|
||||
|
||||
mime_handlers=(
|
||||
|
|
Loading…
Reference in New Issue
Block a user