-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkhal-calendar
executable file
·468 lines (386 loc) · 12.6 KB
/
khal-calendar
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/bin/bash
#
# khal-calendar
#
# Display khal calendar popup for i3blocks
#
# Dependencies:
# khal
# rofi
# jq
#
# Copyright (c) 2024 Beau Hastings. All rights reserved.
# License: GNU General Public License v2
#
# Author: Beau Hastings <[email protected]>
# URL: https://github.com/hastinbe/khal-calendar
export KHAL_CONFIG="$HOME/.config/khal/config"
empty() {
[[ -z $1 ]]
}
not_empty() {
[[ -n $1 ]]
}
command_exists() {
command -v "$1" >/dev/null 2>&1
}
convert_ansi_color_codes_to_pango() {
awk '
{
if (NF == 0) {
print ""
next
}
line=$0
bold_count=0
highlight_count=0
# Start markup
line = "<markup>" line
# Replace bold markers
while (match(line, /\x1b\[1m/)) {
bold_count++
line=substr(line,1,RSTART-1) "<b>" substr(line,RSTART+RLENGTH)
}
# Replace highlight markers (current day)
while (match(line, /\x1b\[7m/)) {
highlight_count++
line=substr(line,1,RSTART-1) "<span background=\"'"$TODAY_BG"'\" foreground=\"'"$TODAY_FG"'\" weight=\"bold\">" substr(line,RSTART+RLENGTH)
}
# Replace reset markers
while (match(line, /\x1b\[0m/)) {
close_tags = ""
if (highlight_count > 0) {
close_tags = close_tags "</span>"
highlight_count--
}
if (bold_count > 0) {
close_tags = close_tags "</b>"
bold_count--
}
line=substr(line,1,RSTART-1) close_tags substr(line,RSTART+RLENGTH)
}
# Clean up any remaining escape sequences
gsub(/\x1b\[[0-9;]*[mK]/, "", line)
# Close any remaining open tags at the end of the line
while (highlight_count > 0) {
line = line "</span>"
highlight_count--
}
while (bold_count > 0) {
line = line "</b>"
bold_count--
}
# Close markup
line = line "</markup>"
print line
}
'
}
convert_i3_to_rofi_font() {
local i3_font="$1"
# Check if the input starts with "pango:", if so, remove it
if [[ "$i3_font" == pango:* ]]; then
i3_font="${i3_font#pango:}"
fi
# Remove any commas from the font description
local -r rofi_font="${i3_font//,/}"
echo "$rofi_font"
}
get_status_line() {
local -r current_time=$(date "+%H:%M")
local -r next_event=$(khal_command list --format "$EVENT_FORMAT" today tomorrow | head -n 1)
if not_empty "$next_event"; then
echo "$current_time | Next: $next_event"
else
echo "$current_time"
fi
}
add_event() {
local event_string
event_string=$(rofi -dmenu -p "Add Event (format: 2024-01-20 14:00 Event Name)" -lines 0)
if not_empty "$event_string"; then
local date time title
date=$(echo "$event_string" | cut -d' ' -f1)
time=$(echo "$event_string" | cut -d' ' -f2)
title=$(echo "$event_string" | cut -d' ' -f3-)
if ! validate_date_format "$date"; then
return 1
fi
if ! validate_time_format "$time"; then
return 1
fi
if empty "$title"; then
log_error "Event title is required"
return 1
fi
debug "Adding event: $date $time $title"
khal_command new "$date" "$time" "$title"
if [[ $? -ne 0 ]]; then
log_error "Failed to add event"
return 1
fi
fi
display_date
}
khal_command() {
local cmd=("$@")
local result=()
local found_command=false
# add KHAL_CONFIG
# result+=("-c" "$KHAL_CONFIG")
for arg in "${cmd[@]}"; do
if [[ $found_command == false && $arg != -* ]]; then
# First non-option argument (the "command") is found
result+=("$arg")
found_command=true
# Add calendars after the command
if not_empty "$CALENDARS"; then
for cal in "${CALENDARS[@]}"; do
result+=("-a" "$cal")
done
fi
else
# Add other arguments as-is
result+=("$arg")
fi
done
debug "Running khal command: khal ${result[*]}"
khal "${result[@]}" 2>&1
}
show_calendar() {
trap "killall rofi" EXIT # Ensure rofi is killed when the function exits
# Save the original i3blocks variables at the very start
local orig_label="$LABEL"
local orig_color="$COLOR"
local current_month
local calendar
local len
local status_line
local initial_month # Add this to store the initial month
current_month=$(date +%Y-%m-01)
initial_month=$current_month # Store initial month
calendar="$(khal_command --color calendar | convert_ansi_color_codes_to_pango)"
len=$(echo "$calendar" | wc -l)
status_line=$(get_status_line)
debug "Starting calendar display for $current_month"
debug "Original label: '$orig_label'"
debug "Original color: '$orig_color'"
while true; do
echo "$calendar" | rofi \
-dmenu \
-font "$FONT" \
-m -3 \
-markup-rows \
-p "$status_line" \
-theme "$ROFI_CONFIG_FILE" \
-theme-str "
* {
background-color: $CALENDAR_BG;
text-color: $CALENDAR_FG;
}
window {
padding: 10px;
width: $ROFI_WIDTH;
anchor: $ANCHOR;
location: $ROFI_LOCATION;
}
listview {
lines: $len;
scrollbar: false;
}" \
"${ROFI_CALENDAR_OPTIONS[@]}"
rofi_exit_code=$?
if [ $rofi_exit_code -eq 1 ]; then
debug "Exiting calendar display"
# Reset to initial month before displaying date
if [[ $current_month != $initial_month ]]; then
khal_command --color calendar "$initial_month" >/dev/null 2>&1
fi
# Explicitly set the variables before displaying date
export LABEL="$orig_label"
export COLOR="$orig_color"
debug "Restoring label: '$LABEL'"
debug "Restoring color: '$COLOR'"
display_date
return
fi
case $rofi_exit_code in
10) current_month=$(date -d "$current_month next month" +%Y-%m-01) ;; # Alt+n - Next month
11) current_month=$(date -d "$current_month last month" +%Y-%m-01) ;; # Alt+p - Previous month
12) add_event ;; # Alt+a - Add event
13) show_events "Today's Events" "today" ;; # Alt+t - Show today's events
* ) display_date; return ;; # Any other key (including Enter)
esac
calendar="$(khal_command --color calendar "$current_month" | convert_ansi_color_codes_to_pango)"
len=$(echo "$calendar" | wc -l)
done
trap - EXIT
}
display_date() {
# Add label if it exists
if not_empty "$LABEL"; then
echo -n "$LABEL"
fi
# if not_empty "$CALENDARS"; then
# echo -n "${#CALENDARS[@]} | "
# fi
# Full format (first line)
date "$DATEFMT"
# Short format (second line)
date "$SHORTFMT"
# Color (optional third line, if set in i3blocks config)
if not_empty "$COLOR"; then
echo "$COLOR"
fi
}
show_events() {
local -r title="$1"
shift
debug "Showing events for $*"
khal_command list "$@" | rofi -dmenu -markup-rows -p "$title"
# khal list -a "private" "$@" | rofi -dmenu -markup-rows -p "$title"
display_date
}
validate_date_format() {
local -r date="$1"
debug "Validating date format: $date"
if ! date -d "$date" >/dev/null 2>&1; then
log_error "Invalid date format: $date"
return 1
fi
return 0
}
validate_time_format() {
local -r time="$1"
debug "Validating time format: $time"
if ! [[ $time =~ ^[0-2][0-9]:[0-5][0-9]$ ]]; then
log_error "Invalid time format: $time"
return 1
fi
return 0
}
log_error() {
echo "ERROR: $*" >&2
}
check_dependencies() {
local -ra dependencies=("khal" "rofi" "jq")
local missing=0
for dep in "${dependencies[@]}"; do
if ! command_exists "$dep"; then
log_error "Error: $dep not found, check your PATH environment variable."
missing=1
fi
debug "Found $dep"
done
[[ $missing -eq 1 ]] && exit "$EX_UNAVAILABLE"
}
load_bar_config() {
if not_empty "$BAR_ID"; then
BAR_CONFIG=$(i3-msg -t get_bar_config "$BAR_ID")
else
BAR_CONFIG=$(i3-msg -t get_bar_config "$(i3-msg -t get_bar_config | jq -r '.[]' | head -n 1)")
fi
empty "$POSITION" && POSITION=$(echo "$BAR_CONFIG" | jq -r '.position')
empty "$FONT" && FONT=$(convert_i3_to_rofi_font "$(echo "$BAR_CONFIG" | jq -r '.font')")
empty "$CALENDAR_BG" && CALENDAR_BG=$(echo "$BAR_CONFIG" | jq -r '.colors.background')
empty "$CALENDAR_FG" && CALENDAR_FG=$(echo "$BAR_CONFIG" | jq -r '.colors.statusline')
empty "$TODAY_BG" && TODAY_BG=$(echo "$BAR_CONFIG" | jq -r '.colors.active_workspace_bg')
empty "$TODAY_FG" && TODAY_FG=$(echo "$BAR_CONFIG" | jq -r '.colors.active_workspace_text')
debug "Loaded bar config: $BAR_CONFIG"
debug "Position: $POSITION"
debug "Font: $FONT"
debug "Calendar BG: $CALENDAR_BG"
debug "Calendar FG: $CALENDAR_FG"
debug "Today BG: $TODAY_BG"
debug "Today FG: $TODAY_FG"
}
check_lock() {
debug "Checking for lock file $LOCK_FILE"
if [ -f "$LOCK_FILE" ]; then
if kill -0 "$(cat "$LOCK_FILE")" 2>/dev/null; then
return 1
fi
rm "$LOCK_FILE"
fi
echo $$ > "$LOCK_FILE"
return 0
}
debug() {
[[ $DEBUG -eq 1 ]] && echo "DEBUG: $*" >&2
}
cleanup() {
rm -f "$LOCK_FILE"
debug "Cleaning up lock file $LOCK_FILE"
# Kill rofi processes spawned by this script
pkill -P "$SCRIPT_PID" rofi 2>/dev/null
debug "Killed rofi processes"
}
main() {
declare -ir SCRIPT_PID=$$
declare -r LOCK_FILE="/tmp/khal-calendar-${USER}.lock"
declare DEBUG=${DEBUG:-0}
# Add explicit handling of i3blocks variables
declare -g LABEL="${LABEL:-}"
declare -g COLOR="${COLOR:-}"
declare -g BLOCK_BUTTON="${BLOCK_BUTTON:-}"
declare -g CALENDARS="${CALENDARS:-}"
if not_empty "$CALENDARS"; then
IFS=',' read -r -a CALENDARS <<< "$CALENDARS"
debug "Found calendars: ${CALENDARS[@]}"
fi
declare -ir \
EX_OK=0 \
EX_UNAVAILABLE=69 \
EX_CANTCREAT=73
check_dependencies
if ! check_lock; then
log_error "Another instance is already running."
exit "$EX_CANTCREAT"
fi
# Basic display settings
declare BAR_ID="${BAR_ID:-}"
declare -r ROFI_CONFIG_FILE="${ROFI_CONFIG_FILE:-/dev/null}"
declare -r DATEFMT="${DATEFMT:-+%a %d %b %Y}"
declare -r SHORTFMT="${SHORTFMT:-+%d/%m/%Y}"
# Event format for khal
declare -r EVENT_FORMAT="${EVENT_FORMAT:-{start-time} {title}}"
# Rofi appearance
declare -r ROFI_WIDTH="${ROFI_WIDTH:-30%}"
declare -r ROFI_LOCATION="${ROFI_LOCATION:-northwest}"
# Keyboard shortcuts (using Alt instead of Ctrl to avoid conflicts)
declare -a ROFI_CALENDAR_OPTIONS=(
-kb-custom-1 "Alt+n"
-kb-custom-2 "Alt+p"
-kb-custom-3 "Alt+a"
-kb-custom-4 "Alt+t"
)
declare BAR_CONFIG
declare POSITION
declare FONT
declare CALENDAR_BG
declare CALENDAR_FG
declare TODAY_BG
declare TODAY_FG
declare ANCHOR="northeast"
if not_empty "$BAR_ID"; then
BAR_CONFIG=$(i3-msg -t get_bar_config "$BAR_ID")
else
BAR_CONFIG=$(i3-msg -t get_bar_config "$(i3-msg -t get_bar_config | jq -r '.[]' | head -n 1)")
fi
load_bar_config
if [[ $POSITION == "top" ]]; then
ANCHOR="northeast"
else
ANCHOR="southeast"
fi
# Handle click events
case "$BLOCK_BUTTON" in
1) show_calendar ;; # Left click - Show calendar
2) show_events "Today's Events" "today" ;; # Middle click - Show today's events
3) show_events "Upcoming Events" "today" "tomorrow" ;; # Right click - Show upcoming events
*) display_date ;;
esac
exit "$EX_OK"
}
main "$@"
trap cleanup EXIT INT TERM