diff options
| author | Robert Günzler <r@gnzler.io> | 2021-11-02 18:48:17 +0100 |
|---|---|---|
| committer | Robert Günzler <r@gnzler.io> | 2021-11-02 19:06:52 +0100 |
| commit | 2467ffc06ef0b2aacd4a4d50658f9637afd8f27c (patch) | |
| tree | ec0a8038a337637c458341509b0536f2aef4a133 | |
| parent | 0126e9252ea3d328c1bcfc98ff95bac6c50de3e5 (diff) | |
bin: add notifmenu
| -rwxr-xr-x | bin/notifmenu | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/bin/notifmenu b/bin/notifmenu new file mode 100755 index 0000000..abee229 --- /dev/null +++ b/bin/notifmenu @@ -0,0 +1,82 @@ +#!/bin/sh + +set -e +[ -n "$DEBUG" ] && set -x + +manual_actions_handling= +[ "${1}" = "-m" ] && manual_actions_handling=1 + +# check requirements +for tool in makoctl jq bemenu; do + if ! command -v "$tool" >/dev/null; then + printf "%s: %s not installed but required\n" "$(basename "$0")" "$tool" >&2 + exit 1 + fi +done + +export MENU_PROMPT=" notif" + +_menu() { + menu -l10 --ifne "$@" +} + +select_notification() { + while read -r line; do # actually only supports 1 + id=$(printf "%s" "${line}" | awk -F'\t' '{print $1}') + makoctl list | jq -r ".. | select(.id?.data==${id}) | @text" + done +} + +handle() { + while read -r json; do + [ -n "$DEBUG" ] && printf "%s" "${json}" | jq + printf "dismiss\nactions\n" | + MENU_PROMPT="${MENU_PROMPT}: $(printf "%s" "${json}" | jq -r '.summary.data')" _menu | + { + read -r line + case ${line} in + dismiss) handle_dismiss "${json}" ;; + actions) handle_actions "${json}" ;; + esac + } + done +} + +handle_dismiss() { + id=$(printf "%s" "${1}" | jq -r '.id.data') + makoctl dismiss -n "${id}" +} + +handle_actions() { + id=$(printf "%s" "${1}" | jq -r '.id.data') + + if [ -n "${manual_actions_handling}" ]; then + printf "%s" "${1}" | jq -r '.actions.data | to_entries | .[] | @text' | + _menu | + jq -r '.key' | + handle_action_manually "${id}" + else + exec makoctl menu -n "${id}" menu -l10 + fi +} + +handle_action_manually() { + id="$1" + while read -r action; do + printf "print\ninvoke (using makoctl)\n" | + _menu --prefix "${action} >" | + { + read -r line + case ${line} in + print) printf "%s\n" "${action}" ;; + invoke*) exec makoctl invoke -n "${id}" "${action}" ;; + esac + } + done +} + +makoctl list | + jq -r '.data[][] | [.id.data, ."app-name".data, .summary.data, .body.data] | @tsv' | + _menu | + select_notification | + handle |