#!/bin/sh set -e [ -n "$DEBUG" ] && set -x manual_actions_handling= act_on_history= while getopts mHh opt; do case "$opt" in # don't use makoctl menu ... m) manual_actions_handling=1 ;; H) act_on_history=1 ;; h | ?) printf 'usage: %s [-h]\n' "$(basename "$0")" printf '\n' printf ' -m handle actions using bemenu instead of "makectl menu"\n' printf ' -H act on the history buffer, instead of active notifications\n' printf "\n" exit 2 ;; esac done shift $((OPTIND - 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 --accept-single "$@" } list_notifications() { if [ -n "${act_on_history}" ]; then makoctl history else makoctl list fi } select_notification() { while read -r line; do # actually only supports 1 id=$(printf '%s' "${line}" | awk -F'\t' '{print $1}') list_notifications | jq -r ".. | select(.id?.data?==${id}) | @text" done } handle() { options=" dismiss actions $(if [ -n "${act_on_history}" ]; then printf restore; fi) " while read -r json; do if [ -n "$DEBUG" ]; then printf '%s' "${json}" | jq; fi for o in $options; do printf '%s\n' "$o"; done | 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}" ;; restore) handle_restore "${json}" ;; esac } done } handle_dismiss() { id=$(printf '%s' "${1}" | jq -r '.id.data') makoctl dismiss -n "${id}" } handle_restore() { id=$(printf '%s' "${1}" | jq -r '.id.data') makoctl restore -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 } list_notifications | jq --arg id "${1}" -r '(..|select(.id?.data?==$id)) // .data[][] | [.id.data, ."app-name".data, .summary.data, .body.data] | @tsv' | if [ -n "${1}" ]; then tac | tac; else _menu; fi | select_notification | handle