-
Notifications
You must be signed in to change notification settings - Fork 1
/
calendar.tool
executable file
·105 lines (84 loc) · 2.5 KB
/
calendar.tool
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
#!/bin/bash
# notify of next event (with action to open it)
calendar="Meetings"
priority="normal"
notify(){
notify-send -u "$priority" -a "in $calendar" -i "calendar" "$@"
}
seconds_left(){
local epoch
epoch="$(date --date="$1" +%s)"
local now
now="$(date +%s)"
echo "$(( epoch - now ))"
}
print_time_left(){
time="$(seconds_left "$1")"
[[ "$time" -lt 3600 ]] &&
printf 'in %02dm' $((time/60)) &&
return
[[ "$time" -lt 86400 ]] &&
printf 'in %02dh%02dm' $((time/3600)) $((time%3600/60)) &&
return
printf 'in %dd %02dh%02dm' $((time/86400)) $((time%86400/3600)) $((time%3600/60))
}
readarray -t installed_apps < <(compgen -c)
readarray -t events < <(\
khal list \
-a "$calendar" \
--format "{start}{tab}{end}{tab}{title}{tab}{location}" \
--day-format "")
[[ "${#events[@]}" -eq 0 ]] &&
notify "Couldn't find events" &&
exit
for event in "${events[@]}"; do
readarray -d $'\t' -t event_fields < <(echo "$event")
start=${event_fields[0]}
end=${event_fields[1]}
title="${event_fields[2]}"
location="${event_fields[3]}"
# check if event is already over
end_secs="$(seconds_left "$end")"
[ "$end_secs" -lt 0 ] && continue
body="<b>time:</b>\n$start ($(print_time_left "$start"))\n"
actions=()
case "$location" in
http*)
body+="<b>location:</b>\nas link"
actions+=( "--action=open=$location")
;;
$'\n')
;;
*)
# shellcheck disable=SC2076
if [[ " ${installed_apps[*]} " =~ " ${location} " ]]; then
body+="<b>app:</b>\n$location"
actions+=( "--action=launch=$location")
else
body+="<b>location:</b>\n$location"
actions+=( "--action=search=google")
[ "$(echo "$location" | wc -w)" -gt 1 ] &&
actions+=( "--action=location=google maps")
fi
;;
esac
action="$( notify "$title" "$body" "${actions[@]}")"
printf "%s\n" "${actions[@]}"
echo "$action"
[ "$action" ] || exit
case "$action" in
launch)
"$location" & disown
;;
open)
xdg-open "$location"
;;
search)
xdg-open "https://www.google.com/search?q=${location// /\+}"
;;
location)
xdg-open "https://www.google.com/maps/place/${location// /\+}"
;;
esac
exit
done