Configuration is serious business
Signed-off-by: fbt <fbt@fleshless.org>
This commit is contained in:
parent
614b2bea94
commit
213c8e4ce2
96
sx-open
96
sx-open
@ -13,13 +13,83 @@ usage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
act() {
|
act() {
|
||||||
(( verbose )) && printf 'CMD: %s\n' "$*" >&2
|
cfg verbose && printf 'CMD: %s\n' "$*" >&2
|
||||||
(( dry_run )) || { "$@"; return $?; }
|
cfg dryrun || { "$@"; return $?; }
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# 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'"
|
||||||
|
return 11
|
||||||
|
;;
|
||||||
|
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'"
|
||||||
|
return 13
|
||||||
|
}
|
||||||
|
|
||||||
|
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() {
|
notify() {
|
||||||
|
cfg notify || return 7 # disabled
|
||||||
|
|
||||||
[[ $DISPLAY ]] || return 3
|
[[ $DISPLAY ]] || return 3
|
||||||
[[ $notifier ]] || return 5
|
[[ $notifier ]] || return 5
|
||||||
|
|
||||||
@ -111,17 +181,26 @@ is_uri() [[ $1 =~ ^[a-zA-Z][a-zA-Z0-9\+\.\-]+:.+ ]]
|
|||||||
main() {
|
main() {
|
||||||
declare cmd_result target
|
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"
|
||||||
[[ -f "$cfg_file" ]] && { source "$cfg_file"; }
|
[[ -f "$cfg_file" ]] && { source "$cfg_file"; }
|
||||||
|
|
||||||
while (( $# )); do
|
while (( $# )); do
|
||||||
case $1 in
|
case $1 in
|
||||||
(-d) dry_run=1; verbose=1;;
|
(-d)
|
||||||
(-v) verbose=1;;
|
cfg dryrun = true
|
||||||
(-h) usage; return 0;;
|
cfg verbose = true
|
||||||
|
;;
|
||||||
|
|
||||||
(-n) notify() { true; };; # disable desktop notifications
|
(-v) cfg verbose = true;;
|
||||||
|
(-n) cfg notify = false;; # disable desktop notifications
|
||||||
|
|
||||||
|
(-h) usage; return 0;;
|
||||||
|
|
||||||
(--) shift; break;;
|
(--) shift; break;;
|
||||||
(*) break;;
|
(*) break;;
|
||||||
@ -132,7 +211,10 @@ main() {
|
|||||||
|
|
||||||
target=$1; [[ "$target" ]] || { usage; exit; }
|
target=$1; [[ "$target" ]] || { usage; exit; }
|
||||||
|
|
||||||
(( dry_run )) && printf 'Dry run: not actually running the handler\n' >&2
|
cfg dryrun && {
|
||||||
|
printf 'Dry run: not actually running the handler\n' >&2
|
||||||
|
cfg verbose true
|
||||||
|
}
|
||||||
|
|
||||||
# Treat file:// as local paths.
|
# Treat file:// as local paths.
|
||||||
[[ "$target" =~ ^file:(//)?(/.+) ]] && target=${BASH_REMATCH[2]}
|
[[ "$target" =~ ^file:(//)?(/.+) ]] && target=${BASH_REMATCH[2]}
|
||||||
|
@ -2,6 +2,15 @@
|
|||||||
# 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.
|
||||||
|
|
||||||
|
# Disable desktop notifications
|
||||||
|
#cfg notify false
|
||||||
|
|
||||||
|
# Enable verbose mode
|
||||||
|
#cfg verbose true
|
||||||
|
|
||||||
|
# Enable dry run mode (implies verbose)
|
||||||
|
#cfg dryrun true
|
||||||
|
|
||||||
# <cmd> macros:
|
# <cmd> macros:
|
||||||
# %target% — The first argument to this script.
|
# %target% — The first argument to this script.
|
||||||
# If not found, target is appended to the end of <cmd>
|
# If not found, target is appended to the end of <cmd>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user