···11+#!/usr/bin/env bash
22+33+# notify a khal user via rofi about upcoming events
44+#
55+# dependencies: ·khal
66+# ·rofi >= 1.7.0 (-timeout-delay 60 -timeout-action "kb-cancel")
77+# ·column (eye candy only. can be removed without problems)
88+#
99+# usage: put it into a cronjob and let it run every 15 min or so
1010+# -only events with reminders will trigger an alarm (shown in khal as: ⏰)
1111+# -snoozed events will show up again on the next run
1212+# -dismissed events will not trigger an alarm again
1313+# -no alarm will be triggered if the script did never run
1414+# for the duration of an event or in TIMESPAN beforehand
1515+#
1616+# rofi: -Shift+Return toggles item selection
1717+# -if any item is selected, "cursor" position has no effect
1818+# -timeout after 60 seconds; this snoozes all events
1919+####################
2020+2121+2222+# adjust cache file location to your liking
2323+cache_file="${XDG_CACHE_HOME:-$HOME/.cache}/khal-events"
2424+# events starting in the next TIMESPAN will trigger an alarm
2525+timespan='60min'
2626+####################
2727+2828+# this is needed for cronjobs and Xorg
2929+export DISPLAY=":0"
3030+snooze_string='💤 Snooze all.'
3131+dismiss_string='✅ Dismiss all.'
3232+# only show events with ⏰ symbol (=has alarm set)
3333+# remove arrows for all-day events to only have one notification
3434+# remove ⏰ symbol because it is obvious
3535+events="$(khal list --format \
3636+ '{calendar}: {cancelled}{start-style}{to-style}{end-style} {title}{description-separator}{description}{location}{repeat-symbol}{alarm-symbol}' \
3737+ now "${timespan}" \
3838+ | sed -ne '/⏰/ p' \
3939+ | sed -e 's/⏰//g' -e 's/\(↦\|↔\|⇥\)[^[:blank:]]*/all-day/g' \
4040+ | column -tl 3)"
4141+4242+# get events that are new compared to cache file
4343+new_events="$(diff -NwU 0 "${cache_file}" - << EOF | sed -ne '/^\+[^+]/ p' \
4444+ | sed -ne 's/^\+\([[:print:]].*\)$/\1/p'
4545+${events}
4646+EOF
4747+)"
4848+4949+[ -z "${new_events}" ] && exit 0
5050+5151+action="$(dunstify --action="default,Dismiss" --action="snooze,Snooze" "${new_events}")"
5252+5353+# input="$(printf '%s\n%s\n%s' "${snooze_string}" \
5454+# "${dismiss_string}" "${new_events}" \
5555+# | rofi -timeout-delay 60 -timeout-action "kb-cancel" \
5656+# -dmenu -i -u 2: -no-custom -multi-select -p \
5757+# '⏰ Alarm! Select alarms to snooze. Rest will be dismissed.')"
5858+5959+case "${action}" in
6060+ "snooze")
6161+ ;;
6262+ "default")
6363+ printf '%s' "${events}" > "${cache_file}"
6464+ ;;
6565+ *)
6666+ printf '%s' "${events}" | grep -Fve "${input}" > "${cache_file}"
6767+ ;;
6868+esac