summary refs log tree commit diff
path: root/bin/notifmenu
blob: 6b993a6dbb61d2f064965b23403ae7bcb5c46ad4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/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