dot_testing/lappy/builds/utils/status

139 lines
4.3 KiB
Plaintext
Raw Normal View History

#!/bin/bash
# try to load the sleep builtin dynamically, because otherwise there's
# a *lot* of overhead (in terms of PID count)
if [[ $(</proc/cmdline) = *"nixos-system"* ]]; then
enable -f /run/current-system/sw/lib/bash/sleep sleep
else
enable -f /lib/bash/sleep sleep
fi
2024-12-04 13:37:07 -06:00
schedule_needs_update="yes"
2024-12-05 10:27:49 -06:00
utc_today=$(date --date '00:00:00 today' '+%s')
2024-12-04 13:37:07 -06:00
get_schedule_info () {
2024-12-05 10:27:49 -06:00
if [ -z "$schedule_state" ]; then
schedule_state="needs_init"
2024-12-04 13:37:07 -06:00
fi
2024-12-05 10:27:49 -06:00
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'
)
2024-12-04 13:37:07 -06:00
2024-12-05 10:27:49 -06:00
for ((i=0; i < ${#lines[@]}; i++)); do
readarray -t -d'|' line <<<"${lines[i]}"
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
fi
current_class_until_event=$((current_class_until_event - 1))
if [ "$current_class_until_event" -eq 0 ]; then
schedule_state="needs_recalc"
fi
;;
esac
2024-12-04 13:37:07 -06:00
}
get_cmus_info () {
if [ /dev/shm/status/cmus -nt /dev/shm/status/modified ]; then
2024-12-04 13:37:07 -06:00
cmus_status="$(</dev/shm/status/cmus)/"
touch /dev/shm/status/modified
fi
}
2024-12-04 13:37:07 -06:00
get_battery_info () {
battery_status=$(</sys/class/power_supply/BAT0/capacity)
}
get_time_info () {
printf -v date_status '%(%d(%a)-%m(%b)-%y)T %(%H)T:%(%M)T:%(%S)T/' -1
}
while true; do
# use /dev/shm to minimize the amount of i/o on disk
if [ ! -d /dev/shm/status ]; then
mkdir -p /dev/shm/status
touch /dev/shm/status/modified
fi
2024-12-04 13:37:07 -06:00
get_time_info
get_battery_info
get_cmus_info
2024-12-04 13:37:07 -06:00
get_schedule_info
2024-12-04 13:37:07 -06:00
printf "%s%s%s%s\n" "$schedule_status" "$cmus_status" "$date_status" "$battery_status"
sleep 1
done | dwm-setstatus