dot_testing/lappy/builds/utils/status

81 lines
2.0 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"
get_schedule_info () {
if [ "$schedule_needs_update" = "yes" ]; then
output=$(
khal at now --format '{title}|{end-time}|{categories}' | \
awk -F'|' '{if($3 == "school") { print $1 "|" $2 }}'
)
if [ -z "$output" ]; then
schedule_needs_update="no"
schedule_status=""
return
fi
class_name=${output%%|*}
schedule_time=${output##*|}
schedule_time_hours=${schedule_time%%:*}
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
}
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