summary refs log tree commit diff
path: root/bin/notifmenu
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2021-11-02 18:48:17 +0100
committerRobert Günzler <r@gnzler.io>2021-11-02 19:06:52 +0100
commit2467ffc06ef0b2aacd4a4d50658f9637afd8f27c (patch)
treeec0a8038a337637c458341509b0536f2aef4a133 /bin/notifmenu
parent0126e9252ea3d328c1bcfc98ff95bac6c50de3e5 (diff)
bin: add notifmenu
Diffstat (limited to 'bin/notifmenu')
-rwxr-xr-xbin/notifmenu82
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