From 2467ffc06ef0b2aacd4a4d50658f9637afd8f27c Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Tue, 2 Nov 2021 18:48:17 +0100 Subject: bin: add notifmenu --- bin/notifmenu | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100755 bin/notifmenu (limited to 'bin') 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 -- cgit 1.4.1