update the statusbar to call khal once

This commit is contained in:
stupidcomputer 2024-12-05 10:27:49 -06:00
parent 899f42fb88
commit 695db5a769
1 changed files with 90 additions and 32 deletions

View File

@ -9,43 +9,101 @@ else
fi fi
schedule_needs_update="yes" schedule_needs_update="yes"
utc_today=$(date --date '00:00:00 today' '+%s')
get_schedule_info () { get_schedule_info () {
if [ "$schedule_needs_update" = "yes" ]; then if [ -z "$schedule_state" ]; then
output=$( schedule_state="needs_init"
khal at now --format '{title}|{end-time}|{categories}' | \ fi
awk -F'|' '{if($3 == "school") { print $1 "|" $2 }}'
case "$schedule_state" in
"needs_init")
readarray -t lines < <(
khal list today 1d --format '{categories}|{title}|{start-time}|{end-time}' |
grep '^school.*' |
sed 's/school|//g'
) )
if [ -z "$output" ]; then for ((i=0; i < ${#lines[@]}; i++)); do
schedule_needs_update="no" readarray -t -d'|' line <<<"${lines[i]}"
schedule_status="" class_names+=("${line[0]}")
starts=${line[1]}
ends=${line[2]}
# convert to unix time
class_starts_hours=${starts%%:*}
class_starts_hours=$((10#$class_starts_hours))
class_starts_hours=$((class_starts_hours * 3600))
class_starts_minutes=${starts##*:}
class_starts_minutes=$((10#$class_starts_minutes))
class_starts_minutes=$((class_starts_minutes * 60))
class_starts_offset=$((class_starts_hours + class_starts_minutes))
class_starts_final=$((utc_today + class_starts_offset))
class_ends_hours=${ends%%:*}
class_ends_hours=$((10#$class_ends_hours))
class_ends_hours=$((class_ends_hours * 3600))
class_ends_minutes=${ends##*:}
class_ends_minutes=$((10#$class_ends_minutes))
class_ends_minutes=$((class_ends_minutes * 60))
class_ends_offset=$((class_ends_hours + class_ends_minutes))
class_ends_final=$((utc_today + class_ends_offset))
class_starts+=("$class_starts_final")
class_ends+=("$class_ends_final")
done
schedule_state="needs_recalc"
;;
"needs_recalc")
schedule_enabled="no"
printf -v ctime '%(%s)T' -1
for ((i=0; i < ${#class_names[@]}; i++)); do
if [ $ctime -lt ${class_starts[$i]} ] && [ $ctime -gt ${class_ends[$(($i - 1))]} ]; then
class_secs_left=$((${class_starts[$i]} - $ctime))
current_class_until_event="$class_secs_left"
current_class_name="${class_names[$i]}"
current_class_status="waiting"
schedule_enabled="yes"
elif [ $ctime -gt ${class_starts[$i]} ] && [ $ctime -lt ${class_ends[$i]} ]; then
class_secs_left=$((${class_ends[$i]} - $ctime))
current_class_until_event="$class_secs_left"
current_class_name="${class_names[$i]}"
current_class_status="inside"
schedule_enabled="yes"
fi
done
if [ "$schedule_enabled" = "no" ]; then
current_class_status="finished"
fi
schedule_state="needs_dec"
;;
"needs_dec")
if [ "$current_class_status" = "inside" ]; then
if [ "$((current_class_until_event / 60))" -le 5 ]; then
printf -v schedule_status "%02d:%02d left in %s/" "$((current_class_until_event / 60))" "$((current_class_until_event % 60))" "$current_class_name"
else
printf -v schedule_status "%s minutes left in %s/" "$((current_class_until_event / 60))" "$current_class_name"
fi
elif [ "$current_class_status" = "outside" ]; then
if [ "$((current_class_until_event / 60))" -le 5 ]; then
printf -v schedule_status "%02d:%02d until %s/" "$((current_class_until_event / 60))" "$((current_class_until_event % 60))" "$current_class_name"
else
printf -v schedule_status "%s minutes until %s/" "$((current_class_until_event / 60))" "$current_class_name"
fi
elif [ "$current_class_status" = "finished" ]; then
printf -v schedule_status ""
return return
fi fi
class_name=${output%%|*} current_class_until_event=$((current_class_until_event - 1))
schedule_time=${output##*|} if [ "$current_class_until_event" -eq 0 ]; then
schedule_time_hours=${schedule_time%%:*} schedule_state="needs_recalc"
schedule_time_minutes=${schedule_time##*:}
schedule_time_unix=$(date --date "$schedule_time" "+%s")
schedule_needs_update="no"
fi
printf -v current_time '%(%s)T' -1
difference=$(($schedule_time_unix - $current_time))
if [ "$difference" -lt 0 ]; then
schedule_needs_update="yes"
get_schedule_info
fi
until_end=$(($difference / 60))
if [ "$until_end" -le 5 ]; then
until_end_seconds=$(($difference % 60))
schedule_status=$(printf "%02d:%02d left in %s/" "$until_end" "$until_end_seconds" "$class_name")
else
schedule_status=$(printf "%d minutes left in %s/" "$until_end" "$class_name")
fi fi
;;
esac
} }
get_cmus_info () { get_cmus_info () {