summary refs log tree commit diff
path: root/bin/notifmenu
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2022-08-17 15:41:21 +0200
committerRobert Günzler <r@gnzler.io>2022-08-17 15:41:21 +0200
commit1f08cc6adfffeea32d12d941c0f59dd875d22707 (patch)
tree9bf852e5f09aa346a1e143dafb6fc88f0a0f178b /bin/notifmenu
parentc6a67807ff922e43a1eebc4d28d24399a7841e86 (diff)
bin/notifmenu: support working on history
Diffstat (limited to '')
-rwxr-xr-xbin/notifmenu68
1 files changed, 50 insertions, 18 deletions
diff --git a/bin/notifmenu b/bin/notifmenu
index 2666472..48f8ca6 100755
--- a/bin/notifmenu
+++ b/bin/notifmenu
@@ -4,15 +4,28 @@ set -e
 [ -n "$DEBUG" ] && set -x
 
 manual_actions_handling=
-if [ "${1}" = "-m" ]; then
-	manual_actions_handling=1
-	shift 1
-fi
+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
+		printf '%s: %s not installed but required\n' "$(basename "$0")" "$tool" >&2
 		exit 1
 	fi
 done
@@ -20,41 +33,60 @@ done
 export MENU_PROMPT="󰡟 notif"
 
 _menu() {
-	menu -l10 --ifne "$@"
+	menu -l10 --ifne --autoselect "$@"
+}
+
+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}')
-		makoctl list | jq -r ".. | select(.id?.data?==${id}) | @text"
+		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
-		[ -n "$DEBUG" ] && printf "%s" "${json}" | jq
-		printf "dismiss\nactions\n" |
-			MENU_PROMPT="${MENU_PROMPT}: $(printf "%s" "${json}" | jq -r '.summary.data')" _menu |
+		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')
+	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')
+	id=$(printf '%s' "${1}" | jq -r '.id.data')
 
 	if [ -n "${manual_actions_handling}" ]; then
-		printf "%s" "${1}" | jq -r '.actions.data | to_entries | .[] | @text' |
+		printf '%s' "${1}" | jq -r '.actions.data | to_entries | .[] | @text' |
 			_menu |
 			jq -r '.key' |
 			handle_action_manually "${id}"
@@ -66,20 +98,20 @@ handle_actions() {
 handle_action_manually() {
 	id="$1"
 	while read -r action; do
-		printf "print\ninvoke (using makoctl)\n" |
+		printf 'print\ninvoke (using makoctl)\n' |
 			_menu --prefix "${action} >" |
 			{
 				read -r line
 				case ${line} in
-				print) printf "%s\n" "${action}" ;;
+				print) printf '%s\n' "${action}" ;;
 				invoke*) exec makoctl invoke -n "${id}" "${action}" ;;
 				esac
 			}
 	done
 }
 
-makoctl list |
+list_notifications |
 	jq --arg id "${1}" -r '(..|select(.id?.data?==$id)) // .data[][] | [.id.data, ."app-name".data, .summary.data, .body.data] | @tsv' |
-	if [ -z "${1}" ]; then _menu; else tac | tac; fi |
+	if [ -n "${1}" ]; then tac | tac; else _menu; fi |
 	select_notification |
 	handle