use notify-send if available

Signed-off-by: fbt <fbt@fleshless.org>
This commit is contained in:
Jack L. Frost 2018-07-27 18:32:42 +03:00
parent 2b1353ac9c
commit 240d35cba3

24
sx-open
View File

@ -18,6 +18,18 @@ act() {
return 0
}
notify() {
[[ $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
@ -122,21 +134,27 @@ main() {
# Treat file:// as local paths.
[[ "$target" =~ ^file:(//)?(/.+) ]] && target=${BASH_REMATCH[2]}
# Figure out if we're in X and set $notifier if we are.
if [[ $DISPLAY ]]; then
# Do we have notify-send?
notifier=$(type -P 'notify-send')
fi
handle_target cmd_result "$target"
case $? in
(1)
printf 'Action failed with exit code: “%s”\n' "$cmd_result" >&2
error "Action on “$target” failed with exit code: “$cmd_result”"
return 4
;;
(2)
printf 'No such file or directory: “%s”\n' "$target" >&2
error "No such file or directory: “$target”"
return 2
;;
(3)
printf 'No handlers found for “%s”\n' "$target" >&2
error "No handlers found for “$target”"
return 3
;;
esac