Compare commits
No commits in common. "master" and "1.3" have entirely different histories.
22
README.md
22
README.md
|
@ -1,22 +1,10 @@
|
||||||
# sx-open
|
sx-open
|
||||||
|
=======
|
||||||
|
|
||||||
sx-open is an attempt to implement a saner alternative to xdg-open.
|
sx-open is an attempt to implement a saner alternative to xdg-open.
|
||||||
|
|
||||||
## Installation
|
Installation
|
||||||
|
------------
|
||||||
|
|
||||||
Clone the repo, drop sx-open.cfg into your $HOME/.config and set it up to do what you want. There are no built-in defaults, it's all in the config.
|
Clone the repo, drop sx-open.cfg into your $HOME/.config.
|
||||||
As this thing is meant to replace xdg-open, you will probably want to link sx-open into xdg-open somewhere in your $PATH as to override the default one.
|
As this thing is meant to replace xdg-open, you will probably want to link sx-open into xdg-open somewhere in your $PATH as to override the default one.
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
sx-open [-dhv] <uri/file>
|
|
||||||
Flags:
|
|
||||||
-d Dry run
|
|
||||||
-v Verbose
|
|
||||||
-h Help
|
|
||||||
|
|
||||||
### Exit codes
|
|
||||||
|
|
||||||
* 1 — action failed
|
|
||||||
* 2 — file not found
|
|
||||||
* 3 — no handlers found
|
|
||||||
|
|
275
sx-open
275
sx-open
|
@ -1,262 +1,57 @@
|
||||||
#!/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.
|
||||||
|
|
||||||
usage() {
|
declare -A uri_handlers
|
||||||
cat <<- EOF
|
declare -A mime_handlers
|
||||||
sx-open [-dhv] <uri/file>
|
|
||||||
Flags:
|
|
||||||
-d Dry run
|
|
||||||
-v Verbose mode
|
|
||||||
-n Disable desktop notifications
|
|
||||||
-h Help
|
|
||||||
EOF
|
|
||||||
}
|
|
||||||
|
|
||||||
act() {
|
|
||||||
cfg verbose && printf 'CMD: %s\n' "$*" >&2
|
|
||||||
cfg dryrun || { "$@"; return $?; }
|
|
||||||
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
urldecode() { : "${*//+/ }"; printf '%b\n' "${_//%/\\x}"; }
|
|
||||||
|
|
||||||
# cfg foo bool = [true|1]
|
|
||||||
# cfg foo [string] = 'bar'
|
|
||||||
# cfg foo
|
|
||||||
cfg() {
|
|
||||||
declare s_name=$1; shift
|
|
||||||
declare -n \
|
|
||||||
_s="cfg[$s_name]" \
|
|
||||||
_s_type="cfg[${s_name}_type]"
|
|
||||||
|
|
||||||
if (( $# )); then
|
|
||||||
while (( $# )); do
|
|
||||||
case $1 in
|
|
||||||
(bool|str) _s_type="$1";;
|
|
||||||
|
|
||||||
(true|false|0|1)
|
|
||||||
_s_type='bool'
|
|
||||||
_s="$1"
|
|
||||||
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
|
|
||||||
(=)
|
|
||||||
[[ $_s_type ]] || _s_type='string'
|
|
||||||
|
|
||||||
[[ $_s_type == 'bool' ]] && {
|
|
||||||
case $2 in
|
|
||||||
(true|false|0|1) true;;
|
|
||||||
(*)
|
|
||||||
error "On line $LINENO, in $FUNCNAME: Invalid value for '$s_name' type 'bool': '$2'"
|
|
||||||
exit 111
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
}
|
|
||||||
|
|
||||||
_s="$2"
|
|
||||||
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
|
|
||||||
(*)
|
|
||||||
error "On line $LINENO, in $FUNCNAME: Syntax error: “cfg $s_name $*”"
|
|
||||||
exit 115
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
shift
|
|
||||||
done
|
|
||||||
else
|
|
||||||
[[ -n $_s ]] || {
|
|
||||||
error "On line $LINENO, in $FUNCNAME: invalid option: '$s_name'"
|
|
||||||
exit 113
|
|
||||||
}
|
|
||||||
|
|
||||||
case $_s_type in
|
|
||||||
(bool)
|
|
||||||
case $_s in
|
|
||||||
(true|1) return 0;;
|
|
||||||
(false|0) return 1;;
|
|
||||||
esac
|
|
||||||
;;
|
|
||||||
|
|
||||||
(*)
|
|
||||||
printf '%s\n' "$_s"
|
|
||||||
return 0
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
notify() {
|
|
||||||
cfg notify || return 7 # disabled
|
|
||||||
|
|
||||||
[[ $DISPLAY ]] || return 3
|
|
||||||
[[ $notifier ]] || return 5
|
|
||||||
|
|
||||||
"${notifier[@]}" 'sx-open' "$@"
|
|
||||||
}
|
|
||||||
|
|
||||||
error() {
|
|
||||||
printf '%s\n' "$*" >&2
|
|
||||||
notify "$*"
|
|
||||||
}
|
|
||||||
|
|
||||||
# handle_target <res> <uri>
|
|
||||||
# 1: cmd failed
|
|
||||||
# 3: no handler
|
|
||||||
handle_target() {
|
|
||||||
declare -n result=$1
|
|
||||||
declare h cmd regex target_is_file target target_left cmd_is_template
|
|
||||||
cmd_append_target=1
|
|
||||||
match=0
|
|
||||||
target=$2
|
|
||||||
target_left=$target
|
|
||||||
|
|
||||||
if [[ -e "$target" ]]; then
|
|
||||||
target_is_file=1
|
|
||||||
|
|
||||||
[[ "$target" =~ ^/.* ]] || { target="${PWD}/${target}"; } # Turn relative paths to absolute ones.
|
|
||||||
|
|
||||||
IFS=';' read target_mimetype _ <<< $( file -ib "$target" )
|
|
||||||
target_left=$target_mimetype
|
|
||||||
|
|
||||||
[[ $target_mimetype == 'inode/symlink' ]] && \
|
|
||||||
IFS=';' read target_mimetype_true _ <<< $( file -ibL "$target" )
|
|
||||||
|
|
||||||
set -- "${mime_handlers[@]}"
|
|
||||||
elif is_uri "$target"; then
|
|
||||||
set -- "${uri_handlers[@]}"
|
|
||||||
else
|
|
||||||
return 2
|
|
||||||
fi
|
|
||||||
|
|
||||||
while (( $# )); do
|
|
||||||
cmd_str=$1 regex=$2
|
|
||||||
cmd=()
|
|
||||||
|
|
||||||
# Fix the regex
|
|
||||||
[[ $regex =~ \^?([^\$]+)\$? ]] && regex="^${BASH_REMATCH[1]}$"
|
|
||||||
|
|
||||||
if [[ $cmd_str == *'%target%'* ]]; then
|
|
||||||
cmd=( ${cmd_str//%target%/$target} )
|
|
||||||
else
|
|
||||||
cmd=( $cmd_str "$target" )
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ $target_left =~ $regex ]]; then
|
|
||||||
match=1
|
|
||||||
elif [[ $target_mimetype_true ]]; then
|
|
||||||
[[ $target_mimetype_true =~ $regex ]] && match=1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if (( match )); then
|
|
||||||
act "${cmd[@]}"; result=$?
|
|
||||||
(( result )) && return 1
|
|
||||||
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
shift 2
|
|
||||||
done
|
|
||||||
|
|
||||||
return 3
|
|
||||||
}
|
|
||||||
|
|
||||||
# DSL
|
|
||||||
uri() {
|
|
||||||
declare r handler=$1; shift
|
|
||||||
|
|
||||||
for r in "$@"; do
|
|
||||||
uri_handlers+=( "$handler" "$r" )
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
mime() {
|
|
||||||
declare r handler=$1; shift
|
|
||||||
|
|
||||||
for r in "$@"; do
|
|
||||||
mime_handlers+=( "$handler" "$r" )
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
scheme() {
|
|
||||||
declare r handler=$1; shift
|
|
||||||
|
|
||||||
for s in "$@"; do
|
|
||||||
uri_handlers+=( "$handler" "$s:.+" )
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
is_uri() [[ $1 =~ ^[a-zA-Z][a-zA-Z0-9\+\.\-]+:.+ ]]
|
|
||||||
|
|
||||||
main() {
|
|
||||||
declare cmd_result target
|
|
||||||
|
|
||||||
declare -gA cfg
|
|
||||||
cfg verbose bool = false
|
|
||||||
cfg dryrun bool = false
|
|
||||||
cfg notify bool = true
|
|
||||||
|
|
||||||
# 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"; }
|
||||||
|
|
||||||
while (( $# )); do
|
usage() { echo "${0##*/} <uri/file>"; }
|
||||||
case $1 in
|
|
||||||
(-d)
|
|
||||||
cfg dryrun = true
|
|
||||||
cfg verbose = true
|
|
||||||
;;
|
|
||||||
|
|
||||||
(-v) cfg verbose = true;;
|
handle_uri() {
|
||||||
(-n) cfg notify = false;; # disable desktop notifications
|
local target="$1"
|
||||||
|
|
||||||
(-h) usage; return 0;;
|
for h in "${!uri_handlers[@]}"; do
|
||||||
|
[[ "$target" =~ ${uri_handlers[${h}]} ]] && {
|
||||||
(--) shift; break;;
|
${h} "$target" &
|
||||||
(*) break;;
|
return 0
|
||||||
esac
|
}
|
||||||
|
|
||||||
shift
|
|
||||||
done
|
done
|
||||||
|
|
||||||
target=$1; [[ "$target" ]] || { usage; exit; }
|
return 1
|
||||||
# target=$(urldecode "$target") # No idea why I thought this was necessary.
|
|
||||||
|
|
||||||
cfg dryrun && {
|
|
||||||
printf 'Dry run: not actually running the handler\n' >&2
|
|
||||||
cfg verbose true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Treat file:// as local paths.
|
handle_mime() {
|
||||||
[[ "$target" =~ ^file:(//)?(/.+) ]] && target=${BASH_REMATCH[2]}
|
target_mimetype=$(file -ib "$target")
|
||||||
|
|
||||||
# Figure out if we're in X and set $notifier if we are.
|
for m in "${!mime_handlers[@]}"; do
|
||||||
if [[ $DISPLAY ]]; then
|
[[ "$target_mimetype" =~ ${mime_handlers[${m}]} ]] && {
|
||||||
# Do we have notify-send?
|
${m} "$target" &
|
||||||
notifier=$(type -P 'notify-send')
|
return 0
|
||||||
fi
|
}
|
||||||
|
done
|
||||||
|
|
||||||
handle_target cmd_result "$target"
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
case $? in
|
main() {
|
||||||
(1)
|
target="$1"
|
||||||
error "Action on “$target” failed with exit code: “$cmd_result”"
|
[[ "$target" ]] || { usage; exit; }
|
||||||
return 4
|
|
||||||
;;
|
|
||||||
|
|
||||||
(2)
|
handle_uri "$target" || {
|
||||||
error "No such file or directory: “$target”"
|
[[ "$target" =~ file://.+ ]] && { target="${target##*file://}"; }
|
||||||
return 2
|
|
||||||
;;
|
|
||||||
|
|
||||||
(3)
|
[[ -e "$target" ]] && {
|
||||||
error "No handlers found for “$target”"
|
[[ "$target" =~ ^/.* ]] || { target="${PWD}/${target}"; }
|
||||||
return 3
|
|
||||||
;;
|
handle_mime "$target"
|
||||||
esac
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[[ "$?" -gt 0 ]] && { echo "No handlers found for $target"; }
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
37
sx-open.cfg
37
sx-open.cfg
|
@ -1,33 +1,14 @@
|
||||||
#!syntax bash
|
|
||||||
# Configuration file for sx-open
|
# Configuration file for sx-open
|
||||||
# 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.
|
||||||
# Regexes imply '^$'
|
|
||||||
|
|
||||||
# Disable desktop notifications
|
uri_handlers=(
|
||||||
#cfg notify false
|
["steam"]='^steam://.+'
|
||||||
|
["javaws"]='kvm.+?\.cgi$'
|
||||||
|
["browser"]='^http(s)://.+'
|
||||||
|
)
|
||||||
|
|
||||||
# Enable verbose mode
|
mime_handlers=(
|
||||||
#cfg verbose true
|
["sxiv"]='image/.+'
|
||||||
|
)
|
||||||
|
|
||||||
# Enable dry run mode (implies verbose)
|
# vim: syntax=sh
|
||||||
#cfg dryrun true
|
|
||||||
|
|
||||||
# <cmd> macros:
|
|
||||||
# %target% — The first argument to this script.
|
|
||||||
# If not found, target is appended to the end of <cmd>
|
|
||||||
# example:
|
|
||||||
#uri 'browser %target% --profile=work' 'https://.+\.?workdomain\.tld\..*'
|
|
||||||
|
|
||||||
# scheme <cmd> <scheme>[ <scheme> ...]
|
|
||||||
#scheme browser http https
|
|
||||||
#scheme steam steam
|
|
||||||
|
|
||||||
# Or you can specify a regex for the entire uri:
|
|
||||||
# uri <cmd> <regex>[ <regex> ...]
|
|
||||||
#uri steam 'steam:.+'
|
|
||||||
#uri browser '(https?|ftp):.+'
|
|
||||||
|
|
||||||
# Mime types for filesystem targets:
|
|
||||||
# mime <cmd> <regex>[ <regex> ...]
|
|
||||||
#mime sxiv 'image/.+'
|
|
||||||
#mime libreoffice 'application/msword'
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user