Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
6afb99d1ce | |||
0deb0b449c | |||
597c801bdf | |||
0347154d6b | |||
2470681dcb | |||
6cb89f0cd6 | |||
958ae3f264 | |||
303bdf5d71 | |||
d8b9b5dc01 | |||
7696ea5ce8 |
39
sx-open
39
sx-open
@@ -1,22 +1,20 @@
|
|||||||
#!/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() { printf '%s <uri/file>\n' "${0##*/}"; }
|
||||||
|
|
||||||
handle_uri() {
|
handle_uri() {
|
||||||
local target="$1"
|
local target="$1"
|
||||||
|
|
||||||
for h in "${!uri_handlers[@]}"; do
|
for h in "${uri_handlers[@]}"; do
|
||||||
[[ "$target" =~ ${uri_handlers[${h}]} ]] && {
|
IFS='=' read cmd regex <<< "$h"
|
||||||
${h} "$target" &
|
|
||||||
|
[[ "$target" =~ ${regex} ]] && {
|
||||||
|
${cmd} "$target" &
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
done
|
done
|
||||||
@@ -25,11 +23,13 @@ handle_uri() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
handle_mime() {
|
handle_mime() {
|
||||||
target_mimetype=$(file -ib "$target")
|
IFS=';' read target_mimetype charset <<< $( file -ib "$target" )
|
||||||
|
|
||||||
for m in "${!mime_handlers[@]}"; do
|
for m in "${mime_handlers[@]}"; do
|
||||||
[[ "$target_mimetype" =~ ${mime_handlers[${m}]} ]] && {
|
IFS='=' read cmd regex <<< "$m"
|
||||||
${m} "$target" &
|
|
||||||
|
[[ "$target_mimetype" =~ ${regex} ]] && {
|
||||||
|
${cmd} "$target" &
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
done
|
done
|
||||||
@@ -41,20 +41,17 @@ main() {
|
|||||||
target="$1"
|
target="$1"
|
||||||
[[ "$target" ]] || { usage; exit; }
|
[[ "$target" ]] || { usage; exit; }
|
||||||
|
|
||||||
if [[ "$target" =~ ${cfg_uri_regex} ]]; then
|
handle_uri "$target" || {
|
||||||
handle_uri "$target"
|
[[ "$target" =~ file://.+ ]] && { target="${target##*file://}"; }
|
||||||
elif [[ -e "$target" ]]; then
|
|
||||||
|
[[ -e "$target" ]] && {
|
||||||
[[ "$target" =~ ^/.* ]] || { target="${PWD}/${target}"; }
|
[[ "$target" =~ ^/.* ]] || { target="${PWD}/${target}"; }
|
||||||
|
|
||||||
handle_uri "file://${target}" || {
|
|
||||||
handle_mime "$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 ]] && { printf "No handlers found for %s\n" "$target"; }
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
@@ -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.
|
# Note that as sx-open checks the regexes in order, they should be placed in order from specific to less so.
|
||||||
|
|
||||||
uri_handlers=(
|
uri_handlers=(
|
||||||
["steam"]='^steam://.+'
|
'steam=^steam://.+'
|
||||||
["browser"]='.+'
|
'javaws=.+kvm.+?\.cgi$'
|
||||||
["javaws"]='^file:///.+?kvm.+?\.cgi$'
|
'browser=^https?://.+'
|
||||||
)
|
)
|
||||||
|
|
||||||
mime_handlers=(
|
mime_handlers=(
|
||||||
["sxiv"]='image/.+'
|
'sxiv=image/.+'
|
||||||
)
|
)
|
||||||
|
|
||||||
# vim: syntax=sh
|
# vim: syntax=sh
|
||||||
|
Reference in New Issue
Block a user