Overhaul dunstctl

This commit is contained in:
Benedikt Heine 2019-08-11 15:13:16 +02:00 committed by Nikos Tsipinakis
parent 305ab6c4e6
commit 72d68fcec4

114
dunstctl
View File

@ -1,89 +1,75 @@
#!/bin/sh -eu #!/bin/sh
dbus_name="org.freedesktop.Notifications" set -eu
dbus_path="/org/freedesktop/Notifications"
dbus_ifac="org.dunstproject.cmd0"
dbus_property="org.freedesktop.DBus.Properties"
usage() { DBUS_NAME="org.freedesktop.Notifications"
echo "Usage: dunstctl <command> [options...]" DBUS_PATH="/org/freedesktop/Notifications"
echo "" DBUS_IFAC_DUNST="org.dunstproject.cmd0"
echo "Commands:" DBUS_IFAC_PROP="org.freedesktop.DBus.Properties"
echo " close [-a|--all] Close the last or all notifications" DBUS_IFAC_FDN="org.freedesktop.Notifications"
echo " context Open context menu of latest notification"
echo " display Redisplay closed notifications" function die(){ printf "%s\n" "${1}" >&2; exit 1; }
echo " status Reload the configuration file"
echo " help Show this help" function show_help() {
cat <<-EOH
Usage: dunstctl <command> [parameters]"
Commands:
close Close the last notification
close-all Close the all notifications
context Open context menu of latest notification
history-pop Pop one notification from history
status Check if
status-set [true|false] Set the status
help Show this help
EOH
} }
method_call() { function method_call() {
dbus-send --print-reply=literal --dest=$dbus_name $dbus_path $dbus_ifac."$*" dbus-send --print-reply=literal --dest="${DBUS_NAME}" "${DBUS_PATH}" "${1}"
} }
property_get() { function property_get() {
result=$(dbus-send --print-reply=literal --dest=$dbus_name $dbus_path $dbus_property.Get string:$dbus_ifac string:"$*") dbus-send --print-reply=literal --dest="${DBUS_NAME}" "${DBUS_PATH}" "${DBUS_IFAC_PROP}.Get" "string:${DBUS_IFAC_DUNST}" "string:${1}"
if test "${result#*'true'}" != "$result"; then
echo "dunst notifications are enabled"
else
echo "dunst notifications are disabled"
fi
} }
property_set() { function property_set() {
dbus-send --print-reply=literal --dest=$dbus_name $dbus_path $dbus_property.Set string:$dbus_ifac string:"$@" dbus-send --print-reply=literal --dest="${DBUS_NAME}" "${DBUS_PATH}" "${DBUS_IFAC_PROP}.Set" "string:${DBUS_IFAC_DUNST}" "string:${1}" "${2}"
} }
if [ $# -eq 0 ] || [ $# -gt 3 ]; then command -v dbus-send >/dev/null 2>/dev/null || \
usage die "Command dbus-send not found"
exit 1
fi
case "$1" in
case "${1:-}" in
"close") "close")
[ $# -lt 2 ] && param="" || param="$2" method_call "${DBUS_IFAC_DUNST}.NotificationCloseLast" >/dev/null
case "$param" in
"-a"|"--all")
method_call NotificationCloseAll
;; ;;
"") "close-all")
method_call NotificationCloseLast method_call "${DBUS_IFAC_DUNST}.NotificationCloseAll" >/dev/null
;;
*)
echo "dunstctl: unrecognized option '$2'"
exit 1
;;
esac
;; ;;
"context") "context")
method_call ContextMenuCall method_call "${DBUS_IFAC_DUNST}.ContextMenuCall" >/dev/null
;; ;;
"display") "history-pop")
method_call NotificationShow method_call "${DBUS_IFAC_DUNST}.NotificationShow" >/dev/null
;; ;;
"status") "status")
[ $# -lt 2 ] && action="" || action="$2" property_get running | ( read _ _ status; printf "%s\n" "${status}"; )
case "$action" in
"disable")
property_set running variant:boolean:false
;; ;;
"enable") "status-set")
property_set running variant:boolean:true [ "${2:-}" ] \
;; || die "No status parameter specified. Please give either 'true' or 'false' as running parameter."
""|"get") [ "${2}" == "true" ] || [ "${2}" == "false" ] \
property_get running || die "Please give either 'true' or 'false' as running parameter."
;; property_set running variant:boolean:"${2}"
*)
echo "dunstctl status: unrecognized option '$2'"
exit 1
;;
esac
;; ;;
"help"|"--help"|"-h") "help"|"--help"|"-h")
usage show_help
;;
"")
die "dunstctl: No command specified. Please consult the usage."
;; ;;
*) *)
echo "dunstctl: unrecognized command '$1'" die "dunstctl: unrecognized command '${1:-}'. Please consult the usage."
exit 1
;; ;;
esac esac