Skip to content

Instantly share code, notes, and snippets.

@jcaesar
Created March 24, 2021 01:33
Show Gist options
  • Select an option

  • Save jcaesar/760562521077315c8e3892253d16ed1e to your computer and use it in GitHub Desktop.

Select an option

Save jcaesar/760562521077315c8e3892253d16ed1e to your computer and use it in GitHub Desktop.
Invoke notify-send based on khal, with a 15 and 3 minute wakeup margin - should play nice with all kinds of UTF-8 and special characters (Curse you, colleagues who put paths to windows shares into appointments. \\foo\)
#!/usr/bin/env bash
set -euo pipefail
trap 'echo "ERROR: $BASH_SOURCE:$LINENO $BASH_COMMAND" >&2' ERR
for prg in notify-send khal; do
if ! which $prg &>/dev/null; then
echo $prg not found
exit -1
fi
done
function clean_iconv {
iconv -c -t UTF-8
}
function clean_grep {
grep -axv '.*'
}
if which iconv &>/dev/null; then
clean=clean_iconv
else
clean=clean_grep
fi
#set -x
lastturn=$(date +%s -d"now - 120 minutes")
while true; do
turn=$(( $(date +%s) / 60 * 60 ))
khal list -f $'{start}\3{title}\3{description}\3\4' -df '' today \
| while IFS= read -rd $'\4' e; do
begin=$(date -d"$(cut <<<"$e" -z -d$'\3' -f1 | tr -d \\0)" +%s)
title="$(cut <<<"$e" -z -d$'\3' -f2 | tr -d \\0)"
desc="$(cut <<<"$e" -z -d$'\3' -f3 | tr -d \\0 | cut -c-50)"
notify=false
for n in 3 15; do
if test $lastturn -lt $(( $begin - 60 * $n )) && test $(( $begin - 60 * $n )) -le $turn; then
in=$(( ($begin - $(date +%s)) / 60 ))
prio=low
if test $in -lt 5; then
prio=critical
elif test $in -lt 16; then
prio=normal
fi
if test $in -gt 0; then
in="In $in min."
else
in="$(( - $in )) min. ago"
fi
echo "Reminding of $title ($in)"
notify-send -u $prio -- "$in: $($clean <<<"$title")" "$($clean <<<"$desc")" || echo notify-send failed
break
fi
done
done
lastturn=$turn
snooze=$(( $turn + 60 - $(date +%s) ))
if test $snooze -gt 0; then
sleep $snooze
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment