~whynothugo/dotfiles

ref: 3768ec57 dotfiles/home/.local/lib/waybar-khal -rwxr-xr-x 2.0 KiB
3768ec57Hugo Osvaldo Barrera pacman: Fix mixup from hook relocation 3 months ago
                                                                                
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
#!/bin/sh
#
# Shows the calendar event that's happening now, of the next on if there isn't
# one.

set -e

render() {
  # Find events starting in two minutes.
  # So if my current event ends in two minutes and another one starts, the
  # widget is already updated with what's upcoming.
  SINCE="$(date -d 'now +2 min' '+%Y-%m-%d %H:%M')"
  UNTIL="$(date -d 'now +1 day' '+%Y-%m-%d %H:%M')"

  # TODO: On overlap, try to prefer the event where I've confirmed attendance.

  EVENT="$(
    khal list "$SINCE" "$UNTIL" \
      --day-format 'SKIPME' \
      --format "{start-end-time-style} {title:.31}{repeat-symbol}" |
      grep -v SKIPME |  # filter out headers
      grep -v -P '↦|↔ |⇥' |  # filter out continuing all day events
      grep -v '\[Lunch\]' |  # filter out lunch
      grep -v '^ ' |  # exclude full-day events
      head -n 1  # show just the first
  )"

  if [ -z "$EVENT" ]; then
    TEXT="(nothing upcoming)"
    CLASS="no-event"
  else
    TEXT=" $EVENT"
    CLASS="event"
  fi

  jq --compact-output \
    --null-input \
    --arg text "$TEXT" \
    --arg class "$CLASS" \
    '{"text": $text, "class": $class}'
}

render # Render once for initial state.

# TODO: try to debounce this, so as to avoid too many executions. This is
#       especially important since inotify may emit several events for just one
#       change.

# In order to make sure events are updated as time passes, this re-renderes
# every two minutes. That aside, whenever a calendar event changes, we alreays
# re-render immediately.
#
# It would be ideal to determine _when_ the current event ends, and set the
# timeout accordinly. That would require parsing khal's output a bit more.
while true; do
  inotifywait \
    --event modify \
    --event create \
    --event delete \
    --event close_write \
    --event moved_to \
    --event move \
    --monitor \
    --timeout 120 \
    --recursive \
    "$HOME/.local/share/calendars" 2> /dev/null | \
    while read -r _; do
      render
    done
done